-
Notifications
You must be signed in to change notification settings - Fork 59
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
It is way too hard to avoid including panicking and formatting infrastructure code #19
Comments
You could theoretically install a panic hook. Which is what I have done in one of my wasm projects in order to be able to find out what the panic was about. |
My experience (although it is more -emscripten like) is that changing However, there was still problems: sometimes this strings wasn't removed despite the fact As far as can tell that wasn't emscripten fault. |
Can one change the panic hook without resorting to unstable features? |
@fitzgen yes, the panic hook API is stable: https://doc.rust-lang.org/stable/std/panic/fn.set_hook.html Generally I don't object to an option that allows you to turn off all panic related things but there should be an option to keep the panic machinery, especially as we can't emit any source maps on the unknown target yet. I know this is a bit hard given that cargo can't recompile std :/. |
Crate authors could optionally set the panic hook based on a cargo feature. If setting a custom panic hook really does get rid of all this bloat, then we should document how to do this somewhere and then close this issue. |
Oh, and devtools show the disassembly, and if you have the "name" section ( |
@fitzgen I'm aware. They don't show the file name and line though. |
There are two mechanisms:
To make the first option work on stable, @japaric has written RFC 2070, but it still has to be implemented (tracking issue). We can't really use this as we are no |
I feel like having In the meantime I guess just going |
It seems that it is also issue for wasm32-unknown-unknown target. |
I'm so glad so many people are having these issues. I've been working on an RFC for a while (since December) on making a fmtless panic, as it is crazy that the core of the language (panic) requires one of the most bloated parts of core. I didn't really work on the RFC seriously as I thought I was the only one with this issue and it wouldn't go anywhere. I'll start to prioritize it. Currently the model is to have an opt-in fmt-less panic routine, perhaps just provides source file and line number (which for embedded targets is glorious debugging as is). |
To me this is a pretty broad and general issue that's not going to have one solution but rather a lot of different pieces. There's a whole slew of reasons that panicking infrastructure / formatting infrastructure are difficult to remove today. I don't think it's worthwhile to blanket shoot for "remove everything at all costs" because panics are actually quite useful when debugging and such. I feel like it's always going to be true that if you're optimizing for code size (like you are on wasm) you'll be writing Rust differently than you would if you weren't optimizing for code size. Most of the idioms of Rust (not just panics) are not optimized for code size, and that's a debt you need to pay down when optimizing for that. For me I think a clear first step towards making this issue less painful is to start looking and reorganizing the standard library where possible. There's plenty of locations in the standard library that contain panics when they shouldn't. In some cases LLVM just couldn't optimize it away or in others the code just needs to be restructured to remove the panic entirely. For example the formatting infrastructure should contain zero panics, but I doubt that's the case for today. Additionally I haven't figured out how to push on a Overall I feel like we shouldn't address the symptom here (panic bloat in a binary) but rather the cause (branches towards having a panic). That'll be a long-lasting solution and benefit basically all targets that Rust has. |
I slightly disagree with @alexcrichton here. While I always agree that we should minimize panics where they are not needed (there's no reason to not do this). Further I agree that there should really be no way of removing panics. They are fundamental to the language and should be kept. However there needs to be a way of implementing a panic that just does something like However fundamentally the internals of Rust have panics which are formatted (that are required and cannot be removed). This is great, for normal use it's nice to see that you indexed X bytes out of bounds or something. However a single use of There has to be a solution beyond just reducing panic use, and I think it has to be generic. Not a case-by-case remove some panics here and some panics there. I think there needs to be an optional panic routine that only takes static strings. This would be a nightmare for backwards compatibility as every panic would need to have a non-formatted alternative. However I think the solution is simpler. Just repr the actual format string. This is ugly, but if you are really that critical of code size then you have to make some sacrifices. For example with a model like this for a bounds check panic you would get something like:
For something like a Might not be the ideal panic message, but keep in mind without something like this I cannot get any message at all. I have to go entirely off of file and line (which honestly is pretty great for embedded work). TL;DR: Unless you can avoid all non- For example this code (built with
Has the code distribution: However by changing this code to have a potential panic due to a bounds check this code massively changes:
Just by panicing with a bounds check added 1485 bytes of code (not data). -B |
See also rust-embedded/wg#41 |
I think that one should be able to create |
Looks like this should help quite a bit too: rust-lang/rust#54981 (comment) (silent aborts, unfortunately also requires Xargo but it's a good step) |
@fitzgen i think we want to track this issue differently- i'm going to close (if i'm wrong tho please reopen and apologies!) |
@ashleygwilliams, could you let us know if/when this is being tracked in a different issue? I am subscribed to this thread because I’m interested in whatever progress is being made on this, but maybe there is another way to stay in the loop? |
It makes perfect sense that in a debug build, you should get the full panicking infrastructure, but in a release build, you should be able to enable a feature that replaces all panics with a free call to abort(). Has such a feature been made available since the last update on this issue thread? |
Think this is what you were looking for rust-lang/rust#54981 (comment) |
Yes, building std with |
Even though panicking just translates into a
trap
without any diagnostic messages, we still include tons ofbeing_panic_fmt
etc type code. This has a huge code size footprint: ~75% of my code size afterwasm-gc
!I had to write
wasm-snip
pretty much just for removing panicking and formatting infrastructure. But that is just a stop-gap, not a solution: it is fragile and manual.The text was updated successfully, but these errors were encountered: