Skip to content

Commit 64949dc

Browse files
authored
Micro-optimize Scope access (#615)
Instead of having the compiler do bounds-checks and generate panic-ing code, just make that case impossible by making sure there is always a top scope.
1 parent 10c5640 commit 64949dc

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

sentry-core/src/scope/real.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::Client;
1010

1111
#[derive(Debug)]
1212
pub struct Stack {
13+
top: StackLayer,
1314
layers: Vec<StackLayer>,
1415
}
1516

@@ -77,29 +78,31 @@ pub struct StackLayer {
7778
impl Stack {
7879
pub fn from_client_and_scope(client: Option<Arc<Client>>, scope: Arc<Scope>) -> Stack {
7980
Stack {
80-
layers: vec![StackLayer { client, scope }],
81+
top: StackLayer { client, scope },
82+
layers: vec![],
8183
}
8284
}
8385

8486
pub fn push(&mut self) {
85-
let layer = self.layers[self.layers.len() - 1].clone();
87+
let layer = self.top.clone();
8688
self.layers.push(layer);
8789
}
8890

8991
pub fn pop(&mut self) {
90-
if self.layers.len() <= 1 {
92+
if self.layers.is_empty() {
9193
panic!("Pop from empty stack");
9294
}
93-
self.layers.pop().unwrap();
95+
self.top = self.layers.pop().unwrap();
9496
}
9597

98+
#[inline(always)]
9699
pub fn top(&self) -> &StackLayer {
97-
&self.layers[self.layers.len() - 1]
100+
&self.top
98101
}
99102

103+
#[inline(always)]
100104
pub fn top_mut(&mut self) -> &mut StackLayer {
101-
let top = self.layers.len() - 1;
102-
&mut self.layers[top]
105+
&mut self.top
103106
}
104107

105108
pub fn depth(&self) -> usize {

0 commit comments

Comments
 (0)