Skip to content

Proposal: Change Array::chunks and chunk_by to Return Views #2785

@quake

Description

@quake

The current implementations of Array::chunks and Array::chunk_by create a new Array[T] for each chunk. This has two main drawbacks:

  1. 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.

  2. 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 the ArrayView.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions