-
Notifications
You must be signed in to change notification settings - Fork 139
Description
The current implementations of Array::chunks
and Array::chunk_by
create a new Array[T]
for each chunk. This has two main drawbacks:
-
Performance Overhead: For each chunk, new memory must be allocated, and elements from the original array must be copied into the new array. This can be very inefficient, especially when dealing with large arrays or a large number of chunks.
-
Increased Memory Usage: Creating copies of data for each chunk leads to a significantly higher memory footprint, as the data is duplicated in memory.
By returning an Array[ArrayView[T]]
, we can adopt a zero-copy approach. ArrayView
is a lightweight structure that simply points to a slice of the original array's data without taking ownership or creating a copy.
Impact
This is a breaking change. Code that relies on the return type being Array[Array[T]]
will no longer compile. For example, code that attempts to modify the returned chunks will fail, as ArrayView
is immutable.
Migration is straightforward:
- If the user only needs to read from the chunks, no change is needed other than updating type annotations.
- If the user needs a mutable
Array[T]
for a chunk, they must now explicitly call.to_array()
on theArrayView
.