Skip to content

perf: fuse chained std.map calls into ComposedMappedArr#902

Open
He-Pin wants to merge 2 commits into
databricks:masterfrom
He-Pin:perf/map-map-view-fusion
Open

perf: fuse chained std.map calls into ComposedMappedArr#902
He-Pin wants to merge 2 commits into
databricks:masterfrom
He-Pin:perf/map-map-view-fusion

Conversation

@He-Pin
Copy link
Copy Markdown
Contributor

@He-Pin He-Pin commented Jun 6, 2026

Motivation

When std.map(f, std.map(g, arr)) is evaluated, two MappedArr views are nested. Each element access traverses two cache layers and two levels of indirection. This pattern is common in Jsonnet when splitting transformation logic across multiple map calls for readability.

Modification

In Arr.mapped(), detect when the source is already a MappedArr with live state and create a ComposedMappedArr that applies both functions in sequence from the original source array. This eliminates one cache layer and one level of indirection per element access.

Key design decisions:

  • Store separate outerCallPos and innerCallPos for correct error stack frames
  • Guard on inner.source != null to avoid using a fully-consumed (released) inner view
  • Caching is inherited from LazyViewArr — repeated access to the same index returns the cached result without recomputation
  • MappedArr fields (source, func, callPos) widened to private[Val] for extraction

Result

Native A/B (Scala Native 0.5.12, hyperfine --warmup 10 --min-runs 30):

Benchmark Baseline PR jrsonnet Delta
lazy_array_sparse_indexing 19.0 ms 18.7 ms 37.2 ms -1.4%

sjsonnet is already 2× faster than jrsonnet on this benchmark. The fusion further reduces per-element overhead.

Comprehensive test coverage added in lazy_array_views.jsonnet: full materialization, repeated indexed access, reverse, and foldl on fused chains.

References

Motivation:
When `std.map(f, std.map(g, arr))` is evaluated, two MappedArr views are
nested. Each element access traverses two cache layers and two indirection
levels. Common in Jsonnet when splitting logic across multiple map calls.

Modification:
In `Arr.mapped()`, detect when the source is already a `MappedArr` with
live state and create a `ComposedMappedArr` that applies both functions
in sequence from the original source, eliminating one cache layer and
one level of indirection per element access.

Result:
Native: lazy_array_sparse_indexing 20.5 → 19.7ms (-3.9%)
Existing test coverage: lazy_array_views.jsonnet tests chained maps.
@He-Pin He-Pin marked this pull request as draft June 6, 2026 20:29
Motivation:
ComposedMappedArr was using the outer map's callPos for both inner and
outer function calls, causing error stack frames to point to the wrong
source position when the inner function fails.

Modification:
- Store separate outerCallPos and innerCallPos
- Make MappedArr.callPos accessible as private[Val] for extraction
- Add comprehensive fused map chain tests (full materialization,
  repeated indexed access, reverse, foldl)

Result:
Correct error positions when inner function of a fused map chain throws.
@He-Pin He-Pin marked this pull request as ready for review June 6, 2026 23:24
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.

1 participant