Skip to content

Commit

Permalink
improve construction and view performance (#224)
Browse files Browse the repository at this point in the history
* improve construction performance

* check bounds only once in view()

* add test_throws

* bump version
  • Loading branch information
aplavin authored May 1, 2022
1 parent 7273bf7 commit 750cbfd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "StructArrays"
uuid = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
version = "0.6.5"
version = "0.6.6"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
9 changes: 5 additions & 4 deletions src/structarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ struct StructArray{T, N, C<:Tup, I} <: AbstractArray{T, N}

function StructArray{T, N, C}(c) where {T, N, C<:Tup}
if length(c) > 0
ax = axes(c[1])
ax = axes(first(c))
length(ax) == N || error("wrong number of dimensions")
for i = 2:length(c)
axes(c[i]) == ax || error("all field arrays must have same shape")
map(tail(c)) do ci
axes(ci) == ax || error("all field arrays must have same shape")
end
end
new{T, N, C, index_type(c)}(c)
Expand Down Expand Up @@ -347,7 +347,8 @@ Base.@propagate_inbounds function Base.getindex(x::StructArray{T, <:Any, <:Any,
end

@inline function Base.view(s::StructArray{T, N, C}, I...) where {T, N, C}
StructArray{T}(map(v -> view(v, I...), components(s)))
@boundscheck checkbounds(s, I...)
StructArray{T}(map(v -> @inbounds(view(v, I...)), components(s)))
end

Base.@propagate_inbounds function Base.setindex!(s::StructArray{<:Any, <:Any, <:Any, CartesianIndex{N}}, vals, I::Vararg{Int, N}) where {N}
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ end
@test pop!(t) == (1, 2.0)
@test getproperty(t, 1) == [2]
@test getproperty(t, 2) == [3.0]

@test_throws ErrorException StructArray(([1, 2], [3]))
end

@testset "constructor from slices" begin
Expand Down Expand Up @@ -350,6 +352,8 @@ end
t2 = @inferred f2()
@test t1 == StructArray((a=[1.2], b=["test"]))
@test t2 == StructArray{Pair{Float64, String}}(([1.2], ["test"]))

@test_throws ErrorException StructArray(a=[1, 2], b=[3])
end

@testset "complex" begin
Expand Down

2 comments on commit 750cbfd

@piever
Copy link
Collaborator

@piever piever commented on 750cbfd May 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/59453

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.6 -m "<description of version>" 750cbfd60daa487ab6be25dbd5e7ae62af8e7182
git push origin v0.6.6

Please sign in to comment.