Skip to content

perf: single-pass std.sum/std.avg without intermediate array#782

Merged
stephenamar-db merged 1 commit intodatabricks:masterfrom
He-Pin:perf/sum-avg-single-pass
Apr 21, 2026
Merged

perf: single-pass std.sum/std.avg without intermediate array#782
stephenamar-db merged 1 commit intodatabricks:masterfrom
He-Pin:perf/sum-avg-single-pass

Conversation

@He-Pin
Copy link
Copy Markdown
Contributor

@He-Pin He-Pin commented Apr 18, 2026

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

  • ./mill 'sjsonnet.jvm[3.3.7]'.test — all tests pass
  • ./mill 'sjsonnet.jvm[3.3.7]'.compile — compiles clean
  • Error messages preserved: std.sum expected number / Cannot calculate average of an empty array

Motivation:
std.sum and std.avg perform three passes over the input array:
forall (validate) → map (allocate intermediate Array[Double]) → sum.

Modification:
Replace with a single while-loop that validates types and accumulates
in one pass. No intermediate array allocation.

Result:
Eliminates O(n) Array[Double] allocation and two redundant iterations
for every std.sum/std.avg call.
@stephenamar-db stephenamar-db merged commit 9c6debb into databricks:master Apr 21, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants