Skip to content

Commit

Permalink
deps: update umio to mio v1
Browse files Browse the repository at this point in the history
  • Loading branch information
da2ce7 committed Aug 24, 2024
1 parent e952875 commit f889ad6
Show file tree
Hide file tree
Showing 35 changed files with 1,380 additions and 588 deletions.
4 changes: 4 additions & 0 deletions cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
"codegen",
"compat",
"concated",
"Condvar",
"coppersurfer",
"cpupool",
"curr",
"cust",
"cvar",
"Cyberneering",
"demonii",
"Deque",
Expand All @@ -51,6 +53,7 @@
"metainfo",
"mpmc",
"myapp",
"nanos",
"natted",
"nextest",
"Oneshot",
Expand All @@ -65,6 +68,7 @@
"rebootstrapping",
"recvd",
"reqq",
"reregister",
"ringbuffer",
"rpath",
"rqst",
Expand Down
6 changes: 5 additions & 1 deletion contrib/umio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ repository.workspace = true
version.workspace = true

[dependencies]
mio = "0.5"
mio = { version = "1", features = ["net", "os-poll"] }
tracing = "0"

[dev-dependencies]
tracing-subscriber = "0"
66 changes: 53 additions & 13 deletions contrib/umio/src/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,47 @@
use std::ops::{Deref, DerefMut};

use tracing::instrument;

#[allow(clippy::module_name_repetitions)]
pub struct BufferPool {
// Use Stack For Temporal Locality
buffers: Vec<Buffer>,
buffer_size: usize,
}

impl std::fmt::Debug for BufferPool {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BufferPool")
.field("buffers_len", &self.buffers.len())
.field("buffer_size", &self.buffer_size)
.finish()
}
}

impl BufferPool {
#[instrument(skip())]
pub fn new(buffer_size: usize) -> BufferPool {
let buffers = Vec::new();

BufferPool { buffers, buffer_size }
}

#[instrument(skip(self), fields(remaining= %self.buffers.len()))]
pub fn pop(&mut self) -> Buffer {
self.buffers.pop().unwrap_or(Buffer::new(self.buffer_size))
if let Some(buffer) = self.buffers.pop() {
tracing::trace!(?buffer, "popping old buffer taken from pool");
buffer
} else {
let buffer = Buffer::new(self.buffer_size);
tracing::trace!(?buffer, "creating new buffer...");
buffer
}
}

#[instrument(skip(self, buffer), fields(existing= %self.buffers.len()))]
pub fn push(&mut self, mut buffer: Buffer) {
tracing::trace!("Pushing buffer back to pool");
buffer.reset_position();

self.buffers.push(buffer);
}
}
Expand All @@ -27,42 +50,58 @@ impl BufferPool {

/// Reusable region of memory for incoming and outgoing messages.
pub struct Buffer {
buffer: Vec<u8>,
bytes_written: usize,
buffer: std::io::Cursor<Vec<u8>>,
}

impl std::fmt::Debug for Buffer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Buffer").field("buffer", &self.as_ref()).finish()
}
}

impl Buffer {
#[instrument(skip())]
fn new(len: usize) -> Buffer {
Buffer {
buffer: vec![0u8; len],
bytes_written: 0,
buffer: std::io::Cursor::new(vec![0_u8; len]),
}
}

fn reset_position(&mut self) {
self.set_written(0);
self.set_position(0);
}
}

impl Deref for Buffer {
type Target = std::io::Cursor<Vec<u8>>;

fn deref(&self) -> &Self::Target {
&self.buffer
}
}

/// Update the number of bytes written to the buffer.
pub fn set_written(&mut self, bytes: usize) {
self.bytes_written = bytes;
impl DerefMut for Buffer {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.buffer
}
}

impl AsRef<[u8]> for Buffer {
fn as_ref(&self) -> &[u8] {
&self.buffer[..self.bytes_written]
self.get_ref().split_at(self.buffer.position().try_into().unwrap()).0
}
}

impl AsMut<[u8]> for Buffer {
fn as_mut(&mut self) -> &mut [u8] {
&mut self.buffer[self.bytes_written..]
let pos = self.buffer.position().try_into().unwrap();
self.get_mut().split_at_mut(pos).1
}
}

#[cfg(test)]
mod tests {

use super::{Buffer, BufferPool};

const DEFAULT_BUFFER_SIZE: usize = 1500;
Expand All @@ -79,7 +118,8 @@ mod tests {
#[test]
fn positive_buffer_len_update() {
let mut buffer = Buffer::new(DEFAULT_BUFFER_SIZE);
buffer.set_written(DEFAULT_BUFFER_SIZE - 1);

buffer.set_position((DEFAULT_BUFFER_SIZE - 1).try_into().unwrap());

assert_eq!(buffer.as_mut().len(), 1);
assert_eq!(buffer.as_ref().len(), DEFAULT_BUFFER_SIZE - 1);
Expand Down
Loading

0 comments on commit f889ad6

Please sign in to comment.