-
Notifications
You must be signed in to change notification settings - Fork 50
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
Reduce number of allocations #8
Comments
It would be nice to add support for rust allocators api. As this implementation works more like state machine, it could benefit a lot from bump allocators which could be reset each polling iteration |
@VictorBulba Cool! I never used those, but sounds interesting! |
I was thinking some more about this, for both async and sync APIs it's typically to use In sans-IO this is not as trivial. In the case of a write the caller needs to pass owned data so progress on writing can be made at a later stage. In str0m this manifests as The bump allocator idea is interesting, but it could only be used for temporary allocations during each |
Str0m could use some circular buffer which is a contiguous chunk of memory, and allows append bytes to the end, and consuming then from the start. Something like: // BytesBuffer is a special circular buffer for str0m
let buf = BytesBuffer::with_capacity(1024);
// It can be written to
buf.write(&[0x01, 0x02, 0x03, 0x04, 0x05, 0x06]);
// Reading updates the circular buffer's offset,
// so consumed memory can be resused
{
// Read guard keeps mutable reference to the buffer,
// so it can't be written to while the guard is alive
//
// fn consumed_read<'a>(&'a mut self, len: usize) -> ReadGuard<'a>
let read_guard = buf.consumed_read(3);
assert_eq!(read_guard.get_bytes(), &[0x01, 0x02, 0x03]);
drop(read_guard);
}
// Previous read memory has been consumed
assert_eq!(buf.consumed_read(2).get_bytes(), &[0x04, 0x05]); str0m could use multiple of such buffers for different purposes. When user does |
Parsing a Keeping things within an |
Good point! I favor |
I submitted a PR for for that: #425 |
The impls has a few
Vec<u8>
arguments. It would be good to look through the instances of these to see where we can re-use buffers (or use ring buffers), and where we can use lifetimes to avoid internal allocation.Zero allocations is a non-goal, but we can do better than we currently do.
The text was updated successfully, but these errors were encountered: