Commit 9c6debb
authored
perf: single-pass std.sum/std.avg without intermediate array (#782)
## Summary
- Replace 3-pass implementation (`forall` validate →
`.map(_.value.asDouble)` allocate intermediate `Array[Double]` → `.sum`
accumulate) with a single while-loop that validates + accumulates in one
pass
- Eliminates intermediate `Array[Double]` allocation entirely
- Same pattern applied to both `std.sum` and `std.avg`
## Changes
- `ArrayModule.scala`: Rewrite `std.sum` and `std.avg` to use
single-pass while-loop with pattern matching for validation +
accumulation
## Test plan
- [x] `./mill 'sjsonnet.jvm[3.3.7]'.test` — all tests pass
- [x] `./mill 'sjsonnet.jvm[3.3.7]'.compile` — compiles clean
- [x] Error messages preserved: `std.sum expected number` / `Cannot
calculate average of an empty array`1 parent 4123ac3 commit 9c6debb
1 file changed
Lines changed: 22 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
736 | 736 | | |
737 | 737 | | |
738 | 738 | | |
739 | | - | |
740 | | - | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
741 | 748 | | |
742 | | - | |
| 749 | + | |
743 | 750 | | |
744 | 751 | | |
745 | | - | |
746 | | - | |
747 | | - | |
748 | | - | |
| 752 | + | |
| 753 | + | |
749 | 754 | | |
750 | 755 | | |
751 | | - | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
752 | 766 | | |
753 | 767 | | |
754 | 768 | | |
0 commit comments