Skip to content

Commit

Permalink
memory: reuse freed nodes in WriteLog
Browse files Browse the repository at this point in the history
Once a node is freed it can be used in next allocation.  Change WriteLog
such that it tries to pick freed nodes first before calling out to the
allocator when customer tries to allocate a new node.
  • Loading branch information
mina86 committed Oct 22, 2023
1 parent 229adb8 commit de1cfae
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions common/memory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,14 @@ impl<'a, A: Allocator> WriteLog<'a, A> {
pub fn allocator(&self) -> &A { &*self.alloc }

pub fn alloc(&mut self, value: A::Value) -> Result<Ptr, OutOfMemory> {
let ptr = self.alloc.alloc(value)?;
self.allocated.push(ptr);
Ok(ptr)
Ok(if let Some(ptr) = self.freed.pop() {
self.set(ptr, value);
ptr
} else {
let ptr = self.alloc.alloc(value)?;
self.allocated.push(ptr);
ptr
})
}

pub fn set(&mut self, ptr: Ptr, value: A::Value) {
Expand Down

0 comments on commit de1cfae

Please sign in to comment.