-
Notifications
You must be signed in to change notification settings - Fork 34
perf: Add new efficient APIs read_unsafe and read_to_vec #248
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
Merged
Conversation
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
I found that a source of significant performance loss is the read method of Memory. The read method takes a mutable buffer which it fills with values read from the stable memory. According to Rust rules, the buffer passed to read must be initialized before it's passed to read (buffers containing uninitialized values are unsound and can cause UB). The usual pattern is to create a properly sized Vec, eg. by using `vec![0; size]` or `vec.resize(size, 0)` and pass that to `read`. However, initializing the bytes with values that get overwritten by `read` is only necessary in order to be sound and requires significant number of instructions. This PR introduces a new method `read_unsafe` which allows passing in a raw pointer and a `count` parameter. Implementations can be more efficient by reading directly and skipping initialization. This can lead to instruction reductions of up to 40%. The PR also introduces a helper method `read_to_vec` which is a safe wrapper around `read_unsafe` for the most common use-case: reading into a `Vec`. Clients can for example pass an empty `Vec` and profit from the extra efficiency without having to call unsafe methods.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, LGTM modulo a few comments. Should we also consider adding some tests for read_to_vec?
The previous implementation wasn't fully safe according to Rust standards because - T was initialized with zeros which may be illegal depending on T - t and t_slice were aliasing the same mutable memory
…nkdavid/unsafe_read
berestovskyy
previously approved these changes
Nov 22, 2024
berestovskyy
approved these changes
Nov 22, 2024
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.
I found that a source of significant performance loss is the read method of
Memory. Thereadmethod takes a mutable buffer which it fills with values read from the stable memory. According to Rust rules, the buffer passed to read must be initialized before it's passed to read (buffers containing uninitialized values are unsound and can cause UB).The usual pattern is to create a properly sized Vec, eg. by using
vec![0; size]orvec.resize(size, 0)and pass that toread. However, initializing the bytes with values that get overwritten byreadis only necessary in order to be sound and requires significant number of instructions.This PR introduces a new method
read_unsafewhich allows passing in a raw pointer and acountparameter. Implementations can be more efficient by reading directly and skipping initialization. This can lead to instruction reductions of up to 40%.The PR also introduces a helper method
read_to_vecwhich is a safe wrapper aroundread_unsafefor the most common use-case: reading into aVec. Clients can for example pass an emptyVecand profit from the extra efficiency without having to call unsafe methods.