-
Notifications
You must be signed in to change notification settings - Fork 833
[NFC] Use quick_exit to skip global cleanup #8212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
|
||
| void wasm::flush_and_quick_exit() { | ||
| std::cout << std::flush; | ||
| std::cerr << std::flush; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might want to add fflush(NULL) here, either in addtion or insteadof the above two lines.
If the stream argument is NULL, fflush() flushes all open output streams.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not an expert on this, but from the docs I think that is not needed, if all the C++ ostreams are closed? Closing them (e.g. by their destructor) should flush them, so only the standard output streams need special handling?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure but fflush(NULL) is fewer lines :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or is there some difference between flushing C++ ostreams and C fflush?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure but fflush(NULL) is fewer lines :)
But I think that C command flushes the file handles themselves, while the C++ command also flushes anything buffered at the C++ level..? That is, C++ has buffering in ostream, iirc - am I wrong?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough, I guess you never know if C++ is doing anything here... but i doubt its doing more buffering on top of what C is doing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added fflush(NULL). From the docs I am not certain it is not needed, and it seems safe/harmless.
dschuff
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... GH won't let me submit a "comment" review with this field blank. Is that new?
| } | ||
|
|
||
| void wasm::flush_and_quick_exit() { | ||
| std::cout << std::flush; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use std::at_quick_exit to register a function to explicitly flush the open descriptors. Can that be done for the output file too, or is that already guaranteed by some other means?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(the advantage of this would be that you could just use std::quick_exit(0) directly at the current flush_and_quick_exit callsites)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the output files should be flushed by their destructors? (this PR ensures those destructors run before exit)
Interesting about at_quick_exit - that could work, though I don't have a preference myself (doesn't seem like a problem to wrap quick_exit, in fact it might be nice for debugging) - wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a strong opinion, it doesn't make that much difference in the end, you either have this wrapper, or a callback. You wouldn't have to worry about plumbing the error code through or not, but you are right that the control is conceptually simpler without the callback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think it's simpler without the callback - plumbing through the code, which I just added, is easy enough.
src/tools/wasm2js.cpp
Outdated
| std::cerr << "done." << std::endl; | ||
| } | ||
|
|
||
| flush_and_quick_exit(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe flush_and_quick_exit(0) here? i.e. take the exit code as an argument.
Otherwise you should call the function flush_and_quick_exit_with_zero :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking that for errors we don't really care about a quick exit anyhow, so this would only be used for the fast/success path? But I don't feel strongly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but I still think its good to be explicit here. I think passing zero is the cleanest way. But you could also call it quick_exit_with_success but that seems like more typing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added explicit 0.
src/tools/wasm-opt.cpp
Outdated
| } | ||
| } | ||
|
|
||
| flush_and_quick_exit(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess you should flag flush_and_quick_exit as [[noreturn]] ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
sbc100
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm with return code passed for explicitness :)
|
One other thing that just occurred to me: do we run this code under ASan/LSan? is this going to cause false leak reports there? |
dschuff
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
I don't love that the destructors in the main functions of the tools now essentially become manual, that seems error-prone. But hopefully those mains can stay simple enough that it won't matter. I guess if we decide we don't like that, we could try to go for an "opt-out" rather than "opt-in" global destruction approach, where we write some kind of destructor that intentionally leaks those problematically-slow allocations, and then we are safer by default.
More likely it might defeat lsan completely by exiting before lsan can produce its report? I think the hard exit like this is probably a good approach since its what we do in lld: https://github.com/llvm/llvm-project/blob/e36ddff7a475bd18d68fbdaa6e4a8ad924f85490/lld/Common/ErrorHandler.cpp#L105-L108 In llvm we use the lower level C function |
Yeah, I don't love this either... If it wasn't actually 10% faster I would not consider it. As you said, these main functions are at least pretty simple, and we don't have a lot of them. |
|
Good points about lsan. I added code to avoid quick_exit when sanitizers run (shutdown speed doesn't matter there anyhow). I'll also verify manually that leak checks still work. |
This reverts commit 387ffb9.
|
LSan confirmed still functional. |
|
Re-measured locally to confirm the 10% speedup remains after all the changes here. |
We have global stores for interned strings and wasm types, and cleaning those
up takes a lot of time. This PR skips them, which makes roundtripping a large
Dart binary 10% faster.
Diff without whitespace is smaller - some code is now indented so that it
runs dtors before main exits.