-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
204 additions
and
252 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright: (c) 2024 wsm25 | ||
|
||
#ifndef WSM_POOL_HPP | ||
#define WSM_POOL_HPP | ||
#include <cstdlib> | ||
#include <cstdio> | ||
#include <new> | ||
|
||
/// @brief Faster vector for manually-drop types, especially built-in types. | ||
/// @tparam T MUST NOT have custom destructor | ||
template<typename T> | ||
class Vec{ | ||
T *from, *end, *cur; | ||
public: | ||
Vec(){ | ||
from=cur=(T*)malloc(sizeof(T)*4); | ||
end=cur+4; | ||
} | ||
~Vec(){free(from);} | ||
void push(T x){ | ||
if(cur==end){ | ||
size_t size=((size_t)end - (size_t)from), doubled=size*2; | ||
from=(T*)realloc(from, doubled); | ||
cur=(T*)((size_t)from + size); | ||
end=(T*)((size_t)from + doubled); | ||
} | ||
*(cur++)=x; // UB if T has custom destructor | ||
} | ||
// SAFETY: must check empty | ||
T pop(){return *(--cur);} | ||
bool empty(){return cur==from;} | ||
// slow | ||
size_t len(){return cur-from;} | ||
}; | ||
|
||
/* | ||
* Pool: allocate in exponentially growing batch, reducing pressure | ||
* on allocator. | ||
* | ||
* ## Usage | ||
```cpp | ||
Pool<int> pool; | ||
// the same effect as ptr=new int; | ||
int* ptr=pool.get(); | ||
// returns ptr to pool, similar to free(ptr) | ||
pool.put(ptr); | ||
``` | ||
* | ||
* For performance consideration, especially for allowing uninitialized | ||
* types and better optimization for built-in types, we will not support | ||
* classes with custom destructors. If you would use that, please call | ||
* placement constructor when getting from pool, and call destructor when | ||
* dropping(put into pool/simply discard) | ||
* | ||
* Benchmark result (in average, O1 optimization, Linux): | ||
* - Pool: <1 ns per get/put | ||
* - Stdlib: 25 ns per malloc, 7 ns per free | ||
*/ | ||
template<typename T> | ||
class Pool{ | ||
class Buf{ | ||
T *from, *end, *cur; | ||
public: | ||
Buf(size_t cap){ | ||
from=cur=(T*)malloc(cap*sizeof(T)); | ||
end=from+cap; | ||
} | ||
bool full(){return cur>=end;} | ||
// UNSAFE: assume full==false | ||
T* get(){return (cur++);} | ||
size_t cap(){return end-from;} | ||
T* raw(){return from;} | ||
}; | ||
|
||
Buf buf; | ||
Vec<T*> used; // bufs | ||
Vec<T*> idle; | ||
public: | ||
Pool():buf(Buf(4)){} | ||
~Pool(){ | ||
while(!used.empty()) free(used.pop()); | ||
free(buf.raw()); | ||
} | ||
T* get(){ | ||
if(idle.empty()){ | ||
if(buf.full()){ | ||
used.push(buf.raw()); | ||
new(&buf) Buf(buf.cap()*2); | ||
} | ||
return buf.get(); | ||
} else return idle.pop(); | ||
} | ||
void put(T* p){idle.push(p);} | ||
}; | ||
#endif |
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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
//! A simple single-thread object pool. As size for single | ||
//! object is fixed, chunks and bins in malloc are too heavy. | ||
//! | ||
//! maintains a virtual memory mapping, enableing | ||
//! - use of memory fragments | ||
//! - prefer lower address | ||
//! - can automatically dealloc high address | ||
|
||
pub struct Heap<T>{ | ||
raw: *mut T, | ||
top: *mut T, | ||
cap: usize, | ||
idle: BinaryHeap<NonNull<T>>, | ||
} | ||
|
||
impl<T> Heap<T>{ | ||
pub fn new(cap: usize)->Self{ | ||
let raw=unsafe{crate::mem::new_arr(cap)}; | ||
Self { raw , top: raw, cap , idle:BinaryHeap::new()} | ||
} | ||
pub fn get(&mut self)->Option<NonNull<T>>{ | ||
match self.idle.is_empty(){ | ||
false=>self.idle.pop(), | ||
true=>{ | ||
if (self.top as usize- self.raw as usize)<self.cap{ | ||
let ptr=Some(unsafe{NonNull::new_unchecked(self.top)}); | ||
self.top=unsafe{self.top.add(1)}; | ||
ptr | ||
} else {None} | ||
} | ||
} | ||
} | ||
// UNSAFE: ptr MUST belong to this heap | ||
pub fn put(&mut self, ptr: NonNull<T>){ | ||
if ptr.as_ptr() == unsafe{self.top.sub(1)}{ | ||
self.top=unsafe{self.top.sub(1)}; | ||
while let Some(ptr)=self.idle.peek(){ | ||
if ptr.as_ptr() != unsafe{self.top.sub(1)} {break;} | ||
self.top=unsafe{self.top.sub(1)}; | ||
self.idle.pop(); | ||
} | ||
} else { | ||
self.idle.push(ptr); | ||
} | ||
} | ||
} | ||
|
||
impl<T> Drop for Heap<T>{ | ||
fn drop(&mut self) { | ||
unsafe{crate::mem::delete_arr(self.raw, self.cap)}; | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
#[test] | ||
fn _test_init(){ | ||
use super::*; | ||
let mut counter=1; | ||
let mut p = Heap::<i32>::new(10); | ||
let g1=p.get().unwrap(); | ||
let g2=p.get().unwrap(); | ||
println!("{}",*unsafe{g1.as_ref()}); | ||
println!("{}",*unsafe{g2.as_ref()}); | ||
drop(p); | ||
} | ||
|
||
/* | ||
// #[test] | ||
fn _test_tokio(){ | ||
use tokio::{ | ||
runtime::Builder, | ||
task::{LocalSet, spawn_local, yield_now}, | ||
}; | ||
use super::*; | ||
async fn sleepygreeting(mut pool: Pool<i32>){ | ||
for _ in 0..5{ | ||
let x=pool.get(); | ||
if true==rand::random(){ | ||
yield_now().await; | ||
} | ||
println!("Get {} from pool!", *x); | ||
} | ||
} | ||
async fn tokio_main(){ | ||
let mut ipool=0; | ||
let pool = Pool::with_generator(move||{ipool+=1; ipool}); | ||
let mut tasks = Vec::new(); | ||
for _ in 0..5{ | ||
tasks.push(spawn_local( | ||
sleepygreeting(pool.clone()) | ||
)); | ||
} | ||
for t in tasks{ | ||
let _ = t.await; | ||
} | ||
} | ||
Builder::new_current_thread().enable_time().build().unwrap().block_on( | ||
LocalSet::new().run_until(tokio_main()) | ||
); | ||
} | ||
*/ | ||
} | ||
|
||
use std::{cell::UnsafeCell, collections::{BTreeMap, BinaryHeap, HashMap}, mem::ManuallyDrop, ptr::NonNull, rc::Rc}; | ||
|
||
use crate::mem; |
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,8 +1,7 @@ | ||
//! Rust toy libraries | ||
pub mod mem; | ||
pub mod localpool; | ||
#[deprecated="benchmark shows terrible performance"] | ||
pub mod thinpool; | ||
pub mod heappool; | ||
#[deprecated] | ||
pub mod locallock; | ||
pub mod rcnode; |
Oops, something went wrong.