Skip to content

Commit 848bf41

Browse files
committed
perf: avoid boxing in update_tuple_of_buffers
1 parent baa928c commit 848bf41

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/systems/parameter_buffer.jl

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ function buffer_to_arraypartition(buf)
141141
v for v in buf))
142142
end
143143

144-
_split_helper(buf_v::T, recurse, raw, idx) where {T} = _split_helper(eltype(T), buf_v, recurse, raw, idx)
144+
function _split_helper(buf_v::T, recurse, raw, idx) where {T}
145+
_split_helper(eltype(T), buf_v, recurse, raw, idx)
146+
end
145147

146148
function _split_helper(::Type{<:AbstractArray}, buf_v, ::Val{true}, raw, idx)
147149
map(b -> _split_helper(eltype(b), b, Val(false), raw, idx), buf_v)
@@ -159,21 +161,25 @@ end
159161

160162
function split_into_buffers(raw::AbstractArray, buf, recurse = Val(true))
161163
idx = Ref(1)
162-
ntuple(i->_split_helper(buf[i], recurse, raw, idx), Val(length(buf)))
164+
ntuple(i -> _split_helper(buf[i], recurse, raw, idx), Val(length(buf)))
165+
end
166+
167+
function _update_tuple_helper(buf_v::T, raw, idx) where {T}
168+
_update_tuple_helper(eltype(T), buf_v, raw, idx)
163169
end
170+
171+
function _update_tuple_helper(::Type{<:AbstractArray}, buf_v, raw, idx)
172+
map(b -> _update_tuple_helper(b, raw, idx), buf_v)
173+
end
174+
175+
function _update_tuple_helper(::Any, buf_v, raw, idx)
176+
copyto!(buf_v, view(raw, idx[]:(idx[] + length(buf_v) - 1)))
177+
idx[] += length(buf_v)
164178
end
165179

166180
function update_tuple_of_buffers(raw::AbstractArray, buf)
167-
idx = 1
168-
function _helper(buf_v)
169-
if eltype(buf_v) <: AbstractArray
170-
_helper.(buf_v)
171-
else
172-
copyto!(buf_v, view(raw, idx:(idx + length(buf_v) - 1)))
173-
idx += length(buf_v)
174-
end
175-
end
176-
_helper.(buf)
181+
idx = Ref(1)
182+
map(b -> _update_tuple_helper(b, raw, idx), buf)
177183
end
178184

179185
SciMLStructures.isscimlstructure(::MTKParameters) = true

0 commit comments

Comments
 (0)