rust: add secondary function with preallocated internal vecs#8936
rust: add secondary function with preallocated internal vecs#8936wlkrm wants to merge 1 commit intogoogle:masterfrom
Conversation
|
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. |
|
@jtdavis777, here is my PR as discussed in the original issue #8912 |
|
Awesome. I'll take a look as I have time and energy 😅 |
|
Yes that would be great! Thanks |
|
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 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. |
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. |
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
FlatBufferBuilderare 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 theFlatBufferBuilder::with_capacity(usize)function.Current Behavior
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: