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

Modifies RawVec reserve fn structure to improve inlining #239

Merged
merged 1 commit into from
Feb 22, 2024

Conversation

zslayton
Copy link
Contributor

@zslayton zslayton commented Feb 21, 2024

This PR reorganizes RawVec::reserve_internal into a family of methods that allow the compiler to make better inlining decisions.

fallible_reserve_internal is a wrapper around reserve_internal that performs the "do we already have enough space?" check:

if self.cap().wrapping_sub(used_cap) >= needed_extra_cap {
    return Ok(());
}

This portion of the code (which produces a very small amount of assembly) is always inlined. Only if this check fails (which is relatively rare) does it then call reserve_internal_or_error, which is not inlined.

The infallible_reserve_internal/reserve_internal_or_panic method pair is similar to the above, but also prevents the code needed to detect an error and panic accordingly from being inlined.

Collectively, this allows the compiler to inline Vec::push and Vec::extend* in more places by transitively shrinking the amount of assembly each produces.

You can see the impact in the benchmarks introduced in #235:

image

Copy link
Owner

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

Looks great, thanks!

@fitzgen fitzgen merged commit 1803cca into fitzgen:main Feb 22, 2024
8 checks passed
@zslayton zslayton deleted the raw-vec-inlining branch February 22, 2024 19: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.

2 participants