Add integration with zerocopy crate - #253
Open
sivadeilra wants to merge 2 commits into
Open
Conversation
The zerocopy crate provides traits and functions for _safely_ transmuting types to/from bytes. It is an important building block for high-performance serialization in many designs. It is also a very well-maintained crate with high standards for quality and its maintainers are actively engaged with the Rust Project on advancing the goals of the Safe Transmute working group. The `zerocopy` crate defines the `FromZeroes` trait, which specifies that a type can be safely constructed from a buffer containing an all-zeroes bit pattern. This PR adds two new methods to `Bump`: `new_zeroed` and `new_slice_zeroed`. `new_zeroed` allocates space for `T` (where `T: FromZeroes`) and returns `&mut T`. This avoids the "placement new" problem in Rust, which causes problems when attempting to allocate large types in the heap, such as `[u8; 0x10000]`. For most containers, such as `Box`, the type is briefly constructed in a temporary on the stack, then a heap allocation is done, then the value is moved into the heap allocation. If `T` is large enough, it can cause stack overflow. The `Bump::new_zeroed` function avoids this problem by simply allocating space for `T` directly in the heap, then filling it with zeroes, then casting the allocation as `&mut T` and returning it. The `FromZeroes` constraint ensures that this is sound. The `new_slice_zeroed` function similarly allows allocating slices directly in a Bump.
fitzgen
reviewed
Jun 10, 2024
fitzgen
left a comment
Owner
There was a problem hiding this comment.
Happy to take a PR that adds a zeroing allocation method, but I don't want to take a dependency on any new crates.
Author
It's an optional dependency, though, and the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The zerocopy crate provides traits and functions for safely transmuting types to/from bytes. It is an important building block for high-performance serialization in many designs. It is also a very well-maintained crate with high standards for quality and its maintainers are actively engaged with the Rust Project on advancing the goals of the Safe Transmute working group.
The
zerocopycrate defines theFromZeroestrait, which specifies that a type can be safely constructed from a buffer containing an all-zeroes bit pattern.This PR adds two new methods to
Bump:new_zeroedandnew_slice_zeroed.new_zeroedallocates space forT(whereT: FromZeroes) and returns&mut T. This avoids the "placement new" problem in Rust, which causes problems when attempting to allocate large types in the heap, such as[u8; 0x10000]. For most containers, such asBox, the type is briefly constructed in a temporary on the stack, then a heap allocation is done, then the value is moved into the heap allocation. IfTis large enough, it can cause stack overflow.The
Bump::new_zeroedfunction avoids this problem by simply allocating space forTdirectly in the heap, then filling it with zeroes, then casting the allocation as&mut Tand returning it. TheFromZeroesconstraint ensures that this is sound.The
new_slice_zeroedfunction similarly allows allocating slices directly in a Bump.