Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add macros to throw in debug builds and log in production #534

Merged
merged 2 commits into from
Apr 14, 2023

Conversation

bcaimano
Copy link
Contributor

We already had a near identical macro in use in source files, let's provide it for more general usage with some warnings.

@bcaimano bcaimano requested review from vlovich and jasnell April 11, 2023 20:02
@bcaimano
Copy link
Contributor Author

CC @byule this is the macro we discussed recently for debug/test only asserts.

@kentonv
Copy link
Member

kentonv commented Apr 12, 2023

Our internal code has a very similar macro called DEBUG_FATAL_RELEASE_WARN. It's slightly different in that it crashes on failure (throws exception inside noexcept), but for the use cases here it would make sense to use (and in general, if you're in debug mode anyway, crashing is probably fine). Maybe we should move that macro here?

#ifdef KJ_DEBUG
void assertInvariantImpl();
#endif
void assertInvariant();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method was intentionally designed to be optimized out in release mode, since it is called every time a Data is moved (note that Data underlies jsg::Value and V8Ref).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, so we should definitely be documenting that. Is the concern that isEmpty() will be expensive or that throwing an exception will prevent the move ctor from being elided?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think just having an out-of-line function call in a move constructor likely makes life difficult for the compiler. Like I would hope the compiler can see when a variable is moved-away and then subsequently destroyed, the destructor can be elided because the move nulled out all the pointers. But if there's an out-of-line function call in the middle of the move then probably the compiler can't reason about it at all anymore.

Both performance and code bloat are concerns since we pass V8Refs by move in quite a few places.

Of course, if actual measurements show this has negligible impact, then I don't have a problem. But I dunno if it's worth spending time to determine that, vs. just leaving this as it is. I feel like the check here should provide sufficient coverage running only in debug mode, and I'm not even sure if I've ever seen it fail in any case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhhhhh, got it. Even if I felt confident that each rvalue ctor would be elided (it is allowed despite side effects after all), I wouldn't want to do this anyway since we'd run the risk of big log bursts if the assertion was incorrect when invoking the move assignment operator.

@bcaimano
Copy link
Contributor Author

Our internal code has a very similar macro called DEBUG_FATAL_RELEASE_WARN. It's slightly different in that it crashes on failure (throws exception inside noexcept), but for the use cases here it would make sense to use (and in general, if you're in debug mode anyway, crashing is probably fine). Maybe we should move that macro here?

Hmmmm, yeah, I'd be fine with using that more generally instead. (The situations I care about do feel like ERROR to me, but I was on the fence about providing a severity anyway.) Its use implies that we are not supporting debug releases of workerd for non-testing usage. Is that accurate?

@kentonv
Copy link
Member

kentonv commented Apr 13, 2023

Its use implies that we are not supporting debug releases of workerd for non-testing usage. Is that accurate?

Yeah, I'd say so.

bcaimano and others added 2 commits April 13, 2023 15:53
We already had a near identical macro in use in source files, let's provide it for more general usage with some warnings.

Co-authored-by: Milan Miladinovic <milan@cloudflare.com>
@bcaimano bcaimano force-pushed the bcaimano/sentry-log-assert branch from 2d89cb9 to a94ac02 Compare April 13, 2023 20:00
@bcaimano
Copy link
Contributor Author

Fyi, @MellowYarker I tagged you as a coauthor since you wrote our internal version.

@bcaimano bcaimano merged commit 7c2db20 into main Apr 14, 2023
@bcaimano bcaimano deleted the bcaimano/sentry-log-assert branch April 14, 2023 16:28
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants