Skip to content

Commit

Permalink
Eliminate allocs in vector-based TimeDependentSums (#171)
Browse files Browse the repository at this point in the history
* Eliminate allocs in vector-based tdops

* avoid allocs with bigger tuples
  • Loading branch information
amilsted authored Jul 9, 2024
1 parent 7c112ac commit b7edca6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "QuantumOpticsBase"
uuid = "4f57444f-1401-5e15-980d-4471b28d5678"
version = "0.5.0"
version = "0.5.1"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
17 changes: 10 additions & 7 deletions src/time_dependent_operator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ function set_time!(o::TimeDependentSum, t::Number)
o.current_time = t
update_static_coefficients!(static_operator(o), coefficients(o), t)
end
set_time!.(suboperators(o), t)
# foreach is type-stable for tuples and concrete-typed vectors
foreach(o -> set_time!(o, t), suboperators(o))
o
end

Expand Down Expand Up @@ -291,13 +292,15 @@ end

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

# This is the performance-critical implementation.
# To avoid allocations in most cases, we model this on map(f, t::Tuple).
@inline eval_coefficients(::Type{T}, coeffs::Tuple{Any,}, t::Number) where T = (T(eval_coefficient(coeffs[1], t)),)
@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)))
@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)))
@inline eval_coefficients(::Type{T}, coeffs::Tuple, t::Number) where T = (T(eval_coefficient(coeffs[1], t)), eval_coefficients(T, Base.tail(coeffs), t)...)
# This is the performance-critical implementation. map(f, ::Tuple) avoids allocs in most cases
@inline eval_coefficients(::Type{T}, coeffs::Tuple, t::Number) where T = map(c->T(eval_coefficient(c, t)), coeffs)

# Now just using map here instead. Maybe restore this in case of regressions.
## To avoid allocations in most cases, we model this on map(f, t::Tuple).
#@inline eval_coefficients(::Type{T}, coeffs::Tuple{Any,}, t::Number) where T = (T(eval_coefficient(coeffs[1], t)),)
#@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)))
#@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)))
#@inline eval_coefficients(::Type{T}, coeffs::Tuple, t::Number) where T = (T(eval_coefficient(coeffs[1], t)), eval_coefficients(T, Base.tail(coeffs), t)...)

_timeshift_coeff(coeff, t0) = (@inline shifted_coeff(t) = coeff(t-t0))
_timeshift_coeff(coeff::Number, _) = coeff
Expand Down

2 comments on commit b7edca6

@amilsted
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/110768

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.1 -m "<description of version>" b7edca6bd9caf5c089f8342325fe7e467aa512d5
git push origin v0.5.1

Please sign in to comment.