Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ cbindgen = "0.27"
name = "arena"
harness = false

[[bench]]
name = "list"
harness = false

[[bench]]
name = "server"
harness = false
Expand Down
200 changes: 200 additions & 0 deletions benches/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/*
* Copyright (C) 2026 Fastly, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

use criterion::{criterion_group, criterion_main, Criterion};
use pushpin::core::arena;
use pushpin::core::list;
use slab::Slab;
use std::rc::Rc;

fn criterion_benchmark(c: &mut Criterion) {
const NODE_COUNT: usize = 10000;

{
// Preallocate the nodes memory
let mut nodes_slab = Some(Slab::with_capacity(NODE_COUNT));

c.bench_function(&format!("index slab list push pop {NODE_COUNT}"), |b| {
b.iter(|| {
let mut nodes = nodes_slab.take().unwrap();
let mut l = list::List::default();

let mut next_value: u64 = 0;
while nodes.len() < nodes.capacity() {
let n = nodes.insert(list::Node::new(next_value));
l.push_back(&mut nodes, n);
next_value += 1;
}

let mut expected_value = 0;
while !nodes.is_empty() {
let n = l.pop_front(&mut nodes).unwrap();
assert_eq!(nodes[n].value, expected_value);
nodes.remove(n);
expected_value += 1;
}

nodes_slab = Some(nodes);
})
});
}

{
// Preallocate the nodes memory
let mut nodes_slab = Some(Slab::with_capacity(NODE_COUNT));

c.bench_function(&format!("generic slab list push pop {NODE_COUNT}"), |b| {
b.iter(|| {
let mut nodes = nodes_slab.take().unwrap();
let mut l = list::SlabList::default();

let mut next_value: u64 = 0;
while nodes.len() < nodes.capacity() {
let n = nodes.insert(list::SlabNode::new(next_value));
l.push_back(&mut nodes, n);
next_value += 1;
}

let mut expected_value = 0;
while !nodes.is_empty() {
let n = l.pop_front(&mut nodes).unwrap();
assert_eq!(nodes[n].value, expected_value);
nodes.remove(n);
expected_value += 1;
}

nodes_slab = Some(nodes);
})
});
}

{
// Preallocate the nodes memory
let nodes_memory = Rc::new(arena::RcMemory::new(NODE_COUNT));

c.bench_function(&format!("arena rc list push pop {NODE_COUNT}"), |b| {
b.iter(|| {
let mut b = list::RcBackend::default();
let mut l = list::GenericList::default();

let mut next_value: [u64; 32] = [0; 32];
while next_value[0] < NODE_COUNT as u64 {
let n = list::RcNode::new(next_value, Some(&nodes_memory));
l.push_back(&mut b, n);
next_value[0] += 1;
}

let mut expected_value = 0;
while expected_value < NODE_COUNT as u64 {
let n = l.pop_front(&mut b).unwrap();
assert_eq!(n.value()[0], expected_value);
expected_value += 1;
}
})
});
}

{
c.bench_function(&format!("std rc list push pop {NODE_COUNT}"), |b| {
b.iter(|| {
let mut b = list::RcBackend::default();
let mut l = list::GenericList::default();

let mut next_value: [u64; 32] = [0; 32];
while next_value[0] < NODE_COUNT as u64 {
let n = list::RcNode::new(next_value, None);
l.push_back(&mut b, n);
next_value[0] += 1;
}

let mut expected_value = 0;
while expected_value < NODE_COUNT as u64 {
let n = l.pop_front(&mut b).unwrap();
assert_eq!(n.value()[0], expected_value);
expected_value += 1;
}
})
});
}

{
// Preallocate the nodes
let nodes_memory = Rc::new(arena::RcMemory::new(NODE_COUNT));
let mut nodes = Vec::new();
let mut next_value: [u64; 32] = [0; 32];
while nodes_memory.len() < nodes_memory.capacity() {
let n = list::RcNode::new(next_value, Some(&nodes_memory));
nodes.push(n);
next_value[0] += 1;
}

c.bench_function(
&format!("arena rc list push pop {NODE_COUNT} (preallocated nodes)"),
|b| {
b.iter(|| {
let mut b = list::RcBackend::default();
let mut l = list::GenericList::default();

for n in &nodes {
l.push_back(&mut b, n.clone());
}

let mut expected_value = 0;
while expected_value < NODE_COUNT as u64 {
let n = l.pop_front(&mut b).unwrap();
assert_eq!(n.value()[0], expected_value);
expected_value += 1;
}
})
},
);
}

{
// Preallocate the nodes
let mut nodes = Vec::new();
let mut next_value: [u64; 32] = [0; 32];
while next_value[0] < NODE_COUNT as u64 {
let n = list::RcNode::new(next_value, None);
nodes.push(n);
next_value[0] += 1;
}

c.bench_function(
&format!("std rc list push pop {NODE_COUNT} (preallocated nodes)"),
|b| {
b.iter(|| {
let mut b = list::RcBackend::default();
let mut l = list::GenericList::default();

for n in &nodes {
l.push_back(&mut b, n.clone());
}

let mut expected_value = 0;
while expected_value < NODE_COUNT as u64 {
let n = l.pop_front(&mut b).unwrap();
assert_eq!(n.value()[0], expected_value);
expected_value += 1;
}
})
},
);
}
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
6 changes: 3 additions & 3 deletions src/connmgr/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ struct PoolItem<K, V> {
}

pub struct Pool<K, V> {
nodes: Slab<list::Node<PoolItem<K, V>>>,
by_key: HashMap<K, list::List>,
nodes: Slab<list::SlabNode<PoolItem<K, V>>>,
by_key: HashMap<K, list::SlabList<PoolItem<K, V>>>,
wheel: TimerWheel,
start: Instant,
current_ticks: u64,
Expand Down Expand Up @@ -69,7 +69,7 @@ where

let timer_id = self.wheel.add(expires, nkey).unwrap();

entry.insert(list::Node::new(PoolItem {
entry.insert(list::SlabNode::new(PoolItem {
key: key.clone(),
value,
timer_id,
Expand Down
14 changes: 13 additions & 1 deletion src/core/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,18 @@ impl<T> Memory<T> {
}
}

#[cfg(test)]
pub fn is_empty(&self) -> bool {
self.entries.borrow().is_empty()
}

pub fn len(&self) -> usize {
self.entries.borrow().len()
}

pub fn capacity(&self) -> usize {
self.entries.borrow().capacity()
}

// Returns a key and a pointer to the inserted entry.
//
// SAFETY: The returned pointer is guaranteed to be valid until the entry
Expand Down Expand Up @@ -390,6 +397,11 @@ impl<T> Rc<T> {
&self.inner().value
}

#[inline]
pub fn ptr_eq(this: &Rc<T>, other: &Rc<T>) -> bool {
this.ptr.as_ptr() == other.ptr.as_ptr()
}

#[inline(always)]
fn inner(&self) -> &RcInner<T> {
// SAFETY: ptr points to a slab element, and slab element addresses
Expand Down
Loading
Loading