-
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
Null terminate core::panic::Location
file strings
#117431
Conversation
@bors try @rust-timer queue |
r? @cuviper (rustbot has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
Null terminate `core::panic::Location` file strings This shrinks `Location` by a usize, making it 24->16 bytes on x86-64. This reduces binary size. I quickly measure `cargo install ripgrep`, which was reduced by 0.3%. That's not a lot, but it's something and the code change is quite simple. From some quick measurements, I expect librustc_driver to shrink by more, but rustc-perf should reveal more. The change to const interning is necessary, see https://rust-lang.zulipchat.com/#narrow/stream/146212-t-compiler.2Fconst-eval/topic/unsupported.20untyped.20pointer.20in.20constant.20error/near/399446285
ccff2ba
to
4761094
Compare
Some changes occurred to the CTFE / Miri engine cc @rust-lang/miri |
// For better errors later, mark the allocation as immutable. | ||
alloc.mutability = Mutability::Not; | ||
// The Location does have leftover allocations, but that's fine, as we control it. | ||
if alloc_kind != MemoryKind::CallerLocation { |
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.
Uh, this is bad. That code is already terrible and my attempts at cleaning it up in #116745 are kind of stuck. I really don't like adding more complications here.
MemoryKind::CallerLocation, | ||
Mutability::Not, | ||
) | ||
.unwrap() | ||
}; |
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 you can avoid the interning change by instead manually interning the inner allocation with the string here.
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.
that sounds good. how exactly do i do the interning? is it the
let alloc = tcx.mk_const_alloc(alloc);
tcx.set_alloc_id_memory(alloc_id, alloc);
part?
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 that's the core thing. We have some helpers in interpret/intern.rs
. Possible one of the existing one needs extension, currently there's basically 2 helpers for 2 users but it'd be good to start establishing some patterns. (That can also happen later though.)
This comment has been minimized.
This comment has been minimized.
#[cfg(not(bootstrap))] | ||
pub struct Location<'a> { | ||
file: *const (), | ||
_file_actual: PhantomData<&'a CStr>, |
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.
Why does Location
have a lifetime? Isn't it always 'static
?
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 know either - but it's stable :(
This saves 8 bytes in `Location` and allows for deduplicating file names more effectively through the linker in the future (by placing them in special seections).
4761094
to
fc2f71f
Compare
The job Click to see the possible cause of the failure (guessed by this bot)
|
#[stable(feature = "panic_hooks", since = "1.10.0")] | ||
#[cfg(not(bootstrap))] | ||
pub struct Location<'a> { | ||
file: *const (), |
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.
Needs an explicit Send
/Sync
impl now to keep the same API.
unsafe { | ||
let cstr = CStr::from_ptr(self.file as _); | ||
let len = cstr.count_bytes(); | ||
crate::str::from_utf8_unchecked(crate::slice::from_raw_parts(self.file as _, len)) |
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 makes calls here and to related (Hash, Debug, PartialEq, etc.) functions much more expensive. I guess we may not be able to do much better... but I think the binary size vs. possibly slower binaries that are keeping these locations around (e.g., HashMap) isn't as obvious.
It's possible we could make this near equivalent by avoiding the upfront 0-byte search internally.
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.
Another option would be doing a varint length prefix instead of a nul terminator. For short paths it would only be 1 byte so the same size as a nul terminator.
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.
Are these ever on the hot path?
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.
Logging, maybe?
there are quite some improvements that can be done, but let's perf this first |
This comment has been minimized.
This comment has been minimized.
Null terminate `core::panic::Location` file strings This shrinks `Location` by a usize, making it 24->16 bytes on x86-64. This reduces binary size. I quickly measure `cargo install ripgrep`, which was reduced by 0.3%. That's not a lot, but it's something and the code change is quite simple. From some quick measurements, I expect librustc_driver to shrink by more, but rustc-perf should give us more numbers. The change to const interning is necessary, see https://rust-lang.zulipchat.com/#narrow/stream/146212-t-compiler.2Fconst-eval/topic/unsupported.20untyped.20pointer.20in.20constant.20error/near/399446285
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (c5474df): comparison URL. Overall result: ✅ improvements - no action neededBenchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf. @bors rollup=never Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Bootstrap: 636.003s -> 634.282s (-0.27%) |
Are you intending to do more here? |
Given that this improvement here doesn't seem to really help, probably not. |
This shrinks
Location
by a usize, making it 24->16 bytes on x86-64. This reduces binary size. I quickly measurecargo install ripgrep
, which was reduced by 0.3%. That's not a lot, but it's something and the code change is quite simple. From some quick measurements, I expect librustc_driver to shrink by more, but rustc-perf should give us more numbers.The change to const interning is necessary, see https://rust-lang.zulipchat.com/#narrow/stream/146212-t-compiler.2Fconst-eval/topic/unsupported.20untyped.20pointer.20in.20constant.20error/near/399446285