Skip to content

[NFC] Use quick_exit to skip global cleanup - #8212

Merged
kripken merged 11 commits into
WebAssembly:mainfrom
kripken:quick_exit
Jan 21, 2026
Merged

kripken merged 11 commits into
WebAssembly:mainfrom
kripken:quick_exit

Conversation

@kripken

@kripken kripken commented Jan 16, 2026

Copy link
Copy Markdown
Member

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.

@kripken
kripken requested review from dschuff and sbc100 January 16, 2026 00:18
Comment thread src/support/file.cpp

void wasm::flush_and_quick_exit() {
std::cout << std::flush;
std::cerr << std::flush;

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Member Author

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?

Copy link
Copy Markdown
Member

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 :)

Copy link
Copy Markdown
Member Author

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?

Copy link
Copy Markdown
Member Author

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?

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Member Author

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 dschuff left a comment

Copy link
Copy Markdown
Member

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?

Comment thread src/support/file.cpp
}

void wasm::flush_and_quick_exit() {
std::cout << std::flush;

Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Member

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)

Copy link
Copy Markdown
Member Author

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?

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Member Author

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.

Comment thread src/tools/wasm2js.cpp Outdated
std::cerr << "done." << std::endl;
}

flush_and_quick_exit();

Copy link
Copy Markdown
Member

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 :)

@kripken kripken Jan 16, 2026 •

Copy link
Copy Markdown
Member Author

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.

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added explicit 0.

Comment thread src/tools/wasm-opt.cpp Outdated
}
}

flush_and_quick_exit();

Copy link
Copy Markdown
Member

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]] ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done.

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Member

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 :)

@dschuff

dschuff commented Jan 16, 2026

Copy link
Copy Markdown
Member

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 dschuff left a comment

Copy link
Copy Markdown
Member

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.

@sbc100

sbc100 commented Jan 16, 2026

Copy link
Copy Markdown
Member

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 _Exit, but I think it amounts the same thing: https://github.com/llvm/llvm-project/blob/e36ddff7a475bd18d68fbdaa6e4a8ad924f85490/llvm/lib/Support/Unix/Process.inc#L433C27-L433C28

@kripken

kripken commented Jan 16, 2026

Copy link
Copy Markdown
Member Author

I don't love that the destructors in the main functions of the tools now essentially become manual, that seems error-prone.

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.

@kripken

kripken commented Jan 16, 2026

Copy link
Copy Markdown
Member Author

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.

@kripken

kripken commented Jan 17, 2026

Copy link
Copy Markdown
Member Author

LSan confirmed still functional.

@kripken

kripken commented Jan 17, 2026

Copy link
Copy Markdown
Member Author

Re-measured locally to confirm the 10% speedup remains after all the changes here.

@kripken
kripken merged commit 95fff89 into WebAssembly:main Jan 21, 2026
17 checks passed
@kripken
kripken deleted the quick_exit branch January 21, 2026 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants