Public repository: timo / feel

Reader

An interpreter of FEEL, written in Rust, with integrations for PostgreSQL, WASM, Lua, and Python.

perf(core): cache the lalrpop parser per thread to avoid per-parse lexer rebuild

public

perf(core): cache the lalrpop parser per thread to avoid per-parse lexer rebuild

ExprParser::new() rebuilds the lexer's regex DFA (MatcherBuilder) on every
call — a fixed ~680µs that dominated parsing. parse() takes &self, so one
parser per thread is reusable. Per-parse cost drops ~680µs -> 6-18µs; the
feel-wasm one-shot path goes from ~50-80x slower than feelin to ~1.4-2.7x
faster. Marked removable: drop once the lexer no longer pays a per-build cost.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Changeset55c3a2f82b6fac81e5b11fdf044a2d822cc8cb2d
Author (claimed)Timo Stolz <timo.stolz@gmail.com>,
Parentsb8103c6085b9
Children
Pushlog
by timo via ssh

modified crates/feel-core/src/lib.rs +12 −2

@@ -86,12 +86,22 @@
8686
     }
8787
 }
8888
 
89
+thread_local! {
90
+    // WORKAROUND (removable): lalrpop's `ExprParser::new()` rebuilds the lexer's
91
+    // regex DFA (`MatcherBuilder`) on every call — a fixed ~680µs that dominates
92
+    // parsing (the actual parse via `parser.parse(&self, _)` is cheap). Caching
93
+    // one parser per thread amortises that to once-per-thread. Drop this whole
94
+    // `thread_local!` and inline `feel::ExprParser::new()` again once the lexer
95
+    // no longer pays a per-construction cost (e.g. a logos-based lexer).
96
+    static PARSER: feel::ExprParser = feel::ExprParser::new();
97
+}
98
+
8999
 /// Parse a FEEL expression into a reusable [`CompiledExpr`] without evaluating it.
90100
 ///
91101
 /// Returns [`FeelError::Parse`] if the expression is syntactically invalid.
92102
 pub fn compile(expr: &str) -> Result<CompiledExpr, FeelError> {
93
-    feel::ExprParser::new()
94
-        .parse(expr)
103
+    PARSER
104
+        .with(|parser| parser.parse(expr))
95105
         .map(CompiledExpr)
96106
         .map_err(|e| FeelError::Parse(e.to_string()))
97107
 }