Skip to content

Reduce allocations in ToMultidimensionalArray - #3093

Open
alexzzzs wants to merge 1 commit into
JamesNK:masterfrom
alexzzzs:perf/optimize-multidimensional-array-allocations
Open

Reduce allocations in ToMultidimensionalArray#3093
alexzzzs wants to merge 1 commit into
JamesNK:masterfrom
alexzzzs:perf/optimize-multidimensional-array-allocations

Conversation

@alexzzzs

Copy link
Copy Markdown

Optimizes the ToMultidimensionalArray method to reduce memory allocations when deserializing multidimensional arrays.

The Issue

The previous implementation of CopyFromJaggedToMultidimensionalArray created a new int[] array at every step of the recursion to store indices. For large multidimensional arrays (e.g., 3D grids), this resulted in O(N) intermediate array allocations, creating unnecessary GC pressure.

The Fix

  • Replaced the recursive array creation with a single int[] buffer allocated once at the entry point.
  • This buffer is passed down the recursion stack and updated in-place.
  • Updated JaggedArrayGetValue to accept a length parameter, allowing it to read from the shared buffer without needing a trimmed copy.

- Replaced O(N) intermediate array allocations in CopyFromJaggedToMultidimensionalArray with a single reusable buffer.
- Updated JaggedArrayGetValue to support partial buffer traversal.
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