Skip to content

Commit 750cbfd

Browse files
authored
improve construction and view performance (#224)
* improve construction performance * check bounds only once in view() * add test_throws * bump version
1 parent 7273bf7 commit 750cbfd

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "StructArrays"
22
uuid = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
3-
version = "0.6.5"
3+
version = "0.6.6"
44

55
[deps]
66
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

src/structarray.jl

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ struct StructArray{T, N, C<:Tup, I} <: AbstractArray{T, N}
1515

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

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

353354
Base.@propagate_inbounds function Base.setindex!(s::StructArray{<:Any, <:Any, <:Any, CartesianIndex{N}}, vals, I::Vararg{Int, N}) where {N}

test/runtests.jl

+4
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,8 @@ end
312312
@test pop!(t) == (1, 2.0)
313313
@test getproperty(t, 1) == [2]
314314
@test getproperty(t, 2) == [3.0]
315+
316+
@test_throws ErrorException StructArray(([1, 2], [3]))
315317
end
316318

317319
@testset "constructor from slices" begin
@@ -350,6 +352,8 @@ end
350352
t2 = @inferred f2()
351353
@test t1 == StructArray((a=[1.2], b=["test"]))
352354
@test t2 == StructArray{Pair{Float64, String}}(([1.2], ["test"]))
355+
356+
@test_throws ErrorException StructArray(a=[1, 2], b=[3])
353357
end
354358

355359
@testset "complex" begin

0 commit comments

Comments
 (0)