Skip to content

Fix type stability of Base.RefValue fields #342

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

Merged
merged 1 commit into from
May 19, 2025
Merged

Conversation

odow
Copy link
Collaborator

@odow odow commented May 19, 2025

I was trying to figure out why #241 was needed. Benchmarking showed some allocations that I didn't expect. Then I found that unsafe_convert was typed as ::Any!

julia> import SCIP

julia> import MathOptInterface as MOI

julia> model = SCIP.Optimizer();

julia> @time SCIP.SCIPgetSols(model)
  0.000005 seconds (2 allocations: 32 bytes)
Ptr{Ptr{Nothing}} @0x0000000000000000

julia> @time SCIP.SCIPgetNSols(model)
  0.000007 seconds (1 allocation: 16 bytes)
0

julia> @code_warntype SCIP.SCIPgetNSols(model)
MethodInstance for SCIP.LibSCIP.SCIPgetNSols(::SCIP.Optimizer)
  from SCIPgetNSols(scip) @ SCIP.LibSCIP ~/.julia/packages/SCIP/9Wf6c/src/LibSCIP.jl:18286
Arguments
  #self#::Core.Const(SCIP.LibSCIP.SCIPgetNSols)
  scip::SCIP.Optimizer
Body::Int32
1%1 = Core.apply_type(SCIP.LibSCIP.Ptr, SCIP.LibSCIP.SCIP)::Core.Const(Ptr{Nothing})
│   %2 = Base.cconvert(%1, scip)::SCIP.Optimizer%3 = Core.apply_type(SCIP.LibSCIP.Ptr, SCIP.LibSCIP.SCIP)::Core.Const(Ptr{Nothing})
│   %4 = Base.unsafe_convert(%3, %2)::Any%5 = $(Expr(:foreigncall, :(Core.tuple(:SCIPgetNSols, SCIP.LibSCIP.libscip)), Int32, svec(Ptr{Nothing}), 0, :(:ccall), :(%4), :(%2)))::Int32
└──      return %5

It's because of this classic gag that Base.Ref{T} is not a concrete value:

julia> x = Ref{Cint}(1)
Base.RefValue{Int32}(1)

julia> typeof(x)
Base.RefValue{Int32}

julia> typeof(x) <: Ref{Cint}
true

The fix is a couple of uses of Base.RefValue:

julia> import SCIP

julia> import MathOptInterface as MOI

julia> model = SCIP.Optimizer();

julia> @time SCIP.SCIPgetSols(model)
  0.000008 seconds (1 allocation: 16 bytes)
Ptr{Ptr{Nothing}} @0x0000000000000000

julia> @time SCIP.SCIPgetNSols(model)
  0.000005 seconds
0

julia> @code_warntype SCIP.SCIPgetNSols(model)
MethodInstance for SCIP.LibSCIP.SCIPgetNSols(::SCIP.Optimizer)
  from SCIPgetNSols(scip) @ SCIP.LibSCIP ~/.julia/dev/SCIP/src/LibSCIP.jl:18291
Arguments
  #self#::Core.Const(SCIP.LibSCIP.SCIPgetNSols)
  scip::SCIP.Optimizer
Body::Int32
1%1 = Core.apply_type(SCIP.LibSCIP.Ptr, SCIP.LibSCIP.SCIP)::Core.Const(Ptr{Nothing})
│   %2 = Base.cconvert(%1, scip)::SCIP.Optimizer%3 = Core.apply_type(SCIP.LibSCIP.Ptr, SCIP.LibSCIP.SCIP)::Core.Const(Ptr{Nothing})
│   %4 = Base.unsafe_convert(%3, %2)::Ptr{Nothing}%5 = $(Expr(:foreigncall, :(Core.tuple(:SCIPgetNSols, SCIP.LibSCIP.libscip)), Int32, svec(Ptr{Nothing}), 0, :(:ccall), :(%4), :(%2)))::Int32
└──      return %5

@matbesancon matbesancon merged commit e38c841 into master May 19, 2025
15 checks passed
@matbesancon matbesancon deleted the od/ref-value branch May 19, 2025 08:00
@matbesancon
Copy link
Member

Good catch thanks! Merged and released

@odow odow mentioned this pull request May 23, 2025
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