Skip to content

Commit a286286

Browse files
committed
Slightly faster construction
1 parent 19dcfb1 commit a286286

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-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: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -143,48 +143,45 @@ 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)[2]
159156
end
160-
return (data, ViewAxis(last_index(last_val) .+ (1:len), (;kvs...)))
157+
return (data, ViewAxis(last_index(last_val) .+ (1:len), kvs))
161158
end
162-
function make_idx(data, pair::Pair, last_val)
163-
data, ax = make_idx(data, pair.second, last_val)
159+
function build_data!(data, pair::Pair, last_val)
160+
data, ax = build_data!(data, pair.second, last_val)
164161
return (data, ViewAxis(last_val:(last_val+len-1), Axis(pair.second)))
165162
end
166-
make_idx(data, x, last_val) = (
163+
build_data!(data, x, last_val) = (
167164
push!(data, x),
168165
ViewAxis(last_index(last_val) + 1)
169166
)
170-
make_idx(data, x::ComponentVector, last_val) = (
171-
pushcat!(data, x),
167+
build_data!(data, x::ComponentVector, last_val) = (
168+
append!(data, x),
172169
ViewAxis(
173170
last_index(last_val) .+ (1:length(x)),
174171
getaxes(x)[1]
175172
)
176173
)
177-
function make_idx(data, x::AbstractArray, last_val)
178-
pushcat!(data, x)
174+
function build_data!(data, x::AbstractArray, last_val)
175+
append!(data, x)
179176
out = last_index(last_val) .+ (1:length(x))
180177
return (data, ViewAxis(out, ShapedAxis(size(x))))
181178
end
182-
function make_idx(data, x::A, last_val) where {A<:AbstractArray{<:Union{NamedTuple, ComponentArray}}}
179+
function build_data!(data, x::A, last_val) where {A<:AbstractArray{<:Union{NamedTuple, ComponentArray}}}
183180
len = recursive_length(x)
184181
if eltype(x) |> isconcretetype
185182
out = ()
186183
for elem in x
187-
(_,out) = make_idx(data, elem, last_val)
184+
(_,out) = build_data!(data, elem, last_val)
188185
end
189186
return (
190187
data,
@@ -200,7 +197,7 @@ function make_idx(data, x::A, last_val) where {A<:AbstractArray{<:Union{NamedTup
200197
error("Only homogeneous arrays of inner ComponentArrays are allowed.")
201198
end
202199
end
203-
function make_idx(data, x::A, last_val) where {A<:AbstractArray{<:AbstractArray}}
200+
function build_data!(data, x::A, last_val) where {A<:AbstractArray{<:AbstractArray}}
204201
error("ComponentArrays cannot currently contain arrays of arrays as elements. This one contains: \n $x\n")
205202
end
206203

@@ -209,7 +206,7 @@ end
209206
_maybe_add_field(x, pair) = haskey(x, pair.first) ? _update_field(x, pair) : _add_field(x, pair)
210207
function _add_field(x, pair)
211208
data = copy(getdata(x))
212-
new_data, new_ax = make_idx(data, pair.second, length(data))
209+
new_data, new_ax = build_data!(data, pair.second, length(data))
213210
new_ax = Axis(NamedTuple{tuple(pair.first)}(tuple(new_ax)))
214211
new_ax = merge(getaxes(x)[1], new_ax)
215212
return ComponentArray(new_data, new_ax)
@@ -220,8 +217,6 @@ function _update_field(x, pair)
220217
return x_copy
221218
end
222219

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

0 commit comments

Comments
 (0)