Skip to content

perf: Val.Arr arraycopy concat and while-loop asStrictArray#696

Merged
stephenamar-db merged 1 commit intodatabricks:masterfrom
He-Pin:perf/arr-concat-arraycopy
Apr 8, 2026
Merged

perf: Val.Arr arraycopy concat and while-loop asStrictArray#696
stephenamar-db merged 1 commit intodatabricks:masterfrom
He-Pin:perf/arr-concat-arraycopy

Conversation

@He-Pin
Copy link
Copy Markdown
Contributor

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

Motivation

Array concatenation (+ on arrays) and asStrictArray currently use functional/iterator patterns that allocate intermediate collections unnecessarily.

Key Design Decision

Use System.arraycopy for array concatenation (zero-copy bulk memory transfer) and convert asStrictArray from functional iteration to a while-loop to avoid closure and iterator allocation.

Modification

  • Replace array concat with System.arraycopy-based implementation
  • Convert asStrictArray to while-loop iteration
  • ~25 lines changed total

Benchmark Results

JMH (JVM, 3 iterations warmup + 3 measurement)

Benchmark Master (ms/op) This PR (ms/op) Change
bench.02 50.427 ± 38.9 44.581 ± 4.3 -11.6%
comparison2 85.854 ± 188.7 69.051 ± 8.6 -19.6%
realistic2 73.458 ± 66.7 66.889 ± 5.1 -8.9%

Analysis

The comparison2 improvement (-19.6%) reflects the heavy array usage in that benchmark. System.arraycopy is a JVM intrinsic that compiles to native memcpy — always faster than element-by-element copy through iterators. The while-loop conversion eliminates closure allocation in tight loops.

References

  • Upstream: jit branch experiment

Result

All 23 tests pass. All benchmarks positive, no regressions.

Replace Scala ArrayOps '++' in Val.Arr.concat with direct System.arraycopy,
avoiding ClassTag resolution and ArrayBuilder overhead. Replace arr.map(_.value)
in asStrictArray with an explicit while-loop to avoid closure allocation.

Redirect OP_+ array path to use the optimized concat method instead of
asLazyArray ++ rArr.asLazyArray.

Upstream: jit branch commit ffc34c2
@He-Pin He-Pin marked this pull request as ready for review April 6, 2026 10:41
@stephenamar-db stephenamar-db merged commit 2fed190 into databricks:master Apr 8, 2026
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