Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve construction and view performance #224

Merged
merged 4 commits into from
May 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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