-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to a unified pool instead of per-thread one
- Loading branch information
Showing
2 changed files
with
37 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 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,39 +1,52 @@ | ||
use std::cell::RefCell; | ||
use core::fmt; | ||
use std::sync::{ | ||
Arc, | ||
Mutex, | ||
}; | ||
|
||
use fuel_core_types::fuel_vm::interpreter::Memory; | ||
|
||
thread_local! { | ||
static POOL: RefCell<Vec<Memory>> = RefCell::new(Vec::new()); | ||
#[derive(Default, Clone)] | ||
pub(crate) struct VmPool { | ||
pool: Arc<Mutex<Vec<Memory>>>, | ||
} | ||
|
||
/// Gets a new VM memory instance from the pool. | ||
pub(crate) fn get_vm_memory() -> Memory { | ||
POOL.with(|pool| { | ||
let mut pool = pool.borrow_mut(); | ||
impl VmPool { | ||
/// Gets a new VM memory instance from the pool. | ||
pub(crate) fn get_new(&self) -> Memory { | ||
let mut pool = self.pool.lock().expect("poisoned"); | ||
pool.pop().unwrap_or_else(Memory::new) | ||
}) | ||
} | ||
} | ||
|
||
/// Recycles a VM memory instance back into the pool. | ||
pub(crate) fn recycle_vm_memory(mut mem: Memory) { | ||
POOL.with(|pool| { | ||
/// Recycles a VM memory instance back into the pool. | ||
pub(crate) fn recycle(&self, mut mem: Memory) { | ||
mem.reset(); | ||
let mut pool = pool.borrow_mut(); | ||
let mut pool = self.pool.lock().expect("poisoned"); | ||
pool.push(mem); | ||
}) | ||
} | ||
} | ||
|
||
impl fmt::Debug for VmPool { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self.pool.lock() { | ||
Ok(pool) => write!(f, "VmPool {{ pool: [{} items] }}", pool.len()), | ||
Err(_) => write!(f, "VmPool {{ pool: [poisoned] }}"), | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_vm_pool() { | ||
let mut mem = get_vm_memory(); | ||
let pool = VmPool::default(); | ||
|
||
let mut mem = pool.get_new(); | ||
mem.grow_stack(1024).expect("Unable to grow stack"); | ||
mem.write_bytes_noownerchecks(0, [1, 2, 3, 4]) | ||
.expect("Unable to write stack"); | ||
let ptr1 = mem.stack_raw() as *const _ as *const u8 as usize; | ||
recycle_vm_memory(mem); | ||
pool.recycle(mem); | ||
|
||
// Make sure we get the same memory allocation back | ||
let mem = get_vm_memory(); | ||
let mem = pool.get_new(); | ||
let ptr2 = mem.stack_raw() as *const _ as *const u8 as usize; | ||
assert_eq!(ptr1, ptr2); | ||
} |