-
Notifications
You must be signed in to change notification settings - Fork 5
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
Concrete return type of BMI.get_value_ptr
#2018
Conversation
This aligns better with the updated expectations in https://github.com/Deltares/BasicModelInterface.jl/releases/tag/v0.1.1. This means we don't return `SubArrays` or similar anymore, but always a simple `Vector{Float64}`.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you comment/link on the actual issue solved here? As in, I get that returning a SubArray is bad implicitly here, but why?
core/src/util.jl
Outdated
function unsafe_array( | ||
A::SubArray{Float64, 1, Vector{Float64}, Tuple{UnitRange{Int64}}, true}, | ||
)::Vector{Float64} | ||
unsafe_wrap(Array, pointer(A), length(A)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This probably requires a GC.@preserve A
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would only be needed if A is at risk of GC right? In which case this shouldn't be called anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well I rather program defensively than making those assumptions. Worst case you don't get a crash, but silent corruption. I don't think the macro has downsides for us here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't have downsides, but I don't think it helps us here. The docs mention:
Mark the objects x1, x2, ... as being in use during the evaluation of the expression expr.
So if we return from this function this won't apply anymore I think.
I confirmed this with a little experiment:
f(a) = unsafe_wrap(Array, pointer(a), length(a))
g(a) = GC.@preserve a unsafe_wrap(Array, pointer(a), length(a))
a = collect(1:9)
b = f(a) # f or g give the same results
a = 2 # deref the original a
GC.gc() # a bunch of times
b # the data changes after a while
The type stability was indeed not directly giving us problems that I know of. But I want to keep those functions as simple as possible, and when trying out |
This aligns better with the updated expectations in https://github.com/Deltares/BasicModelInterface.jl/releases/tag/v0.1.1.
This means we don't return
SubArrays
or similar anymore, but always a simpleVector{Float64}
.