Skip to content

Commit

Permalink
Use a u32 index for stream store (#451)
Browse files Browse the repository at this point in the history
A connection can never have more than u32::MAX >> 1 streams, so we'll
never store more than that many in the store slab. Define the
`SlabIndex` as a `u32` to reduce the number of bytes moved around.
  • Loading branch information
seanmonstar authored Feb 28, 2020
1 parent 78c8ec6 commit 1b01300
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/proto/streams/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ pub(crate) struct Key {
stream_id: StreamId,
}

// We can never have more than `StreamId::MAX` streams in the store,
// so we can save a smaller index (u32 vs usize).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct SlabIndex(usize);
struct SlabIndex(u32);

#[derive(Debug)]
pub(super) struct Queue<N> {
Expand Down Expand Up @@ -102,7 +104,7 @@ impl Store {
}

pub fn insert(&mut self, id: StreamId, val: Stream) -> Ptr {
let index = SlabIndex(self.slab.insert(val));
let index = SlabIndex(self.slab.insert(val) as u32);
assert!(self.ids.insert(id, index).is_none());

Ptr {
Expand Down Expand Up @@ -171,7 +173,7 @@ impl ops::Index<Key> for Store {

fn index(&self, key: Key) -> &Self::Output {
self.slab
.get(key.index.0)
.get(key.index.0 as usize)
.filter(|s| s.id == key.stream_id)
.unwrap_or_else(|| {
panic!("dangling store key for stream_id={:?}", key.stream_id);
Expand All @@ -182,7 +184,7 @@ impl ops::Index<Key> for Store {
impl ops::IndexMut<Key> for Store {
fn index_mut(&mut self, key: Key) -> &mut Self::Output {
self.slab
.get_mut(key.index.0)
.get_mut(key.index.0 as usize)
.filter(|s| s.id == key.stream_id)
.unwrap_or_else(|| {
panic!("dangling store key for stream_id={:?}", key.stream_id);
Expand Down Expand Up @@ -330,7 +332,7 @@ impl<'a> Ptr<'a> {
debug_assert!(!self.store.ids.contains_key(&self.key.stream_id));

// Remove the stream state
let stream = self.store.slab.remove(self.key.index.0);
let stream = self.store.slab.remove(self.key.index.0 as usize);
assert_eq!(stream.id, self.key.stream_id);
stream.id
}
Expand Down Expand Up @@ -390,7 +392,7 @@ impl<'a> VacantEntry<'a> {
pub fn insert(self, value: Stream) -> Key {
// Insert the value in the slab
let stream_id = value.id;
let index = SlabIndex(self.slab.insert(value));
let index = SlabIndex(self.slab.insert(value) as u32);

// Insert the handle in the ID map
self.ids.insert(index);
Expand Down

0 comments on commit 1b01300

Please sign in to comment.