Skip to content

Commit

Permalink
Reserve minimum number of entries in Scope.
Browse files Browse the repository at this point in the history
  • Loading branch information
schungx committed Sep 2, 2023
1 parent a905cfc commit 66bcd2f
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/types/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ use std::{
marker::PhantomData,
};

/// Minimum number of entries in the [`Scope`] to avoid reallocations.
pub const MIN_SCOPE_ENTRIES: usize = 8;

/// Type containing information about the current scope. Useful for keeping state between
/// [`Engine`][crate::Engine] evaluation runs.
///
Expand Down Expand Up @@ -343,6 +346,11 @@ impl Scope<'_> {
access: AccessMode,
mut value: Dynamic,
) -> &mut Self {
if self.is_empty() {
self.names.reserve(MIN_SCOPE_ENTRIES);
self.values.reserve(MIN_SCOPE_ENTRIES);
self.aliases.reserve(MIN_SCOPE_ENTRIES);
}
self.names.push(name.into());
self.aliases.push(Vec::new());
value.set_access_mode(access);
Expand Down Expand Up @@ -783,6 +791,11 @@ impl Scope<'_> {
let mut v2 = v1.clone();
v2.set_access_mode(v1.access_mode());

if self.is_empty() {
scope.names.reserve(MIN_SCOPE_ENTRIES);
scope.values.reserve(MIN_SCOPE_ENTRIES);
scope.aliases.reserve(MIN_SCOPE_ENTRIES);
}
scope.names.push(name.clone());
scope.values.push(v2);
scope.aliases.push(alias.clone());
Expand Down

0 comments on commit 66bcd2f

Please sign in to comment.