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

Improve type inference #308

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/fft.jl
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,9 @@
# This is accomplished by the maybe_destroy_plan function, which is used as the plan finalizer.

# these functions should only be called while the fftwlock is held
unsafe_destroy_plan(plan::FFTWPlan{<:fftwDouble}) =
unsafe_destroy_plan(@nospecialize(plan::FFTWPlan{<:fftwDouble})) =
stevengj marked this conversation as resolved.
Show resolved Hide resolved
ccall((:fftw_destroy_plan,libfftw3[]), Cvoid, (PlanPtr,), plan)
unsafe_destroy_plan(plan::FFTWPlan{<:fftwSingle}) =
unsafe_destroy_plan(@nospecialize(plan::FFTWPlan{<:fftwSingle})) =
ccall((:fftwf_destroy_plan,libfftw3f[]), Cvoid, (PlanPtr,), plan)

const deferred_destroy_lock = ReentrantLock() # lock protecting the deferred_destroy_plans list
Expand All @@ -335,7 +335,12 @@
# we'll do nothing (the other function will eventually run destroy_deferred).
if !isempty(deferred_destroy_plans) && trylock(fftwlock)
try
foreach(unsafe_destroy_plan, deferred_destroy_plans)
@static if Base.VERSION >= v"1.9"
@inline foreach(unsafe_destroy_plan, deferred_destroy_plans)

Check warning on line 339 in src/fft.jl

View check run for this annotation

Codecov / codecov/patch

src/fft.jl#L338-L339

Added lines #L338 - L339 were not covered by tests
Copy link
Member

Choose a reason for hiding this comment

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

Why is inlining needed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The core issue is that foreach in Base does not specialize on its function argument: https://github.com/JuliaLang/julia/blob/a007e807623f0bbb820315b8ce3340bd3d41262b/base/abstractarray.jl#L3213-L3214

This is missing something like foreach(f::F, ...) where F which would cause this to specialize better - otherwise as-is it contains a dynamic dispatch. Arguably that should be fixed upstream, but this improves things for released versions of Julia too

else
# call-site @inline isn't supported on old versions of Julia
foreach(unsafe_destroy_plan, deferred_destroy_plans)

Check warning on line 342 in src/fft.jl

View check run for this annotation

Codecov / codecov/patch

src/fft.jl#L342

Added line #L342 was not covered by tests
end
empty!(deferred_destroy_plans)
finally
unlock(fftwlock)
Expand Down
Loading