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

[REPLCompletions] improve REPLInterpreter effects of dict operations #52347

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ function empty!(h::Dict{K,V}) where V where K
end

# get the index where a key is stored, or -1 if not present
@assume_effects :terminates_locally function ht_keyindex(h::Dict{K,V}, key) where V where K
function ht_keyindex(h::Dict{K,V}, key) where V where K
isempty(h) && return -1
sz = length(h.keys)
iter = 0
Expand All @@ -280,9 +280,9 @@ end
index, sh = hashindex(key, sz)
keys = h.keys

@inbounds while true
@assume_effects :terminates_locally :noub @inbounds while true
isslotempty(h,index) && return -1
if h.slots[index] == sh
if sh == h.slots[index]
k = keys[index]
if (key === k || isequal(key, k))
return index
Expand Down Expand Up @@ -507,7 +507,7 @@ end

function getindex(h::Dict{K,V}, key) where V where K
index = ht_keyindex(h, key)
@inbounds return (index < 0) ? throw(KeyError(key)) : h.vals[index]::V
return index < 0 ? throw(KeyError(key)) : @assume_effects :noub @inbounds h.vals[index]::V
end

"""
Expand Down
18 changes: 18 additions & 0 deletions stdlib/REPL/test/replcompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2156,3 +2156,21 @@ let t = REPLCompletions.repl_eval_ex(:(Base.PersistentDict(issue52099 => 3)), @_
@test length(t.val) == 1
end
end

# test REPLInterpreter effects for `getindex(::Dict, key)`
for (DictT, KeyT) = Any[(Dict{Symbol,Any}, Symbol),
(Dict{Int,Any}, Int),
(Dict{String,Any}, String)]
@testset let DictT=DictT, KeyT=KeyT
effects = Base.infer_effects(getindex, (DictT,KeyT); interp=REPL.REPLCompletions.REPLInterpreter())
@test Core.Compiler.is_effect_free(effects)
@test Core.Compiler.is_terminates(effects)
@test Core.Compiler.is_noub(effects)
effects = Base.infer_effects((DictT,KeyT); interp=REPL.REPLCompletions.REPLInterpreter()) do d, key
key in keys(d)
end
@test Core.Compiler.is_effect_free(effects)
@test Core.Compiler.is_terminates(effects)
@test Core.Compiler.is_noub(effects)
end
end