-
-
Notifications
You must be signed in to change notification settings - Fork 562
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rust): added global buffer allocator to reduce memory fragmentation
- Loading branch information
1 parent
6d3e50b
commit 6f87bf0
Showing
15 changed files
with
168 additions
and
27 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,70 @@ | ||
use crate::compat::sync::Mutex; | ||
use crate::compat::vec::Vec; | ||
use core::fmt::{Debug, Formatter}; | ||
use once_cell::sync::Lazy; | ||
|
||
const MIN_BUFFER_SIZE: usize = 96 * 1024; | ||
const MAX_BUFFER_SIZE: usize = 192 * 1024; | ||
const MAX_BUFFERS: usize = 32; | ||
|
||
/// The global instance of [`BufferPool`]. | ||
pub static GLOBAL_BUFFER_POOL: Lazy<BufferPool> = Lazy::new(BufferPool::new); | ||
|
||
/// A buffer pool for reusing buffers at least big as [`MIN_BUFFER_SIZE`]. | ||
pub struct BufferPool { | ||
buffers: Mutex<Inner>, | ||
} | ||
|
||
impl Debug for BufferPool { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { | ||
let len = self.buffers.lock().unwrap().buffers.len(); | ||
f.debug_struct("BufferPool").field("buffers", &len).finish() | ||
} | ||
} | ||
|
||
struct Inner { | ||
buffers: Vec<Vec<u8>>, | ||
} | ||
|
||
impl BufferPool { | ||
fn new() -> Self { | ||
Self { | ||
buffers: Mutex::new(Inner { | ||
buffers: Vec::new(), | ||
}), | ||
} | ||
} | ||
|
||
/// When the size is big enough, it'll return a buffer from the pool, | ||
/// otherwise it'll return a new buffer. | ||
pub fn try_size(&self, size: usize) -> Vec<u8> { | ||
if (MIN_BUFFER_SIZE..=MAX_BUFFER_SIZE).contains(&size) { | ||
self.take() | ||
} else { | ||
Vec::with_capacity(size) | ||
} | ||
} | ||
|
||
/// Take a buffer from the pool. | ||
pub fn take(&self) -> Vec<u8> { | ||
let mut buffers = self.buffers.lock().unwrap(); | ||
if let Some(mut buffer) = buffers.buffers.pop() { | ||
buffer.clear(); | ||
buffer | ||
} else { | ||
Vec::with_capacity(MIN_BUFFER_SIZE) | ||
} | ||
} | ||
|
||
/// Release a buffer back to the pool, the buffer will only be reused if | ||
/// it's capacity is within [`MIN_BUFFER_SIZE`] and [`MAX_BUFFER_SIZE`]. | ||
pub fn release(&self, buffer: Vec<u8>) { | ||
if buffer.capacity() >= MIN_BUFFER_SIZE && buffer.capacity() <= MAX_BUFFER_SIZE { | ||
let mut buffers = self.buffers.lock().unwrap(); | ||
if buffers.buffers.len() < MAX_BUFFERS { | ||
buffers.buffers.push(buffer); | ||
buffers.buffers.sort_by_key(|b| -(b.capacity() as i64)); | ||
} | ||
} | ||
} | ||
} |
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
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
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
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
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
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
File renamed without changes.
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
Oops, something went wrong.