From a7c32e29dd04d2d3b40665e1b5df989a9267b9f2 Mon Sep 17 00:00:00 2001 From: Arpad Borsos Date: Thu, 14 Sep 2023 14:40:41 +0200 Subject: [PATCH] Micro-optimize Scope access 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. --- sentry-core/src/scope/real.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/sentry-core/src/scope/real.rs b/sentry-core/src/scope/real.rs index 31c55b0d..f38ed52d 100644 --- a/sentry-core/src/scope/real.rs +++ b/sentry-core/src/scope/real.rs @@ -10,6 +10,7 @@ use crate::Client; #[derive(Debug)] pub struct Stack { + top: StackLayer, layers: Vec, } @@ -77,29 +78,31 @@ pub struct StackLayer { impl Stack { pub fn from_client_and_scope(client: Option>, scope: Arc) -> Stack { Stack { - layers: vec![StackLayer { client, scope }], + top: StackLayer { client, scope }, + layers: vec![], } } pub fn push(&mut self) { - let layer = self.layers[self.layers.len() - 1].clone(); + let layer = self.top.clone(); self.layers.push(layer); } pub fn pop(&mut self) { - if self.layers.len() <= 1 { + if self.layers.is_empty() { panic!("Pop from empty stack"); } - self.layers.pop().unwrap(); + self.top = self.layers.pop().unwrap(); } + #[inline(always)] pub fn top(&self) -> &StackLayer { - &self.layers[self.layers.len() - 1] + &self.top } + #[inline(always)] pub fn top_mut(&mut self) -> &mut StackLayer { - let top = self.layers.len() - 1; - &mut self.layers[top] + &mut self.top } pub fn depth(&self) -> usize {