Skip to content

Commit c2665bd

Browse files
committed
chore(interpreter): Remove extra allocation
1 parent 7f1e202 commit c2665bd

File tree

3 files changed

+12
-17
lines changed

3 files changed

+12
-17
lines changed

liquid-interpreter/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ travis-ci = { repository = "cobalt-org/liquid-rust" }
1515
appveyor = { repository = "johannhof/liquid-rust" }
1616

1717
[dependencies]
18-
lazy_static = "1.0"
1918
itertools = "0.7.0"
2019
# Exposed in API
2120
liquid-error = { version = "0.15.0", path = "../liquid-error" }

liquid-interpreter/src/context.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,23 +97,18 @@ impl<'a, 'g> CycleState<'a, 'g> {
9797
}
9898
}
9999

100-
lazy_static! {
101-
static ref EMPTY_GLOBALS: Object = Object::new();
102-
}
103-
104100
/// Stack of variables.
105101
#[derive(Debug, Clone)]
106102
pub struct Stack<'g> {
107-
globals: &'g Globals,
103+
globals: Option<&'g Globals>,
108104
stack: Vec<Object>,
109105
}
110106

111107
impl<'g> Stack<'g> {
112108
/// Create an empty stack
113109
pub fn empty() -> Self {
114-
let empty: &Object = &*EMPTY_GLOBALS;
115110
Self {
116-
globals: empty,
111+
globals: None,
117112
// Mutable frame for globals.
118113
stack: vec![Object::new()],
119114
}
@@ -122,7 +117,7 @@ impl<'g> Stack<'g> {
122117
/// Create a stack initialized with read-only `Globals`.
123118
pub fn with_globals(globals: &'g Globals) -> Self {
124119
let mut stack = Self::empty();
125-
stack.globals = globals;
120+
stack.globals = Some(globals);
126121
stack
127122
}
128123

@@ -152,7 +147,7 @@ impl<'g> Stack<'g> {
152147
return rval;
153148
}
154149
}
155-
self.globals.get(name)
150+
self.globals.and_then(|g| g.get(name))
156151
}
157152

158153
/// Recursively index into the stack.
@@ -225,23 +220,22 @@ impl<'g> Default for Stack<'g> {
225220

226221
/// Create processing context for a template.
227222
pub struct ContextBuilder<'g> {
228-
globals: &'g Globals,
223+
globals: Option<&'g Globals>,
229224
filters: sync::Arc<HashMap<&'static str, BoxedValueFilter>>,
230225
}
231226

232227
impl<'g> ContextBuilder<'g> {
233228
/// Creates a new, empty rendering context.
234229
pub fn new() -> Self {
235-
let empty: &Object = &*EMPTY_GLOBALS;
236230
Self {
237-
globals: empty,
231+
globals: None,
238232
filters: Default::default(),
239233
}
240234
}
241235

242236
/// Initialize the stack with the given globals.
243237
pub fn set_globals(mut self, values: &'g Globals) -> Self {
244-
self.globals = values;
238+
self.globals = Some(values);
245239
self
246240
}
247241

@@ -256,8 +250,12 @@ impl<'g> ContextBuilder<'g> {
256250

257251
/// Create the `Context`.
258252
pub fn build(self) -> Context<'g> {
253+
let stack = match self.globals {
254+
Some(globals) => Stack::with_globals(globals),
255+
None => Stack::empty(),
256+
};
259257
Context {
260-
stack: Stack::with_globals(self.globals),
258+
stack,
261259
interrupt: InterruptState::default(),
262260
cycles: CycleStateInner::default(),
263261
filters: self.filters,

liquid-interpreter/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
#![warn(unreachable_pub)]
55
#![warn(unused_extern_crates)]
66

7-
#[macro_use]
8-
extern crate lazy_static;
97
extern crate itertools;
108
extern crate liquid_error;
119
extern crate liquid_value;

0 commit comments

Comments
 (0)