Skip to content

Add some methods and optimizations in buffer vec#22470

Merged
alice-i-cecile merged 1 commit intobevyengine:mainfrom
beicause:buffer-vec-methods
Jan 11, 2026
Merged

Add some methods and optimizations in buffer vec#22470
alice-i-cecile merged 1 commit intobevyengine:mainfrom
beicause:buffer-vec-methods

Conversation

@beicause
Copy link
Copy Markdown
Member

Objective

UninitBufferVec is missing methods to get its capacity and set label. And BufferVec needs reserve_internal to reduce memory reallocation if element count is known.

Also there are two places that we can use Vec::reserve to preallocate memory.

Solution

Add reserve_internal to BufferVec
Add capacity set_label get_label to UninitBufferVec
Use Vec::reserve to reduce some allocation

Testing

CI

Add `reserve_internal` to `BufferVec`
Add `capacity` `set_label` `get_label` to `UninitBufferVec`
Use `Vec::reserve` to reduce some allocation
/// Preallocates space for `count` elements in the internal CPU-side buffer.
///
/// Unlike [`Self::reserve`], this doesn't have any effect on the GPU buffer.
pub fn reserve_internal(&mut self, count: usize) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Any reason why you don't call this inside .reserve()? It seems to me like it would make sense to do that?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

RawBufferVec::reserve_internal has use cases where the creation/writing of GPU buffers is deferred.

let element_size = u64::from(T::min_size()) as usize;
let offset = self.data.len();

// `extend` does not optimize for reallocation. Related `trusted_len` feature is unstable.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't understand the part about trusted_len

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Vec.extend reserves space internally if iterator impls TrustedLen trait (RepeatN does), though I haven't tested this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Did not know that, it's like a stronger size_hint iiuc.

@kfc35 kfc35 added A-Rendering Drawing game state to the screen D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jan 11, 2026

// `extend` does not optimize for reallocation. Related `trusted_len` feature is unstable.
self.data.reserve(self.data.len() + element_size);
// TODO: Consider using unsafe code to push uninitialized, to prevent
Copy link
Copy Markdown
Contributor

@goodartistscopy goodartistscopy Jan 11, 2026

Choose a reason for hiding this comment

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

My suggestion (after #22361) would be to reword the the following TODO comment:

// We can't optimize and push uninitialized data here because write_into() does not initialize any T's inner paddings

@alice-i-cecile alice-i-cecile added the X-Uncontroversial This work is generally agreed upon label Jan 11, 2026
@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jan 11, 2026
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jan 11, 2026
Merged via the queue into bevyengine:main with commit 503655b Jan 11, 2026
49 checks passed
@beicause beicause deleted the buffer-vec-methods branch January 12, 2026 03:21
github-merge-queue Bot pushed a commit that referenced this pull request Mar 29, 2026
# Objective

In #22470 (comment)
I thought `Vec::extend(repeat_n( ... ))` doesn't have `TrustedLen`
optimization but I was wrong! Rust std is allowed to use unstable
features internally. See https://godbolt.org/z/x4sGj9on9 the generated
MIR uses `Vec::extend_trusted`.

## Solution

Remove the unnecessary `reserve`

## Testing

None
splo pushed a commit to splo/bevy that referenced this pull request Mar 31, 2026
# Objective

In bevyengine#22470 (comment)
I thought `Vec::extend(repeat_n( ... ))` doesn't have `TrustedLen`
optimization but I was wrong! Rust std is allowed to use unstable
features internally. See https://godbolt.org/z/x4sGj9on9 the generated
MIR uses `Vec::extend_trusted`.

## Solution

Remove the unnecessary `reserve`

## Testing

None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Rendering Drawing game state to the screen D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it X-Uncontroversial This work is generally agreed upon

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants