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

buffer preallocation in hcubature #47

Merged
merged 6 commits into from
Dec 16, 2022

Conversation

maltezfaria
Copy link
Contributor

@maltezfaria maltezfaria commented Dec 6, 2022

Introduce the alloc_buf function to preallocate the BinaryMaxHeap buffer used in hcubature, and adapt the signature of hcubature to accept an optional buffer keyword argument (similar to what QuadGK currently provides).

This can be helpful when calling hcubature several times on similar functions:

julia> buffer = alloc_buf(;dimension=1)
julia> @btime hcubature(x -> cos(x[1]), (0,), (1,);buffer=$buffer)
  105.399 ns (0 allocations: 0 bytes)
(0.8414709848078966, 1.1102230246251565e-16)

julia> @btime hcubature(x -> cos(x[1]), (0,), (1,))
  151.451 ns (2 allocations: 400 bytes)
(0.8414709848078966, 1.1102230246251565e-16)

For more complex integrands or higher dimensions, the gain is usually minimal (but it reduces the allocations):

julia> buffer = alloc_buf(;dimension=2, range_type=Float64, domain_type=Float64)

julia> @btime hcubature(x -> sqrt(x[1])*x[2], (0,0), (2pi, pi);buffer=$buffer)
  2.504 μs (0 allocations: 0 bytes)
(51.81413988124638, 6.910421404114884e-7)

julia> @btime hcubature(x -> sqrt(x[1])*x[2], (0,0), (2pi, pi))
  2.724 μs (3 allocations: 2.86 KiB)
(51.81413988124638, 6.910421404114884e-7)

src/HCubature.jl Outdated Show resolved Hide resolved
src/HCubature.jl Outdated Show resolved Hide resolved
@maltezfaria
Copy link
Contributor Author

Is there anything else needed for this PR? Thanks.

@stevengj stevengj merged commit dcf6314 into JuliaMath:master Dec 16, 2022
@stevengj
Copy link
Member

Thanks!

@maltezfaria
Copy link
Contributor Author

Would it be possible to tag a new minor release (1.5.2)?

@maltezfaria maltezfaria mentioned this pull request Sep 20, 2023
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.

2 participants