/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 | 26.4k | pub fn new() -> Self { |
30 | 26.4k | Buffer { slab: Slab::new() } |
31 | 26.4k | } <h2::proto::streams::buffer::Buffer<h2::proto::streams::recv::Event>>::new Line | Count | Source | 29 | 13.2k | pub fn new() -> Self { | 30 | 13.2k | Buffer { slab: Slab::new() } | 31 | 13.2k | } |
<h2::proto::streams::buffer::Buffer<h2::frame::Frame>>::new Line | Count | Source | 29 | 645 | pub fn new() -> Self { | 30 | 645 | Buffer { slab: Slab::new() } | 31 | 645 | } |
<h2::proto::streams::buffer::Buffer<h2::frame::Frame>>::new Line | Count | Source | 29 | 12.5k | pub fn new() -> Self { | 30 | 12.5k | Buffer { slab: Slab::new() } | 31 | 12.5k | } |
|
32 | | |
33 | 1.14k | pub fn is_empty(&self) -> bool { |
34 | 1.14k | self.slab.is_empty() |
35 | 1.14k | } |
36 | | } |
37 | | |
38 | | impl Deque { |
39 | 933k | pub fn new() -> Self { |
40 | 933k | Deque { indices: None } |
41 | 933k | } |
42 | | |
43 | 3.90M | pub fn is_empty(&self) -> bool { |
44 | 3.90M | self.indices.is_none() |
45 | 3.90M | } |
46 | | |
47 | 897k | pub fn push_back<T>(&mut self, buf: &mut Buffer<T>, value: T) { |
48 | 897k | let key = buf.slab.insert(Slot { value, next: None }); |
49 | | |
50 | 897k | match self.indices { |
51 | 428k | Some(ref mut idxs) => { |
52 | 428k | buf.slab[idxs.tail].next = Some(key); |
53 | 428k | idxs.tail = key; |
54 | 428k | } |
55 | 468k | None => { |
56 | 468k | self.indices = Some(Indices { |
57 | 468k | head: key, |
58 | 468k | tail: key, |
59 | 468k | }); |
60 | 468k | } |
61 | | } |
62 | 897k | } <h2::proto::streams::buffer::Deque>::push_back::<h2::proto::streams::recv::Event> Line | Count | Source | 47 | 5.14k | pub fn push_back<T>(&mut self, buf: &mut Buffer<T>, value: T) { | 48 | 5.14k | let key = buf.slab.insert(Slot { value, next: None }); | 49 | | | 50 | 5.14k | match self.indices { | 51 | 3.39k | Some(ref mut idxs) => { | 52 | 3.39k | buf.slab[idxs.tail].next = Some(key); | 53 | 3.39k | idxs.tail = key; | 54 | 3.39k | } | 55 | 1.74k | None => { | 56 | 1.74k | self.indices = Some(Indices { | 57 | 1.74k | head: key, | 58 | 1.74k | tail: key, | 59 | 1.74k | }); | 60 | 1.74k | } | 61 | | } | 62 | 5.14k | } |
<h2::proto::streams::buffer::Deque>::push_back::<h2::frame::Frame> Line | Count | Source | 47 | 548 | pub fn push_back<T>(&mut self, buf: &mut Buffer<T>, value: T) { | 48 | 548 | let key = buf.slab.insert(Slot { value, next: None }); | 49 | | | 50 | 548 | 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 | 548 | None => { | 56 | 548 | self.indices = Some(Indices { | 57 | 548 | head: key, | 58 | 548 | tail: key, | 59 | 548 | }); | 60 | 548 | } | 61 | | } | 62 | 548 | } |
<h2::proto::streams::buffer::Deque>::push_back::<h2::frame::Frame> Line | Count | Source | 47 | 891k | pub fn push_back<T>(&mut self, buf: &mut Buffer<T>, value: T) { | 48 | 891k | let key = buf.slab.insert(Slot { value, next: None }); | 49 | | | 50 | 891k | match self.indices { | 51 | 424k | Some(ref mut idxs) => { | 52 | 424k | buf.slab[idxs.tail].next = Some(key); | 53 | 424k | idxs.tail = key; | 54 | 424k | } | 55 | 466k | None => { | 56 | 466k | self.indices = Some(Indices { | 57 | 466k | head: key, | 58 | 466k | tail: key, | 59 | 466k | }); | 60 | 466k | } | 61 | | } | 62 | 891k | } |
|
63 | | |
64 | 39.8k | pub fn push_front<T>(&mut self, buf: &mut Buffer<T>, value: T) { |
65 | 39.8k | let key = buf.slab.insert(Slot { value, next: None }); |
66 | | |
67 | 39.8k | match self.indices { |
68 | 0 | Some(ref mut idxs) => { |
69 | 0 | buf.slab[key].next = Some(idxs.head); |
70 | 0 | idxs.head = key; |
71 | 0 | } |
72 | 39.8k | None => { |
73 | 39.8k | self.indices = Some(Indices { |
74 | 39.8k | head: key, |
75 | 39.8k | tail: key, |
76 | 39.8k | }); |
77 | 39.8k | } |
78 | | } |
79 | 39.8k | } 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 | 39.8k | pub fn push_front<T>(&mut self, buf: &mut Buffer<T>, value: T) { | 65 | 39.8k | let key = buf.slab.insert(Slot { value, next: None }); | 66 | | | 67 | 39.8k | match self.indices { | 68 | 0 | Some(ref mut idxs) => { | 69 | 0 | buf.slab[key].next = Some(idxs.head); | 70 | 0 | idxs.head = key; | 71 | 0 | } | 72 | 39.8k | None => { | 73 | 39.8k | self.indices = Some(Indices { | 74 | 39.8k | head: key, | 75 | 39.8k | tail: key, | 76 | 39.8k | }); | 77 | 39.8k | } | 78 | | } | 79 | 39.8k | } |
|
80 | | |
81 | 2.05M | pub fn pop_front<T>(&mut self, buf: &mut Buffer<T>) -> Option<T> { |
82 | 2.05M | match self.indices { |
83 | 935k | Some(mut idxs) => { |
84 | 935k | let mut slot = buf.slab.remove(idxs.head); |
85 | | |
86 | 935k | if idxs.head == idxs.tail { |
87 | 507k | assert!(slot.next.is_none()); |
88 | 507k | self.indices = None; |
89 | 427k | } else { |
90 | 427k | idxs.head = slot.next.take().unwrap(); |
91 | 427k | self.indices = Some(idxs); |
92 | 427k | } |
93 | | |
94 | 935k | Some(slot.value) |
95 | | } |
96 | 1.12M | None => None, |
97 | | } |
98 | 2.05M | } <h2::proto::streams::buffer::Deque>::pop_front::<h2::proto::streams::recv::Event> Line | Count | Source | 81 | 457k | pub fn pop_front<T>(&mut self, buf: &mut Buffer<T>) -> Option<T> { | 82 | 457k | match self.indices { | 83 | 3.95k | Some(mut idxs) => { | 84 | 3.95k | let mut slot = buf.slab.remove(idxs.head); | 85 | | | 86 | 3.95k | if idxs.head == idxs.tail { | 87 | 938 | assert!(slot.next.is_none()); | 88 | 938 | self.indices = None; | 89 | 3.01k | } else { | 90 | 3.01k | idxs.head = slot.next.take().unwrap(); | 91 | 3.01k | self.indices = Some(idxs); | 92 | 3.01k | } | 93 | | | 94 | 3.95k | Some(slot.value) | 95 | | } | 96 | 453k | None => None, | 97 | | } | 98 | 457k | } |
<h2::proto::streams::buffer::Deque>::pop_front::<h2::frame::Frame> Line | Count | Source | 81 | 1.09k | pub fn pop_front<T>(&mut self, buf: &mut Buffer<T>) -> Option<T> { | 82 | 1.09k | match self.indices { | 83 | 548 | Some(mut idxs) => { | 84 | 548 | let mut slot = buf.slab.remove(idxs.head); | 85 | | | 86 | 548 | if idxs.head == idxs.tail { | 87 | 548 | assert!(slot.next.is_none()); | 88 | 548 | self.indices = None; | 89 | 0 | } else { | 90 | 0 | idxs.head = slot.next.take().unwrap(); | 91 | 0 | self.indices = Some(idxs); | 92 | 0 | } | 93 | | | 94 | 548 | Some(slot.value) | 95 | | } | 96 | 548 | None => None, | 97 | | } | 98 | 1.09k | } |
<h2::proto::streams::buffer::Deque>::pop_front::<h2::frame::Frame> Line | Count | Source | 81 | 1.59M | pub fn pop_front<T>(&mut self, buf: &mut Buffer<T>) -> Option<T> { | 82 | 1.59M | match self.indices { | 83 | 931k | Some(mut idxs) => { | 84 | 931k | let mut slot = buf.slab.remove(idxs.head); | 85 | | | 86 | 931k | if idxs.head == idxs.tail { | 87 | 506k | assert!(slot.next.is_none()); | 88 | 506k | self.indices = None; | 89 | 424k | } else { | 90 | 424k | idxs.head = slot.next.take().unwrap(); | 91 | 424k | self.indices = Some(idxs); | 92 | 424k | } | 93 | | | 94 | 931k | Some(slot.value) | 95 | | } | 96 | 667k | None => None, | 97 | | } | 98 | 1.59M | } |
|
99 | | } |