Repository navigation
[NFC] Use quick_exit to skip global cleanup - #8212
Conversation
|
|
||
| void wasm::flush_and_quick_exit() { | ||
| std::cout << std::flush; | ||
| std::cerr << std::flush; |
There was a problem hiding this comment.
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.
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.
Sure but fflush(NULL) is fewer lines :)
There was a problem hiding this comment.
Or is there some difference between flushing C++ ostreams and C fflush?
There was a problem hiding this comment.
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.
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.
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.
... 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.
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.
(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.
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.
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.
Yeah, I think it's simpler without the callback - plumbing through the code, which I just added, is easy enough.
| std::cerr << "done." << std::endl; | ||
| } | ||
|
|
||
| flush_and_quick_exit(); |
There was a problem hiding this comment.
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.
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.
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.
| } | ||
| } | ||
|
|
||
| flush_and_quick_exit(); |
There was a problem hiding this comment.
I guess you should flag flush_and_quick_exit as [[noreturn]] ?
sbc100
left a comment
There was a problem hiding this comment.
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.
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.