Skip to content

Commit

Permalink
Add with capacity & increase default tp buf capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianSchmid committed Nov 13, 2023
1 parent 9bdb969 commit f1f164e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/tp_buf_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ mod test {
#[test]
fn default() {
let actual: TpBufConfig = Default::default();
assert_eq!(0x4000, actual.tp_buffer_start_payload_alloc_len);
assert_eq!(0x40000, actual.tp_buffer_start_payload_alloc_len);
assert_eq!(TpBufConfig::MAX_TP_PAYLOAD_LEN, actual.tp_max_payload_len);
}

Expand Down
24 changes: 24 additions & 0 deletions src/tp_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,27 @@ where
}
}

pub fn with_capacity(buf_config: TpBufConfig, initial_bufs_count: usize) -> TpPool<ChannelId, Timestamp> {
TpPool {
active: HashMap::with_capacity(initial_bufs_count),
finished: {
let mut v = Vec::with_capacity(initial_bufs_count);
for _ in 0..initial_bufs_count {
v.push(TpBuf::new(buf_config.clone()));
}
v
},
buf_config,
}
}

/// Reserves the given number as buffers.
pub fn reserve(&mut self, additional: usize) {
self.finished.reserve(additional);
for _ in 0..additional {
self.finished.push(TpBuf::new(self.buf_config.clone()));
}
self.active.reserve(additional);
}

#[inline]
Expand Down Expand Up @@ -176,13 +191,22 @@ mod tests {
assert_eq!(pool.buf_config(), &TpBufConfig::default());
}

#[test]
fn with_capacity() {
let pool = TpPool::<(), ()>::with_capacity(Default::default(), 3);
assert_eq!(3, pool.finished_bufs().len());
assert!(pool.active.capacity() >= 3);
}

#[test]
fn reserve() {
let mut pool = TpPool::<(), ()>::new(Default::default());
pool.reserve(2);
assert_eq!(2, pool.finished_bufs().len());
assert!(pool.active.capacity() >= 2);
pool.reserve(3);
assert_eq!(5, pool.finished_bufs().len());
assert!(pool.active.capacity() >= 5);
}

struct TestPacket {
Expand Down

0 comments on commit f1f164e

Please sign in to comment.