Skip to content

Commit

Permalink
Force inlining
Browse files Browse the repository at this point in the history
About 15% faster on microbenchmark, `f!(d)` example in pr comments
  • Loading branch information
Zentrik committed Oct 29, 2023
1 parent 2d8a87a commit 74e0ca6
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions base/iddict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ function sizehint!(d::IdDict, newsz)
rehash!(d, newsz)
end

function ht_keyindex!(d::IdDict{K, V}, @nospecialize(key)) where {K, V}
@inline function ht_keyindex!(d::IdDict{K, V}, @nospecialize(key)) where {K, V}
!isa(key, K) && throw(ArgumentError("$(limitrepr(key)) is not a valid key for type $K"))

ht = d.ht
t = @_gc_preserve_begin ht

ref = Ref{Ptr{Any}}(pointer_from_objref(ht))
ref = RefValue{Ptr{Memory{Any}}}(pointer_from_objref(ht))
# # keyindex - where a key is stored, or -pos if the key was not present and was inserted at pos
keyindex = ccall(:jl_table_assign_bp, Cssize_t, (Ptr{Ptr{Any}}, Any, Any, Cint), ref, key, C_NULL, 0)
keyindex = ccall(:jl_table_assign_bp, Cssize_t, (Ptr{Ptr{Memory{Any}}}, Any, Any, Cint), ref, key, C_NULL, 0)
d.ht = unsafe_pointer_to_objref(ref[])::Memory{Any}

@_gc_preserve_end t
Expand All @@ -110,7 +110,7 @@ function _setindex!(d::IdDict{K, V}, val::V, key::K, keyindex::Int, inserted::Bo
return nothing
end

function setindex!(d::IdDict{K,V}, @nospecialize(val), @nospecialize(key)) where {K, V}
@inline function setindex!(d::IdDict{K,V}, @nospecialize(val), @nospecialize(key)) where {K, V}
keyindex, inserted = ht_keyindex!(d, key)
if !(val isa V) # avoid a dynamic call
val = convert(V, val)::V
Expand Down Expand Up @@ -178,7 +178,7 @@ isempty(d::IdDict) = length(d) == 0

copy(d::IdDict) = typeof(d)(d)

function get!(d::IdDict{K,V}, @nospecialize(key), @nospecialize(default)) where {K, V}
@inline function get!(d::IdDict{K,V}, @nospecialize(key), @nospecialize(default)) where {K, V}
keyindex, inserted = ht_keyindex!(d, key)

if inserted
Expand Down

0 comments on commit 74e0ca6

Please sign in to comment.