Skip to content
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

Use a custom container for Cache's cache #689

Merged
merged 4 commits into from
Jan 4, 2025

Conversation

workingjubilee
Copy link
Member

The new Lru type, as a single-purpose "ArrayVec",

  • removes a level of indirection
  • omits a needless Vec allocation entirely
  • internalizes invariants we attempted to maintain in open-coded form

It costs a little unsafe to handle Drop, which we unfortunately need as addr2line uses Arc inside Context.

The new Lru type, as a single-purpose "ArrayVec",
- removes a level of indirection
- omits a needless Vec allocation entirely
- internalizes invariants we attempted to maintain in open-coded form

It costs a little `unsafe` to handle Drop, which we unfortunately need
as addr2line uses Arc inside Context.
Copy link

github-actions bot commented Jan 4, 2025

Code size changes for a hello-world Rust program linked with libstd with backtrace:

On platform ubuntu-latest:

  • Original binary size: 383,480 B
  • Updated binary size: 373,512 B
  • Difference: -9,968 B (-2.6%)

Copy link
Member Author

@workingjubilee workingjubilee left a comment

Choose a reason for hiding this comment

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

hell yeah!

fn default() -> Self {
Lru {
len: 0,
arr: [const { MaybeUninit::uninit() }; N],
Copy link
Member Author

Choose a reason for hiding this comment

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

this requires an MSRV bump, or to use the infamously sus-looking-but-actually-okay-for-this-ONE-instance:

Suggested change
arr: [const { MaybeUninit::uninit() }; N],
arr: unsafe { MaybeUninit::uninit().assume_init() },

Copy link
Member

Choose a reason for hiding this comment

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

On the one hand I'm not against bumping the MSRV for this crate but on the other hand it is only one line. But on the third hand it is a very sussy line that would at least need a comment saying "no really, this is ok!"

What would the new MSRV be?

Copy link
Member Author

Choose a reason for hiding this comment

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

if 318dd91 passes then the answer is 1.79

Copy link
Member

Choose a reason for hiding this comment

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

Ah 1.73 to 1.79? That seems fine to me. We should probably also update the rust-version in Cargo.toml as that seems to have gone out of the sink.

Copy link

github-actions bot commented Jan 4, 2025

Code size changes for a hello-world Rust program linked with libstd with backtrace:

On platform ubuntu-latest:

  • Original binary size: 383,480 B
  • Updated binary size: 373,512 B
  • Difference: -9,968 B (-2.6%)

Copy link

github-actions bot commented Jan 4, 2025

Code size changes for a hello-world Rust program linked with libstd with backtrace:

On platform ubuntu-latest:

  • Original binary size: 383,480 B
  • Updated binary size: 373,512 B
  • Difference: -9,968 B (-2.6%)

let len = self.len;
self.len = 0;
// SAFETY: we can't touch these values again due to setting self.len = 0
unsafe { ptr::drop_in_place(ptr::addr_of_mut!(self.arr[0..len]) as *mut [T]) }
Copy link
Member

Choose a reason for hiding this comment

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

cast still requires sized, shame.

let len_to_init = self.len + 1;
let mut last = MaybeUninit::new(value);
for elem in self.arr[0..len_to_init].iter_mut() {
last = mem::replace(elem, last);
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this just mem::swap?

Copy link
Member Author

@workingjubilee workingjubilee Jan 4, 2025

Choose a reason for hiding this comment

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

what we do is simultaneously assign the last to an index, and take out the old value at that index. eventually everything gets moved over one index by this process. it isn't really mem::swap because we're instead moving everything a step over, and that isn't really expressible that way (and there's no windows_mut, besides).

Copy link
Member

Choose a reason for hiding this comment

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

Maybe I'm having a brain fart but I mean literally last = mem::replace(elem, last); looks suspiciously like mem::swap(elem, &mut last); no?

Copy link
Member Author

Choose a reason for hiding this comment

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

oh.

Copy link
Member Author

Choose a reason for hiding this comment

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

yes.

Copy link
Member

Choose a reason for hiding this comment

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

Hm, that had a big effect on code size???

Copy link

github-actions bot commented Jan 4, 2025

Code size changes for a hello-world Rust program linked with libstd with backtrace:

On platform ubuntu-latest:

  • Original binary size: 383,480 B
  • Updated binary size: 385,800 B
  • Difference: 2,320 B (0.6%)

@workingjubilee
Copy link
Member Author

...
@ChrisDenton apparently it optimizes worse?

@ChrisDenton
Copy link
Member

Yeah ok, no idea what that's about but revering back to replace makes sense. Maybe with a comment saying why.

Copy link

github-actions bot commented Jan 4, 2025

Code size changes for a hello-world Rust program linked with libstd with backtrace:

On platform ubuntu-latest:

  • Original binary size: 383,480 B
  • Updated binary size: 373,512 B
  • Difference: -9,968 B (-2.6%)

Copy link
Member

@ChrisDenton ChrisDenton left a comment

Choose a reason for hiding this comment

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

This is such a nice improvement, thanks!

@ChrisDenton ChrisDenton merged commit d7c5a45 into rust-lang:master Jan 4, 2025
41 checks passed
@workingjubilee workingjubilee deleted the formalize-lru branch January 4, 2025 02:17
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.

2 participants