-
-
Notifications
You must be signed in to change notification settings - Fork 78
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
Allocations in convert(Ptr{T}, Handle{T})
#435
Comments
Handle{T}
convert(Ptr{T}, Handle{T})
@vtjnash Is this a fundamental limitation of struct Handle{T}
ptr_ref::Ref{Ptr{T}}
end
Base.unsafe_convert(::Type{Ptr{T}}, h::Handle{T}) where {T} = h.ptr_ref[]
h = Handle{Int}(Ref(Ptr{Int}(0)))
@code_warntype Base.unsafe_convert(Ptr{Int}, h) Shows an
|
It is an abstract type. A mutable struct usually makes more sense, since it performs better (if you don't need to pass it to C) and allocating a new Ref makes more sense if you do need to pass to C (since it can be stack allocated) |
So if I'm understanding you correctly, you're suggesting
|
Addresses SciML#435 and also fixes a potential problem with memory safety: 1) Allocations in convert(Ptr{T}, Handle{T}) Fix is as suggested in SciML#435 (Handle is now a mutable struct with a ptr field) 2) Memory safety robustness fix Remove convert and use paired Base.cconvert / Base.unsafe_convert to get the ptr field from a Handle object h, so that the h is preserved from GC across the ccall This fix is analogous to that for the NVector wrapper in PR SciML#380 (NB: there is no actual problem at least when Sundials is used with the SciML interface, as the Handle{CVODEMem} and similar objects are held by a persistent solver data structure, but this change should reduce the risk of something going wrong in the future, or for eg test harnesses that don't use the SciML interface)
Addresses #435 and also fixes a potential problem with memory safety: 1) Allocations in convert(Ptr{T}, Handle{T}) Fix is as suggested in #435 (Handle is now a mutable struct with a ptr field) 2) Memory safety robustness fix Remove convert and use paired Base.cconvert / Base.unsafe_convert to get the ptr field from a Handle object h, so that the h is preserved from GC across the ccall This fix is analogous to that for the NVector wrapper in PR #380 (NB: there is no actual problem at least when Sundials is used with the SciML interface, as the Handle{CVODEMem} and similar objects are held by a persistent solver data structure, but this change should reduce the risk of something going wrong in the future, or for eg test harnesses that don't use the SciML interface)
We appear to be getting unnecessary allocations due to the implementation of
Handle{T}
. In particular:The
::Any
fromh.ptr_ref[]
seems to force the allocation of aBox
, and since this happens every time we ask the integrator for a state vector, it can add up to many thousands of allocations per solve, and if you're doing a large parameter sweep of simple systems, you can quickly spend more time GC'ing than solving.The text was updated successfully, but these errors were encountered: