Skip to content

Commit 7d9f5d5

Browse files
giordanonsajko
andauthored
Automatically skip boundschecks in setindex! when safe to do so (#68)
* Automatically skip boundschecks in `setindex!` when safe to do so * Update src/FixedSizeArrays.jl Co-authored-by: Neven Sajko <s@purelymail.com> --------- Co-authored-by: Neven Sajko <s@purelymail.com>
1 parent f62e401 commit 7d9f5d5

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/FixedSizeArrays.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ end
5555

5656
Base.IndexStyle(::Type{<:FixedSizeArray}) = IndexLinear()
5757
Base.@propagate_inbounds Base.getindex(A::FixedSizeArray, i::Int) = A.mem[i]
58-
Base.@propagate_inbounds Base.setindex!(A::FixedSizeArray, v, i::Int) = A.mem[i] = v
58+
Base.@propagate_inbounds Base.@assume_effects :noub_if_noinbounds function Base.setindex!(A::FixedSizeArray{T}, x, i::Int) where {T}
59+
@boundscheck checkbounds(A, i)
60+
@inbounds A.mem[i] = x
61+
return A
62+
end
5963

6064
Base.size(a::FixedSizeArray) = a.size
6165

test/runtests.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ end
185185
end
186186
end
187187

188+
@testset "setindex!" begin
189+
v = FSV{Float64}(undef, 2)
190+
v[1] = 1
191+
v[2] = 2
192+
@test v[1] == 1
193+
@test v[2] == 2
194+
@test_throws BoundsError v[3] = 3
195+
end
196+
188197
@testset "FixedSizeVector" begin
189198
v = FSV{Float64}(undef, 3)
190199
@test length(v) == 3

0 commit comments

Comments
 (0)