Skip to content

Commit 487bd83

Browse files
committed
Slightly faster construction
1 parent 19dcfb1 commit 487bd83

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ComponentArrays"
22
uuid = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66"
33
authors = ["Jonnie Diegelman <47193959+jonniedie@users.noreply.github.com>"]
4-
version = "0.12.2"
4+
version = "0.12.3"
55

66
[deps]
77
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"

src/componentarray.jl

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -143,48 +143,46 @@ function make_carray_args(nt)
143143
end
144144
make_carray_args(::Type{T}, nt) where {T} = make_carray_args(Vector{T}, nt)
145145
function make_carray_args(A::Type{<:AbstractArray}, nt)
146-
data, idx = make_idx([], nt, 0)
146+
data, idx = build_data!([], nt, 0)
147147
return (A(data), Axis(idx))
148148
end
149149

150150
# Builds up data vector and returns appropriate AbstractAxis type for each input type
151-
function make_idx(data, nt::NamedTuple, last_val)
151+
function build_data!(data, nt::NamedTuple, last_val)
152152
len = recursive_length(nt)
153-
kvs = []
154153
lv = 0
155-
for (k,v) in zip(keys(nt), values(nt))
156-
(_,val) = make_idx(data, v, lv)
157-
push!(kvs, k => val)
158-
lv = val
154+
kvs = map(nt) do v
155+
(_, lv) = build_data!(data, v, lv)
156+
lv
159157
end
160-
return (data, ViewAxis(last_index(last_val) .+ (1:len), (;kvs...)))
158+
return (data, ViewAxis(last_index(last_val) .+ (1:len), kvs))
161159
end
162-
function make_idx(data, pair::Pair, last_val)
163-
data, ax = make_idx(data, pair.second, last_val)
160+
function build_data!(data, pair::Pair, last_val)
161+
data, ax = build_data!(data, pair.second, last_val)
164162
return (data, ViewAxis(last_val:(last_val+len-1), Axis(pair.second)))
165163
end
166-
make_idx(data, x, last_val) = (
164+
build_data!(data, x, last_val) = (
167165
push!(data, x),
168166
ViewAxis(last_index(last_val) + 1)
169167
)
170-
make_idx(data, x::ComponentVector, last_val) = (
171-
pushcat!(data, x),
168+
build_data!(data, x::ComponentVector, last_val) = (
169+
append!(data, x),
172170
ViewAxis(
173171
last_index(last_val) .+ (1:length(x)),
174172
getaxes(x)[1]
175173
)
176174
)
177-
function make_idx(data, x::AbstractArray, last_val)
178-
pushcat!(data, x)
175+
function build_data!(data, x::AbstractArray, last_val)
176+
append!(data, x)
179177
out = last_index(last_val) .+ (1:length(x))
180178
return (data, ViewAxis(out, ShapedAxis(size(x))))
181179
end
182-
function make_idx(data, x::A, last_val) where {A<:AbstractArray{<:Union{NamedTuple, ComponentArray}}}
180+
function build_data!(data, x::A, last_val) where {A<:AbstractArray{<:Union{NamedTuple, ComponentArray}}}
183181
len = recursive_length(x)
184182
if eltype(x) |> isconcretetype
185183
out = ()
186184
for elem in x
187-
(_,out) = make_idx(data, elem, last_val)
185+
(_,out) = build_data!(data, elem, last_val)
188186
end
189187
return (
190188
data,
@@ -200,7 +198,7 @@ function make_idx(data, x::A, last_val) where {A<:AbstractArray{<:Union{NamedTup
200198
error("Only homogeneous arrays of inner ComponentArrays are allowed.")
201199
end
202200
end
203-
function make_idx(data, x::A, last_val) where {A<:AbstractArray{<:AbstractArray}}
201+
function build_data!(data, x::A, last_val) where {A<:AbstractArray{<:AbstractArray}}
204202
error("ComponentArrays cannot currently contain arrays of arrays as elements. This one contains: \n $x\n")
205203
end
206204

@@ -209,7 +207,7 @@ end
209207
_maybe_add_field(x, pair) = haskey(x, pair.first) ? _update_field(x, pair) : _add_field(x, pair)
210208
function _add_field(x, pair)
211209
data = copy(getdata(x))
212-
new_data, new_ax = make_idx(data, pair.second, length(data))
210+
new_data, new_ax = build_data!(data, pair.second, length(data))
213211
new_ax = Axis(NamedTuple{tuple(pair.first)}(tuple(new_ax)))
214212
new_ax = merge(getaxes(x)[1], new_ax)
215213
return ComponentArray(new_data, new_ax)
@@ -220,8 +218,6 @@ function _update_field(x, pair)
220218
return x_copy
221219
end
222220

223-
pushcat!(a, b) = reduce((x1,x2) -> push!(x1,x2), b; init=a)
224-
225221
# Reshape ComponentArrays with ShapedAxis axes
226222
maybe_reshape(data, ::NotShapedOrPartitionedAxis...) = data
227223
function maybe_reshape(data, axs::AbstractAxis...)

0 commit comments

Comments
 (0)