-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
std: Minimize size of panicking on wasm #49488
Conversation
r? @sfackler |
So if I previously registered a custom panic handler to print the panic message via console.error (which gives you a nice stack trace btw), this panic handler will not print anything anymore? In production you probably won't want such a handler so the default setting is probably most useful for production settings. Only asking in order to understand this PR. |
src/libcore/panic.rs
Outdated
/// not use. | ||
#[unstable(feature = "std_internals", issue = "0")] | ||
#[doc(hidden)] | ||
pub trait BoxMeUp { |
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.
nit: shouldn't this be an unsafe trait
since the box_me_up
method must return a valid box pointer?
582ebbd
to
8050f5a
Compare
No, we will still invoke panic handlers |
src/libstd/panicking.rs
Outdated
@@ -164,6 +166,12 @@ fn default_hook(info: &PanicInfo) { | |||
#[cfg(feature = "backtrace")] | |||
use sys_common::backtrace; | |||
|
|||
// Some platfors know that printing to stderr won't ever actually print |
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.
platfor
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![unstable(feature = "thread_local_internals", issue = "0")] |
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.
wat
LGTM other than the fast thread local stuff that sounds like it's not needed. |
@alexcrichton yes but will it get a meaningful payload? |
8050f5a
to
03a18aa
Compare
📌 Commit 03a18aa has been approved by |
cc @rust-lang/wg-wasm; just a heads up!
|
03a18aa
to
033b177
Compare
@bors: r=sfackler |
📌 Commit 033b177 has been approved by |
⌛ Testing commit 033b17760deef5251f9151f0347ad2e7d1192f1f with merge 31927a8e74b50918ba8a2d9f73c0072912245e10... |
💔 Test failed - status-travis |
Your PR failed on Travis. Through arcane magic we have determined that the following fragments from the build log may contain information about the problem. Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
1 similar comment
Your PR failed on Travis. Through arcane magic we have determined that the following fragments from the build log may contain information about the problem. Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
|
☔ The latest upstream changes (presumably #49661) made this pull request unmergeable. Please resolve the merge conflicts. |
f886518
to
e9d6cc2
Compare
Ok I think I've debugged the issue of the failing test. In the meantime I did a few more size optimizations here for libstd, @sfackler mind re-reviewing and re-r+'ing? |
@bors: p=0 |
src/liballoc/raw_vec.rs
Outdated
@@ -736,6 +736,10 @@ fn alloc_guard(alloc_size: usize) -> Result<(), CollectionAllocErr> { | |||
} | |||
} | |||
|
|||
fn capacity_overflow() -> ! { |
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.
This feels non-obvious as to why it's extracted, maybe a comment would be good?
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.
👍
src/libcore/fmt/mod.rs
Outdated
@@ -1212,7 +1212,7 @@ impl<'a> Formatter<'a> { | |||
// truncation. However other flags like `fill`, `width` and `align` | |||
// must act as always. | |||
if let Some((i, _)) = s.char_indices().skip(max).next() { | |||
&s[..i] | |||
s.get(..i).unwrap_or(&s) |
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 a comment would be helpful...
src/libstd/panicking.rs
Outdated
@@ -369,9 +424,7 @@ pub fn begin_panic<M: Any + Send>(msg: M, file_line_col: &(&'static str, u32, u3 | |||
/// This is the entry point or panics from libcore, formatted panics, and | |||
/// `Box<Any>` panics. Here we'll verify that we're not panicking recursively, | |||
/// run panic hooks, and then delegate to the actual implementation of panics. | |||
#[inline(never)] | |||
#[cold] |
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.
Removing these seems fine, but it might be good to get some perf stats on some benchmark (though I don't have any particular ideas...).
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.
FWIW the comment here is out of date and no longer correct, this function hasn't survived well through the various refactorings. I'll update it.
Updated! |
e9d6cc2
to
bb9c9b2
Compare
☔ The latest upstream changes (presumably #49669) made this pull request unmergeable. Please resolve the merge conflicts. |
This commit applies a few code size optimizations for the wasm target to the standard library, namely around panics. We notably know that in most configurations it's impossible for us to print anything in wasm32-unknown-unknown so we can skip larger portions of panicking that are otherwise simply informative. This allows us to get quite a nice size reduction. Finally we can also tweak where the allocation happens for the `Box<Any>` that we panic with. By only allocating once unwinding starts we can reduce the size of a panicking wasm module from 44k to 350 bytes.
Create one canonical location which panics with "capacity overflow" instead of having many. This reduces the size of a `panic!("{}", 1)` binary on wasm from 34k to 17k.
The expression `&s[..i]` in general can panic if `i` is out of bounds or not on a character boundary for a string, and this caused the codegen for `Formatter::pad` to be a bit larger than it otherwise needed to be. This commit replaces this with `s.get(..i).unwrap_or(&s)` which while having different behavior if `i` is out of bounds has a much smaller code footprint and otherwise avoids the need for `unsafe` code.
This commit removes allocation of the panic message in instances like `panic!("foo: {}", "bar")` if we don't actually end up needing the message. We don't need it in the case of wasm32 right now, and in general it's not needed for panic=abort instances that use the default panic hook. For now this commit only solves the wasm use case where with LTO the allocation is entirely removed, but the panic=abort use case can be implemented at a later date if needed.
bb9c9b2
to
46d16b6
Compare
ping r? @sfackler |
@bors r+ |
📌 Commit 46d16b6 has been approved by |
std: Minimize size of panicking on wasm This commit applies a few code size optimizations for the wasm target to the standard library, namely around panics. We notably know that in most configurations it's impossible for us to print anything in wasm32-unknown-unknown so we can skip larger portions of panicking that are otherwise simply informative. This allows us to get quite a nice size reduction. Finally we can also tweak where the allocation happens for the `Box<Any>` that we panic with. By only allocating once unwinding starts we can reduce the size of a panicking wasm module from 44k to 350 bytes.
☀️ Test successful - status-appveyor, status-travis |
This commit applies a few code size optimizations for the wasm target to
the standard library, namely around panics. We notably know that in most
configurations it's impossible for us to print anything in
wasm32-unknown-unknown so we can skip larger portions of panicking that
are otherwise simply informative. This allows us to get quite a nice
size reduction.
Finally we can also tweak where the allocation happens for the
Box<Any>
that we panic with. By only allocating once unwinding startswe can reduce the size of a panicking wasm module from 44k to 350 bytes.