Commit 652bdf7
authored
perf: skip assert super-chain walk for assert-free objects (#698)
## Motivation
When accessing fields on a `Val.Obj`, the evaluator walks the
super-chain to trigger all `assert` statements. For objects without any
assertions (the common case), this walk is wasted work — iterating
through potentially deep inheritance chains for no effect.
## Key Design Decision
Add a `hasAsserts` flag to `Val.Obj` that tracks whether the object or
any of its supers contain assertions. Skip the super-chain walk entirely
when `hasAsserts == false`.
## Modification
- **`Val.scala`**: Added `hasAsserts` flag, propagated through object
merge (`+`)
- **`Evaluator.scala`**: Guard assert walk with `hasAsserts` check
- **Test**: Added test for assert-free objects and objects with asserts
to verify both paths
## Benchmark Results
### JMH (JVM, 3 iterations)
| Benchmark | Master (ms/op) | This PR (ms/op) | Change |
|-----------|---------------|-----------------|--------|
| bench.02 | 50.427 ± 38.906 | 47.337 ± 2.780 | **-6.1%** |
| **comparison2** | **85.854 ± 188.657** | **67.984 ± 33.584** |
**-20.8%** 🔥 |
| **realistic2** | **73.458 ± 66.747** | **67.857 ± 11.936** | **-7.6%**
✅ |
## Analysis
- **comparison2**: Largest improvement — this benchmark creates many
small objects without assertions, so skipping the assert walk saves
significant time
- **realistic2**: Also benefits from reduced per-field-access overhead
- Safe optimization: objects with assertions still execute them
correctly
- No regressions
## References
- Upstream exploration: `he-pin/sjsonnet` jit branch commit `85ca07ca`
## Result
-7% to -21% JVM improvement by skipping unnecessary assert super-chain
walks for assertion-free objects.1 parent 2fed190 commit 652bdf7
1 file changed
Lines changed: 7 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
459 | 459 | | |
460 | 460 | | |
461 | 461 | | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
462 | 467 | | |
463 | 468 | | |
464 | 469 | | |
| |||
485 | 490 | | |
486 | 491 | | |
487 | 492 | | |
488 | | - | |
489 | | - | |
490 | | - | |
| 493 | + | |
| 494 | + | |
491 | 495 | | |
492 | 496 | | |
493 | 497 | | |
| |||
0 commit comments