-
Notifications
You must be signed in to change notification settings - Fork 1
Optimize async future memory usage with bump allocator for embedded systems #15
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
Draft
Copilot
wants to merge
8
commits into
master
Choose a base branch
from
copilot/fix-14
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+784
−3
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f6cdc54
Initial plan
Copilot d9fd95f
Add debug example to measure future sizes and confirm the issue
Copilot 4fec86e
Optimize async future memory usage by boxing large futures
Copilot f201abc
Implement bump allocator to eliminate heap allocations in embedded en…
Copilot b8026bb
Clean up accidentally committed build artifact
Copilot 6aabfa2
Remove Box::pin usage, make bump allocator generic, implement WiFi bu…
Copilot 1d2075f
Complete Thread stack bump allocator implementation
Copilot 89ae133
Fix the compilation errors
ivmarkov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/.vscode | ||
/target | ||
/Cargo.lock | ||
*.rlib |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
//! A simple bump allocator for fixed-size memory chunks | ||
//! | ||
//! This allocator is designed for allocating objects that have a known maximum lifetime. | ||
//! All allocations are freed when the allocator is dropped. | ||
|
||
use core::alloc::Layout; | ||
use core::cell::RefCell; | ||
use core::mem::MaybeUninit; | ||
use core::pin::Pin; | ||
use core::ptr::NonNull; | ||
|
||
struct Inner<'a> { | ||
memory: &'a mut [MaybeUninit<u8>], | ||
offset: usize, | ||
} | ||
|
||
/// A bump allocator that uses a provided memory chunk | ||
pub struct BumpAlloc<'a>(RefCell<Inner<'a>>); | ||
|
||
impl<'a> BumpAlloc<'a> { | ||
/// Create a new bump allocator with the provided memory chunk | ||
pub const fn new(memory: &'a mut [MaybeUninit<u8>]) -> Self { | ||
Self(RefCell::new(Inner { memory, offset: 0 })) | ||
} | ||
|
||
/// Allocate memory for an object and pin it | ||
pub fn box_pin<T>(&self, object: T) -> Result<Pin<BumpBox<'_, T>>, AllocError> | ||
where | ||
T: Sized, | ||
{ | ||
let layout = Layout::new::<T>(); | ||
let ptr = self.alloc_raw(layout)?; | ||
|
||
// Safety: We just allocated the memory and it's properly aligned | ||
unsafe { | ||
let ptr = ptr.as_ptr() as *mut T; | ||
ptr.write(object); | ||
Ok(Pin::new_unchecked(BumpBox { | ||
ptr: NonNull::new_unchecked(ptr), | ||
_allocator: core::marker::PhantomData, | ||
})) | ||
} | ||
} | ||
|
||
fn alloc_raw(&self, layout: Layout) -> Result<NonNull<u8>, AllocError> { | ||
let size = layout.size(); | ||
let align = layout.align(); | ||
|
||
let mut inner = self.0.borrow_mut(); | ||
|
||
let current_offset = inner.offset; | ||
|
||
// Align the offset | ||
let aligned_offset = (current_offset + align - 1) & !(align - 1); | ||
let new_offset = aligned_offset + size; | ||
|
||
if new_offset > inner.memory.len() { | ||
return Err(AllocError); | ||
} | ||
|
||
inner.offset = new_offset; | ||
|
||
// Safety: We checked bounds and alignment | ||
let ptr = unsafe { inner.memory.as_mut_ptr().add(aligned_offset) as *mut u8 }; | ||
|
||
Ok(unsafe { NonNull::new_unchecked(ptr) }) | ||
} | ||
|
||
/// Get the current memory usage | ||
pub fn used(&self) -> usize { | ||
self.0.borrow().offset | ||
} | ||
|
||
/// Get the total capacity | ||
pub fn capacity(&self) -> usize { | ||
self.0.borrow().memory.len() | ||
} | ||
} | ||
|
||
/// A box-like container that uses bump allocation | ||
pub struct BumpBox<'a, T> { | ||
ptr: NonNull<T>, | ||
_allocator: core::marker::PhantomData<&'a ()>, | ||
} | ||
|
||
impl<T> core::ops::Deref for BumpBox<'_, T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
unsafe { self.ptr.as_ref() } | ||
} | ||
} | ||
|
||
impl<T> core::ops::DerefMut for BumpBox<'_, T> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
unsafe { self.ptr.as_mut() } | ||
} | ||
} | ||
|
||
impl<T: core::future::Future> core::future::Future for BumpBox<'_, T> { | ||
type Output = T::Output; | ||
|
||
fn poll( | ||
self: Pin<&mut Self>, | ||
cx: &mut core::task::Context<'_>, | ||
) -> core::task::Poll<Self::Output> { | ||
// Safety: We maintain the pin invariant | ||
let future = unsafe { self.map_unchecked_mut(|s| &mut **s) }; | ||
future.poll(cx) | ||
} | ||
} | ||
|
||
impl<T> Drop for BumpBox<'_, T> { | ||
fn drop(&mut self) { | ||
// Safety: The pointer is valid and we own the data | ||
unsafe { | ||
self.ptr.as_ptr().drop_in_place(); | ||
} | ||
} | ||
} | ||
|
||
/// Error type for allocation failures | ||
#[derive(Debug)] | ||
pub struct AllocError; | ||
|
||
impl core::fmt::Display for AllocError { | ||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
write!(f, "Bump allocator out of memory") | ||
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
impl std::error::Error for AllocError {} |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
We might need this indeed or we might not. Can you explain why you need to implement the
Future
trait on theBumpBox
?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
Future
trait implementation onBumpBox
is needed because the allocated objects are futures that need to be polled byselect3()
and similar combinators. Without this impl, the async runtime couldn't poll the bump-allocated futures.When we do
allocator.alloc_pin(some_future)
, the result needs to be usable wherever aFuture
is expected.