-
Notifications
You must be signed in to change notification settings - Fork 76
Add optimized grow()/shrink() functions to Allocator implementation for pool #191
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
base: main
Are you sure you want to change the base?
Conversation
423bf5f
to
91d7f67
Compare
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.
Pull Request Overview
This PR implements optimized grow()
and shrink()
functions for the Pool allocator that can avoid real allocations when resizing the last allocation in the pool by simply adjusting the pool's last pointer.
- Adds
grow()
method that can expand the last allocation in-place if there's sufficient space - Adds
shrink()
method that can contract the last allocation in-place by moving the pool's last pointer backward - Falls back to allocate-copy-deallocate pattern when in-place resizing isn't possible
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
src/core/pool.rs
Outdated
old_layout.size(), | ||
); | ||
self.deallocate(ptr, old_layout); | ||
} |
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.
Redundant unsafe
block: the entire function is already declared as unsafe
, so this inner unsafe
block is unnecessary.
} | |
ptr::copy_nonoverlapping( | |
ptr.as_ptr(), | |
new_ptr.as_ptr().cast(), | |
old_layout.size(), | |
); | |
self.deallocate(ptr, old_layout); |
Copilot uses AI. Check for mistakes.
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.
The right fix is to upgrade the crate to edition 2024, where the code, as written, is required by rustc.
src/core/pool.rs
Outdated
old_layout.size(), | ||
); | ||
self.deallocate(ptr, old_layout); | ||
} |
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.
Redundant unsafe
block: the entire function is already declared as unsafe
, so this inner unsafe
block is unnecessary.
} | |
ptr::copy_nonoverlapping( | |
ptr.as_ptr(), | |
new_ptr.as_ptr().cast(), | |
old_layout.size(), | |
); | |
self.deallocate(ptr, old_layout); |
Copilot uses AI. Check for mistakes.
91d7f67
to
c6e44e6
Compare
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.
Some minor nitpicks, for the commit log
Making the commit log shorter and adding a new line for the body.
`feat: Add optimized grow()/shrink() functions for Pool
If resizing is requested for the last allocation in the pool, it may be
possible to adjust pool data and avoid any real allocations.`
?
Otherwise looks good.
c6e44e6
to
433e6e0
Compare
|
5009a7a
to
22992a9
Compare
If resizing is requested for the last allocation in the pool, it may be possible to adjust pool data and avoid any real allocations.
22992a9
to
5bf391f
Compare
ptr::write_bytes( | ||
new_ptr.as_ptr().cast::<u8>().byte_add(new_layout.size()), | ||
0, | ||
old_layout.size() - new_layout.size(), |
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.
if new_layout.size() >= old_layout.size()
old_layout.size() - new_layout.size()
will be negative?
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.
ptr::write_bytes(
new_ptr.as_ptr().cast::<u8>().add(old_layout.size()), // Start at end of old data
0,
new_layout.size() - old_layout.size(), // Zero the NEW bytes
)
Proposed changes
If resizing is requested for the last allocation in the pool, it may be possible to adjust pool data and avoid any real allocations.
Checklist
Before creating a PR, run through this checklist and mark each as complete.