/src/h2/src/proto/streams/buffer.rs
Line | Count | Source |
1 | | use slab::Slab; |
2 | | |
3 | | /// Buffers frames for multiple streams. |
4 | | #[derive(Debug)] |
5 | | pub struct Buffer<T> { |
6 | | slab: Slab<Slot<T>>, |
7 | | } |
8 | | |
9 | | /// A sequence of frames in a `Buffer` |
10 | | #[derive(Debug)] |
11 | | pub struct Deque { |
12 | | indices: Option<Indices>, |
13 | | } |
14 | | |
15 | | /// Tracks the head & tail for a sequence of frames in a `Buffer`. |
16 | | #[derive(Debug, Default, Copy, Clone)] |
17 | | struct Indices { |
18 | | head: usize, |
19 | | tail: usize, |
20 | | } |
21 | | |
22 | | #[derive(Debug)] |
23 | | struct Slot<T> { |
24 | | value: T, |
25 | | next: Option<usize>, |
26 | | } |
27 | | |
28 | | impl<T> Buffer<T> { |
29 | 28.7k | pub fn new() -> Self { |
30 | 28.7k | Buffer { slab: Slab::new() } |
31 | 28.7k | } <h2::proto::streams::buffer::Buffer<h2::proto::streams::recv::Event>>::new Line | Count | Source | 29 | 14.3k | pub fn new() -> Self { | 30 | 14.3k | Buffer { slab: Slab::new() } | 31 | 14.3k | } |
<h2::proto::streams::buffer::Buffer<h2::frame::Frame>>::new Line | Count | Source | 29 | 797 | pub fn new() -> Self { | 30 | 797 | Buffer { slab: Slab::new() } | 31 | 797 | } |
<h2::proto::streams::buffer::Buffer<h2::frame::Frame>>::new Line | Count | Source | 29 | 13.5k | pub fn new() -> Self { | 30 | 13.5k | Buffer { slab: Slab::new() } | 31 | 13.5k | } |
|
32 | | |
33 | 1.06k | pub fn is_empty(&self) -> bool { |
34 | 1.06k | self.slab.is_empty() |
35 | 1.06k | } |
36 | | } |
37 | | |
38 | | impl Deque { |
39 | 895k | pub fn new() -> Self { |
40 | 895k | Deque { indices: None } |
41 | 895k | } |
42 | | |
43 | 3.58M | pub fn is_empty(&self) -> bool { |
44 | 3.58M | self.indices.is_none() |
45 | 3.58M | } |
46 | | |
47 | 893k | pub fn push_back<T>(&mut self, buf: &mut Buffer<T>, value: T) { |
48 | 893k | let key = buf.slab.insert(Slot { value, next: None }); |
49 | | |
50 | 893k | match self.indices { |
51 | 444k | Some(ref mut idxs) => { |
52 | 444k | buf.slab[idxs.tail].next = Some(key); |
53 | 444k | idxs.tail = key; |
54 | 444k | } |
55 | 448k | None => { |
56 | 448k | self.indices = Some(Indices { |
57 | 448k | head: key, |
58 | 448k | tail: key, |
59 | 448k | }); |
60 | 448k | } |
61 | | } |
62 | 893k | } <h2::proto::streams::buffer::Deque>::push_back::<h2::proto::streams::recv::Event> Line | Count | Source | 47 | 2.91k | pub fn push_back<T>(&mut self, buf: &mut Buffer<T>, value: T) { | 48 | 2.91k | let key = buf.slab.insert(Slot { value, next: None }); | 49 | | | 50 | 2.91k | match self.indices { | 51 | 2.18k | Some(ref mut idxs) => { | 52 | 2.18k | buf.slab[idxs.tail].next = Some(key); | 53 | 2.18k | idxs.tail = key; | 54 | 2.18k | } | 55 | 729 | None => { | 56 | 729 | self.indices = Some(Indices { | 57 | 729 | head: key, | 58 | 729 | tail: key, | 59 | 729 | }); | 60 | 729 | } | 61 | | } | 62 | 2.91k | } |
<h2::proto::streams::buffer::Deque>::push_back::<h2::frame::Frame> Line | Count | Source | 47 | 710 | pub fn push_back<T>(&mut self, buf: &mut Buffer<T>, value: T) { | 48 | 710 | let key = buf.slab.insert(Slot { value, next: None }); | 49 | | | 50 | 710 | match self.indices { | 51 | 0 | Some(ref mut idxs) => { | 52 | 0 | buf.slab[idxs.tail].next = Some(key); | 53 | 0 | idxs.tail = key; | 54 | 0 | } | 55 | 710 | None => { | 56 | 710 | self.indices = Some(Indices { | 57 | 710 | head: key, | 58 | 710 | tail: key, | 59 | 710 | }); | 60 | 710 | } | 61 | | } | 62 | 710 | } |
<h2::proto::streams::buffer::Deque>::push_back::<h2::frame::Frame> Line | Count | Source | 47 | 889k | pub fn push_back<T>(&mut self, buf: &mut Buffer<T>, value: T) { | 48 | 889k | let key = buf.slab.insert(Slot { value, next: None }); | 49 | | | 50 | 889k | match self.indices { | 51 | 442k | Some(ref mut idxs) => { | 52 | 442k | buf.slab[idxs.tail].next = Some(key); | 53 | 442k | idxs.tail = key; | 54 | 442k | } | 55 | 447k | None => { | 56 | 447k | self.indices = Some(Indices { | 57 | 447k | head: key, | 58 | 447k | tail: key, | 59 | 447k | }); | 60 | 447k | } | 61 | | } | 62 | 889k | } |
|
63 | | |
64 | 44.3k | pub fn push_front<T>(&mut self, buf: &mut Buffer<T>, value: T) { |
65 | 44.3k | let key = buf.slab.insert(Slot { value, next: None }); |
66 | | |
67 | 44.3k | match self.indices { |
68 | 1 | Some(ref mut idxs) => { |
69 | 1 | buf.slab[key].next = Some(idxs.head); |
70 | 1 | idxs.head = key; |
71 | 1 | } |
72 | 44.3k | None => { |
73 | 44.3k | self.indices = Some(Indices { |
74 | 44.3k | head: key, |
75 | 44.3k | tail: key, |
76 | 44.3k | }); |
77 | 44.3k | } |
78 | | } |
79 | 44.3k | } Unexecuted instantiation: <h2::proto::streams::buffer::Deque>::push_front::<h2::proto::streams::recv::Event> <h2::proto::streams::buffer::Deque>::push_front::<h2::frame::Frame> Line | Count | Source | 64 | 44.3k | pub fn push_front<T>(&mut self, buf: &mut Buffer<T>, value: T) { | 65 | 44.3k | let key = buf.slab.insert(Slot { value, next: None }); | 66 | | | 67 | 44.3k | match self.indices { | 68 | 1 | Some(ref mut idxs) => { | 69 | 1 | buf.slab[key].next = Some(idxs.head); | 70 | 1 | idxs.head = key; | 71 | 1 | } | 72 | 44.3k | None => { | 73 | 44.3k | self.indices = Some(Indices { | 74 | 44.3k | head: key, | 75 | 44.3k | tail: key, | 76 | 44.3k | }); | 77 | 44.3k | } | 78 | | } | 79 | 44.3k | } |
|
80 | | |
81 | 2.31M | pub fn pop_front<T>(&mut self, buf: &mut Buffer<T>) -> Option<T> { |
82 | 2.31M | match self.indices { |
83 | 937k | Some(mut idxs) => { |
84 | 937k | let mut slot = buf.slab.remove(idxs.head); |
85 | | |
86 | 937k | if idxs.head == idxs.tail { |
87 | 492k | assert!(slot.next.is_none()); |
88 | 492k | self.indices = None; |
89 | 444k | } else { |
90 | 444k | idxs.head = slot.next.take().unwrap(); |
91 | 444k | self.indices = Some(idxs); |
92 | 444k | } |
93 | | |
94 | 937k | Some(slot.value) |
95 | | } |
96 | 1.37M | None => None, |
97 | | } |
98 | 2.31M | } <h2::proto::streams::buffer::Deque>::pop_front::<h2::proto::streams::recv::Event> Line | Count | Source | 81 | 914k | pub fn pop_front<T>(&mut self, buf: &mut Buffer<T>) -> Option<T> { | 82 | 914k | match self.indices { | 83 | 2.71k | Some(mut idxs) => { | 84 | 2.71k | let mut slot = buf.slab.remove(idxs.head); | 85 | | | 86 | 2.71k | if idxs.head == idxs.tail { | 87 | 523 | assert!(slot.next.is_none()); | 88 | 523 | self.indices = None; | 89 | 2.18k | } else { | 90 | 2.18k | idxs.head = slot.next.take().unwrap(); | 91 | 2.18k | self.indices = Some(idxs); | 92 | 2.18k | } | 93 | | | 94 | 2.71k | Some(slot.value) | 95 | | } | 96 | 911k | None => None, | 97 | | } | 98 | 914k | } |
<h2::proto::streams::buffer::Deque>::pop_front::<h2::frame::Frame> Line | Count | Source | 81 | 1.42k | pub fn pop_front<T>(&mut self, buf: &mut Buffer<T>) -> Option<T> { | 82 | 1.42k | match self.indices { | 83 | 710 | Some(mut idxs) => { | 84 | 710 | let mut slot = buf.slab.remove(idxs.head); | 85 | | | 86 | 710 | if idxs.head == idxs.tail { | 87 | 710 | assert!(slot.next.is_none()); | 88 | 710 | self.indices = None; | 89 | 0 | } else { | 90 | 0 | idxs.head = slot.next.take().unwrap(); | 91 | 0 | self.indices = Some(idxs); | 92 | 0 | } | 93 | | | 94 | 710 | Some(slot.value) | 95 | | } | 96 | 710 | None => None, | 97 | | } | 98 | 1.42k | } |
<h2::proto::streams::buffer::Deque>::pop_front::<h2::frame::Frame> Line | Count | Source | 81 | 1.39M | pub fn pop_front<T>(&mut self, buf: &mut Buffer<T>) -> Option<T> { | 82 | 1.39M | match self.indices { | 83 | 934k | Some(mut idxs) => { | 84 | 934k | let mut slot = buf.slab.remove(idxs.head); | 85 | | | 86 | 934k | if idxs.head == idxs.tail { | 87 | 491k | assert!(slot.next.is_none()); | 88 | 491k | self.indices = None; | 89 | 442k | } else { | 90 | 442k | idxs.head = slot.next.take().unwrap(); | 91 | 442k | self.indices = Some(idxs); | 92 | 442k | } | 93 | | | 94 | 934k | Some(slot.value) | 95 | | } | 96 | 461k | None => None, | 97 | | } | 98 | 1.39M | } |
|
99 | | } |