JuliaArrays / JuliaArrays/StaticArrays.jl

Extending setindex for vector indexing

Open
#764 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

arrays
Dominant language
Julia
Stars
844
Forks
159
Avg merge
3d 21h
Merged PRs (30d)
3

Description

Learning from my experiments on #745 i looked back at the missing support for vector indexing in setindex.

@inline function setindex(a::SVector{L}, v::AbstractArray, indices::AbstractArray) where L
    out = MVector(a)
    for (i, x) in zip(indices, v)
        # Do boundscheck here to allow Julia to fully elide the dynamic allocation.
        @boundscheck if (i < 1 || i > L)
            throw(BoundsError(a, i))
        end
        @inbounds out[i] = x
    end
    return SVector(out)
end

while we're at it, looks like setindex for scalars can easily be written this way as well.

@inline function setindex2(a::SVector{L}, x, index::Integer) where L
    out = MVector(a)
    # Do boundscheck here to allow Julia to fully elide the dynamic allocation.
    @boundscheck if (index < 1 || index > L)
        throw(BoundsError(a, index))
    end
    @inbounds out[index] = x
    return SVector(out)
end

Looking at assembly, it looks nicer for this implementation. I have had some mixed results trying to benchmark this; peaking at @code_native looked very efficient, but benchmark times still fell a bit short. Perhaps my benchmarks were lacking, or i missed something.

So is this interesting?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the existing setindex methods for SVector and review the related discussion in issue #745. Compare vector and scalar indexing behavior, including the shown bounds checks, then use assembly inspection and benchmarks to verify the intended implementation. Done means both forms work with correct bounds errors and their performance is validated.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
performance
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.