Skip to content

Commit 3d52b31

Browse files
committed
perf: speed up split_into_buffers
By avoiding boxing in this function, SciMLStructures.replace becomes inferable
1 parent c1b5d83 commit 3d52b31

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/systems/parameter_buffer.jl

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,20 @@ 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)
145+
146+
function _split_helper(::Type{<:AbstractArray}, buf_v, recurse, raw, idx)
147+
recurse ? map(b -> _split_helper(b, recurse, raw, idx), buf_v) : _split_helper(Any, b, recurse, raw, idx)
148+
end
149+
150+
function _split_helper(::Any, buf_v, recurse, raw, idx)
151+
res = reshape(raw[idx[]:(idx[] + length(buf_v) - 1)], size(buf_v))
152+
idx[] += length(buf_v)
153+
return res
154+
end
155+
144156
function split_into_buffers(raw::AbstractArray, buf; recurse = true)
145-
idx = 1
146-
function _helper(buf_v; recurse = true)
147-
if eltype(buf_v) <: AbstractArray && recurse
148-
return _helper.(buf_v; recurse = false)
149-
else
150-
res = reshape(raw[idx:(idx + length(buf_v) - 1)], size(buf_v))
151-
idx += length(buf_v)
152-
return res
153-
end
154-
end
155-
return Tuple(_helper(buf_v; recurse) for buf_v in buf)
157+
ntuple(i->_split_helper(buf[i], recurse, raw, Ref(1)), Val(length(buf)))
156158
end
157159

158160
function update_tuple_of_buffers(raw::AbstractArray, buf)

0 commit comments

Comments
 (0)