Skip to content

rust: add secondary function with preallocated internal vecs#8936

Open
wlkrm wants to merge 1 commit intogoogle:masterfrom
wlkrm:rust-preallocated-buffers
Open

rust: add secondary function with preallocated internal vecs#8936
wlkrm wants to merge 1 commit intogoogle:masterfrom
wlkrm:rust-preallocated-buffers

Conversation

@wlkrm
Copy link

@wlkrm wlkrm commented Feb 23, 2026

Because of its zero-copy properties, flatbuffers might be a good fit for real-time systems.
For real-time systems, memory allocations in critical sections, such as control loops, should be avoided.
Currently in the Rust implementation, I can only preallocate the "data" buffer used for serialization.
This only avoids some allocations at runtime: Some internal vecs of the FlatBufferBuilder are allocated on-demand, which will lead to additional allocations at runtime. It would be useful to be able to preallocate some user-definable space for the internal buffers optionally, as it is already possible for the "data" buffer using the FlatBufferBuilder::with_capacity(usize) function.

Current Behavior

// Schema
namespace users;

table User {
  name:string;
}

root_type User;
let mut buffer = FlatBufferBuilder::with_capacity(1024);
buffer.reset();

loop {
        let name = buffer.create_string("testt");
        let user_args = UserArgs { name: Some(name) };
        let test = User::create(&mut buffer, &user_args);
        buffer.finish(test, None);
        let _ = buffer.finished_data();
 }

This PR

My suggestion is to simply add a second initializer function besides with_capacity(), which also allocated internal capacity, e.g., FlatBufferBuilder::with_internal_capacity(1024 /* data buffer's len */, 16, 32, ... /* internal vecs' lengths */) (Of course with a different len for every vec in the implementation).
Of course, the user would have to make a sophisticated decision on how much to preallocate, but this is already the case for the "data" buffer size.

This PR implements the proposed feature and adds a test, which verfies, that the preallocation is indeed working.
The new API call looks like this:

let mut builder = FlatBufferBuilder::with_internal_capacity(64, 8, 16, 32);

@wlkrm wlkrm requested a review from dbaileychess as a code owner February 23, 2026 19:46
@google-cla
Copy link

google-cla bot commented Feb 23, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions bot added the rust label Feb 23, 2026
@wlkrm
Copy link
Author

wlkrm commented Feb 23, 2026

@jtdavis777, here is my PR as discussed in the original issue #8912

@jtdavis777
Copy link
Collaborator

Awesome. I'll take a look as I have time and energy 😅

@wlkrm
Copy link
Author

wlkrm commented Feb 25, 2026

Yes that would be great! Thanks

@jtdavis777
Copy link
Collaborator

hey @wlkrm, I do not know very much about rust, this does look good and like a good change. I would like to see if there's any good place in the documentation to describe this feature, either in readme's or in the docs that go up on the site.

@jtdavis777 jtdavis777 added the waiting-for-update This PR is waiting for a change from the author or contributors before it is ready for merge label Mar 5, 2026
@wlkrm
Copy link
Author

wlkrm commented Mar 5, 2026

Hey, thanks for reviewing. The docsstrings I added to the new functions will appear in the crates doc. So it will be automatically added here https://docs.rs/flatbuffers/latest/flatbuffers/struct.FlatBufferBuilder.html just like the with_capacity function right now.
What other places for docs do you have in mind?, as this is a feature specifically for the rust implementation, as other languages for example have a garbage collector.

@wlkrm
Copy link
Author

wlkrm commented Mar 5, 2026

hey @wlkrm, I do not know very much about rust, this does look good and like a good change. I would like to see if there's any good place in the documentation to describe this feature, either in readme's or in the docs that go up on the site.

Hey thanks for the review, I have already added docsstrings for the new functions which go up with the rust documentation site of the FlatbufferBuilder struct on the official doc site.
https://docs.rs/flatbuffers/latest/flatbuffers/struct.FlatBufferBuilder.html

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

Labels

rust waiting-for-update This PR is waiting for a change from the author or contributors before it is ready for merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants