Skip to content

Commit b7edca6

Browse files
authored
Eliminate allocs in vector-based TimeDependentSums (#171)
* Eliminate allocs in vector-based tdops * avoid allocs with bigger tuples
1 parent 7c112ac commit b7edca6

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "QuantumOpticsBase"
22
uuid = "4f57444f-1401-5e15-980d-4471b28d5678"
3-
version = "0.5.0"
3+
version = "0.5.1"
44

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

src/time_dependent_operator.jl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ function set_time!(o::TimeDependentSum, t::Number)
160160
o.current_time = t
161161
update_static_coefficients!(static_operator(o), coefficients(o), t)
162162
end
163-
set_time!.(suboperators(o), t)
163+
# foreach is type-stable for tuples and concrete-typed vectors
164+
foreach(o -> set_time!(o, t), suboperators(o))
164165
o
165166
end
166167

@@ -291,13 +292,15 @@ end
291292

292293
@inline eval_coefficients(coeffs::Tuple, t::Number) = map(c->eval_coefficient(c, t), coeffs)
293294

294-
# This is the performance-critical implementation.
295-
# To avoid allocations in most cases, we model this on map(f, t::Tuple).
296-
@inline eval_coefficients(::Type{T}, coeffs::Tuple{Any,}, t::Number) where T = (T(eval_coefficient(coeffs[1], t)),)
297-
@inline eval_coefficients(::Type{T}, coeffs::Tuple{Any, Any}, t::Number) where T = (T(eval_coefficient(coeffs[1], t)), T(eval_coefficient(coeffs[2], t)))
298-
@inline eval_coefficients(::Type{T}, coeffs::Tuple{Any, Any, Any}, t::Number) where T = (T(eval_coefficient(coeffs[1], t)), T(eval_coefficient(coeffs[2], t)), T(eval_coefficient(coeffs[3], t)))
299-
@inline eval_coefficients(::Type{T}, coeffs::Tuple, t::Number) where T = (T(eval_coefficient(coeffs[1], t)), eval_coefficients(T, Base.tail(coeffs), t)...)
295+
# This is the performance-critical implementation. map(f, ::Tuple) avoids allocs in most cases
296+
@inline eval_coefficients(::Type{T}, coeffs::Tuple, t::Number) where T = map(c->T(eval_coefficient(c, t)), coeffs)
300297

298+
# Now just using map here instead. Maybe restore this in case of regressions.
299+
## To avoid allocations in most cases, we model this on map(f, t::Tuple).
300+
#@inline eval_coefficients(::Type{T}, coeffs::Tuple{Any,}, t::Number) where T = (T(eval_coefficient(coeffs[1], t)),)
301+
#@inline eval_coefficients(::Type{T}, coeffs::Tuple{Any, Any}, t::Number) where T = (T(eval_coefficient(coeffs[1], t)), T(eval_coefficient(coeffs[2], t)))
302+
#@inline eval_coefficients(::Type{T}, coeffs::Tuple{Any, Any, Any}, t::Number) where T = (T(eval_coefficient(coeffs[1], t)), T(eval_coefficient(coeffs[2], t)), T(eval_coefficient(coeffs[3], t)))
303+
#@inline eval_coefficients(::Type{T}, coeffs::Tuple, t::Number) where T = (T(eval_coefficient(coeffs[1], t)), eval_coefficients(T, Base.tail(coeffs), t)...)
301304

302305
_timeshift_coeff(coeff, t0) = (@inline shifted_coeff(t) = coeff(t-t0))
303306
_timeshift_coeff(coeff::Number, _) = coeff

0 commit comments

Comments
 (0)