Repository navigation
Commit 06cd0b9
authored
Reuse the cel environment built from a Context across evaluations (#51)
## Why
Every `evaluate()` and `Program.execute()` call rebuilt the cel-rust context from scratch: each variable re-boxed into a `Box<dyn Val>`, each registered Python function re-wrapped in a fresh closure. So the per-call cost scaled with the size of the Python `Context`, not the expression. The CLI, which registers all 47 extended-stdlib functions, paid ~7.5 µs of setup to evaluate `1 + 2`.
## What
`Context` now builds its cel environment on first use and caches it (`Mutex<Option<Arc<cel::Context<'static>>>>`). `add_variable`, `add_function` and `update` drop the cache. `set_variable_resolver` does not need to: the resolver is bound per call in a child scope via cel-rust's `Context::new_inner_scope`, which keeps exactly the lookup order the resolver had on the root (resolver → registered variables → type identifiers), and leaves the root untouched and shareable.
The evaluation borrows an `Arc` clone rather than the Python object, so a callback may mutate, or re-enter evaluation with, the `Context` it was registered on; the mutation applies from the next evaluation, which is also what happened before (the old code copied variables and functions out before executing).
Dict contexts are still materialised per call, because a dict can change between calls without notice.
Also in this PR, because the same code was being rewritten: the Python-function wrapper and the compile step are factored into helpers shared by `evaluate()`/`compile()`, and the panic message for an execution-time panic no longer calls itself a parser error.
## Numbers
`min` of 3 × 50 000 iterations, release build, same machine. "Before" is `main`'s Rust (built from the #44 branch, which only touches Python).
| `Program.execute()` against… | before | after |
|---|---:|---:|
| no context | 0.13 µs | 0.15 µs |
| dict, 2 vars | 0.43 µs | 0.54 µs |
| `Context`, 2 vars | 0.31 µs | 0.18 µs |
| `Context`, 2 vars + 1 Python fn (called) | 0.70 µs | 0.37 µs |
| `Context`, 2 vars + resolver | 0.43 µs | 0.24 µs |
| `Context` with the 47 extended-stdlib functions | **7.48 µs** | **0.16 µs** |
| `Context` with 200 vars | **31.85 µs** | **0.13 µs** |
The no-context and dict rows are within run-to-run noise (an alternating A/B of 7 × 100 000 iterations gave 0.13–0.14 vs 0.13–0.14 and 0.45–0.48 vs 0.42–0.48).
## Tests
New `tests/test_context_reuse.py` pins the contract: every mutator is visible on the next evaluation (including replacing a function and replacing the resolver); resolver precedence over registered variables is unchanged; a callback can mutate the context it runs under and can call `evaluate()`/`execute()` with that same context; one `Context` serves many programs; a `Context` shared across 8 threads returns correct results. 528 passed, 1 skipped, 5 xfailed. `cargo fmt --check`, `cargo clippy -D warnings`, `cargo test`, ruff and mypy clean.
Note for merging: this and #44 both add to the CHANGELOG's Unreleased section, so whichever lands second will need a trivial conflict resolution there.1 parent 2c71b41 commit 06cd0b9
5 files changed
Lines changed: 473 additions & 181 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
18 | 46 | | |
19 | 47 | | |
20 | 48 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
207 | 207 | | |
208 | 208 | | |
209 | 209 | | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
210 | 216 | | |
211 | 217 | | |
212 | 218 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
44 | | - | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
45 | 48 | | |
46 | | - | |
| 49 | + | |
| 50 | + | |
47 | 51 | | |
48 | 52 | | |
49 | 53 | | |
50 | 54 | | |
51 | 55 | | |
52 | 56 | | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
53 | 108 | | |
54 | 109 | | |
55 | 110 | | |
| |||
130 | 185 | | |
131 | 186 | | |
132 | 187 | | |
| 188 | + | |
133 | 189 | | |
134 | 190 | | |
135 | 191 | | |
| |||
208 | 264 | | |
209 | 265 | | |
210 | 266 | | |
| 267 | + | |
211 | 268 | | |
212 | 269 | | |
213 | 270 | | |
| |||
346 | 403 | | |
347 | 404 | | |
348 | 405 | | |
| 406 | + | |
349 | 407 | | |
350 | 408 | | |
351 | 409 | | |
| |||
442 | 500 | | |
443 | 501 | | |
444 | 502 | | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
445 | 506 | | |
446 | 507 | | |
447 | 508 | | |
| |||
0 commit comments