Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid redundant ifs (using generated functions?) #362

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

PerezHz
Copy link
Contributor

@PerezHz PerezHz commented Jul 9, 2024

Currently, there are some patterns in the code which go along the lines of

for T in (:Taylor1, :TaylorN)
    # NOTE: For $T = TaylorN, `mul!` *accumulates* the result of a * b in c[k]
    @eval @inline function mul!(c::$T{T}, a::$T{T}, b::$T{T}, k::Int) where {T<:Number}
        if $T == Taylor1
            @inbounds c[k] = a[0] * b[k]
            @inbounds for i = 1:k
                c[k] += a[i] * b[k-i]
            end
        else
            @inbounds mul!(c[k], a[0], b[k])
            @inbounds for i = 1:k
                mul!(c[k], a[i], b[k-i])
            end
        end
        return nothing
    end
# ...
end

(these lines can be be found around here:

if $T == Taylor1
)

I gather such patterns avoid code repetitions, while generally the "cost" of running these ifs is quite low. Just for the sake of confirming this, I experimented a bit with generated functions, which allow to preserve the specialization for each type while avoiding code repetition, with the advantage of resolving them at compilation time, instead of being checked during run time once per method call. This can become important when handling mixtures of Taylor1{TaylorN{T}} in TaylorIntegration, where the number of coefficients can be quite large, and these if's can be checked many times during run time, when it's only necessary to check them at dispatch.

Gotten to this point, though, maybe an equally good solution would be to simply separate Taylor1 and TaylorN methods for patterns such as above? The latter can be more friendly to develop, and would avoid the use of generated functions, which looks maybe a bit overkill here?

So essentially what I'm proposing is to either remove the if $T == Taylor1 ... in the way they are now, or keep them by using generated functions, such that performance can improve a bit. Benchmarks I've run with TaylorIntegration on JT integrations show ~5% performance improvement. Not much indeed, but can be meaningful for longer runs.

@PerezHz PerezHz changed the title Avoid a lot of ifs (using generated functions?) Avoid redundant ifs (using generated functions?) Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant