/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 | 29.0k | pub fn new() -> Self { |
30 | 29.0k | Buffer { slab: Slab::new() } |
31 | 29.0k | } <h2::proto::streams::buffer::Buffer<h2::proto::streams::recv::Event>>::new Line | Count | Source | 29 | 14.5k | pub fn new() -> Self { | 30 | 14.5k | Buffer { slab: Slab::new() } | 31 | 14.5k | } |
<h2::proto::streams::buffer::Buffer<h2::frame::Frame>>::new Line | Count | Source | 29 | 749 | pub fn new() -> Self { | 30 | 749 | Buffer { slab: Slab::new() } | 31 | 749 | } |
<h2::proto::streams::buffer::Buffer<h2::frame::Frame>>::new Line | Count | Source | 29 | 13.7k | pub fn new() -> Self { | 30 | 13.7k | Buffer { slab: Slab::new() } | 31 | 13.7k | } |
|
32 | | |
33 | 1.22k | pub fn is_empty(&self) -> bool { |
34 | 1.22k | self.slab.is_empty() |
35 | 1.22k | } |
36 | | } |
37 | | |
38 | | impl Deque { |
39 | 1.09M | pub fn new() -> Self { |
40 | 1.09M | Deque { indices: None } |
41 | 1.09M | } |
42 | | |
43 | 4.63M | pub fn is_empty(&self) -> bool { |
44 | 4.63M | self.indices.is_none() |
45 | 4.63M | } |
46 | | |
47 | 1.04M | pub fn push_back<T>(&mut self, buf: &mut Buffer<T>, value: T) { |
48 | 1.04M | let key = buf.slab.insert(Slot { value, next: None }); |
49 | | |
50 | 1.04M | match self.indices { |
51 | 492k | Some(ref mut idxs) => { |
52 | 492k | buf.slab[idxs.tail].next = Some(key); |
53 | 492k | idxs.tail = key; |
54 | 492k | } |
55 | 549k | None => { |
56 | 549k | self.indices = Some(Indices { |
57 | 549k | head: key, |
58 | 549k | tail: key, |
59 | 549k | }); |
60 | 549k | } |
61 | | } |
62 | 1.04M | } <h2::proto::streams::buffer::Deque>::push_back::<h2::proto::streams::recv::Event> Line | Count | Source | 47 | 6.15k | pub fn push_back<T>(&mut self, buf: &mut Buffer<T>, value: T) { | 48 | 6.15k | let key = buf.slab.insert(Slot { value, next: None }); | 49 | | | 50 | 6.15k | match self.indices { | 51 | 3.96k | Some(ref mut idxs) => { | 52 | 3.96k | buf.slab[idxs.tail].next = Some(key); | 53 | 3.96k | idxs.tail = key; | 54 | 3.96k | } | 55 | 2.18k | None => { | 56 | 2.18k | self.indices = Some(Indices { | 57 | 2.18k | head: key, | 58 | 2.18k | tail: key, | 59 | 2.18k | }); | 60 | 2.18k | } | 61 | | } | 62 | 6.15k | } |
<h2::proto::streams::buffer::Deque>::push_back::<h2::frame::Frame> Line | Count | Source | 47 | 647 | pub fn push_back<T>(&mut self, buf: &mut Buffer<T>, value: T) { | 48 | 647 | let key = buf.slab.insert(Slot { value, next: None }); | 49 | | | 50 | 647 | 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 | 647 | None => { | 56 | 647 | self.indices = Some(Indices { | 57 | 647 | head: key, | 58 | 647 | tail: key, | 59 | 647 | }); | 60 | 647 | } | 61 | | } | 62 | 647 | } |
<h2::proto::streams::buffer::Deque>::push_back::<h2::frame::Frame> Line | Count | Source | 47 | 1.03M | pub fn push_back<T>(&mut self, buf: &mut Buffer<T>, value: T) { | 48 | 1.03M | let key = buf.slab.insert(Slot { value, next: None }); | 49 | | | 50 | 1.03M | match self.indices { | 51 | 488k | Some(ref mut idxs) => { | 52 | 488k | buf.slab[idxs.tail].next = Some(key); | 53 | 488k | idxs.tail = key; | 54 | 488k | } | 55 | 546k | None => { | 56 | 546k | self.indices = Some(Indices { | 57 | 546k | head: key, | 58 | 546k | tail: key, | 59 | 546k | }); | 60 | 546k | } | 61 | | } | 62 | 1.03M | } |
|
63 | | |
64 | 50.1k | pub fn push_front<T>(&mut self, buf: &mut Buffer<T>, value: T) { |
65 | 50.1k | let key = buf.slab.insert(Slot { value, next: None }); |
66 | | |
67 | 50.1k | match self.indices { |
68 | 102 | Some(ref mut idxs) => { |
69 | 102 | buf.slab[key].next = Some(idxs.head); |
70 | 102 | idxs.head = key; |
71 | 102 | } |
72 | 50.0k | None => { |
73 | 50.0k | self.indices = Some(Indices { |
74 | 50.0k | head: key, |
75 | 50.0k | tail: key, |
76 | 50.0k | }); |
77 | 50.0k | } |
78 | | } |
79 | 50.1k | } 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 | 50.1k | pub fn push_front<T>(&mut self, buf: &mut Buffer<T>, value: T) { | 65 | 50.1k | let key = buf.slab.insert(Slot { value, next: None }); | 66 | | | 67 | 50.1k | match self.indices { | 68 | 102 | Some(ref mut idxs) => { | 69 | 102 | buf.slab[key].next = Some(idxs.head); | 70 | 102 | idxs.head = key; | 71 | 102 | } | 72 | 50.0k | None => { | 73 | 50.0k | self.indices = Some(Indices { | 74 | 50.0k | head: key, | 75 | 50.0k | tail: key, | 76 | 50.0k | }); | 77 | 50.0k | } | 78 | | } | 79 | 50.1k | } |
|
80 | | |
81 | 2.45M | pub fn pop_front<T>(&mut self, buf: &mut Buffer<T>) -> Option<T> { |
82 | 2.45M | match self.indices { |
83 | 1.09M | Some(mut idxs) => { |
84 | 1.09M | let mut slot = buf.slab.remove(idxs.head); |
85 | | |
86 | 1.09M | if idxs.head == idxs.tail { |
87 | 598k | assert!(slot.next.is_none()); |
88 | 598k | self.indices = None; |
89 | 491k | } else { |
90 | 491k | idxs.head = slot.next.take().unwrap(); |
91 | 491k | self.indices = Some(idxs); |
92 | 491k | } |
93 | | |
94 | 1.09M | Some(slot.value) |
95 | | } |
96 | 1.36M | None => None, |
97 | | } |
98 | 2.45M | } <h2::proto::streams::buffer::Deque>::pop_front::<h2::proto::streams::recv::Event> Line | Count | Source | 81 | 530k | pub fn pop_front<T>(&mut self, buf: &mut Buffer<T>) -> Option<T> { | 82 | 530k | match self.indices { | 83 | 4.49k | Some(mut idxs) => { | 84 | 4.49k | let mut slot = buf.slab.remove(idxs.head); | 85 | | | 86 | 4.49k | if idxs.head == idxs.tail { | 87 | 1.23k | assert!(slot.next.is_none()); | 88 | 1.23k | self.indices = None; | 89 | 3.26k | } else { | 90 | 3.26k | idxs.head = slot.next.take().unwrap(); | 91 | 3.26k | self.indices = Some(idxs); | 92 | 3.26k | } | 93 | | | 94 | 4.49k | Some(slot.value) | 95 | | } | 96 | 525k | None => None, | 97 | | } | 98 | 530k | } |
<h2::proto::streams::buffer::Deque>::pop_front::<h2::frame::Frame> Line | Count | Source | 81 | 1.29k | pub fn pop_front<T>(&mut self, buf: &mut Buffer<T>) -> Option<T> { | 82 | 1.29k | match self.indices { | 83 | 647 | Some(mut idxs) => { | 84 | 647 | let mut slot = buf.slab.remove(idxs.head); | 85 | | | 86 | 647 | if idxs.head == idxs.tail { | 87 | 647 | assert!(slot.next.is_none()); | 88 | 647 | self.indices = None; | 89 | 0 | } else { | 90 | 0 | idxs.head = slot.next.take().unwrap(); | 91 | 0 | self.indices = Some(idxs); | 92 | 0 | } | 93 | | | 94 | 647 | Some(slot.value) | 95 | | } | 96 | 647 | None => None, | 97 | | } | 98 | 1.29k | } |
<h2::proto::streams::buffer::Deque>::pop_front::<h2::frame::Frame> Line | Count | Source | 81 | 1.91M | pub fn pop_front<T>(&mut self, buf: &mut Buffer<T>) -> Option<T> { | 82 | 1.91M | match self.indices { | 83 | 1.08M | Some(mut idxs) => { | 84 | 1.08M | let mut slot = buf.slab.remove(idxs.head); | 85 | | | 86 | 1.08M | if idxs.head == idxs.tail { | 87 | 596k | assert!(slot.next.is_none()); | 88 | 596k | self.indices = None; | 89 | 488k | } else { | 90 | 488k | idxs.head = slot.next.take().unwrap(); | 91 | 488k | self.indices = Some(idxs); | 92 | 488k | } | 93 | | | 94 | 1.08M | Some(slot.value) | 95 | | } | 96 | 834k | None => None, | 97 | | } | 98 | 1.91M | } |
|
99 | | } |