Skip to content

Commit b79226e

Browse files
aviateskKristofferC
authored andcommitted
inference: make limit::Int as a caching key of CachedMethodTable (#46799)
Sometimes `Core.Compiler.findall(::Type, ::CachedMethodTable; limit::Int)` is called with different `limit` setting (in particularity `return_type_tfunc` calls it with `limit=-1`). The query should return different results given different `limit` settings, so its cache should also have different keys per different `limit` settings. fix #46722
1 parent 86d0354 commit b79226e

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

base/compiler/methodtable.jl

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,23 @@ struct OverlayMethodTable <: MethodTableView
4444
mt::Core.MethodTable
4545
end
4646

47+
struct MethodMatchKey
48+
sig # ::Type
49+
limit::Int
50+
MethodMatchKey(@nospecialize(sig), limit::Int) = new(sig, limit)
51+
end
52+
4753
"""
4854
struct CachedMethodTable <: MethodTableView
4955
5056
Overlays another method table view with an additional local fast path cache that
5157
can respond to repeated, identical queries faster than the original method table.
5258
"""
5359
struct CachedMethodTable{T} <: MethodTableView
54-
cache::IdDict{Any, Union{Missing, MethodMatchResult}}
60+
cache::IdDict{MethodMatchKey, Union{Missing,MethodMatchResult}}
5561
table::T
5662
end
57-
CachedMethodTable(table::T) where T = CachedMethodTable{T}(IdDict{Any, Union{Missing, MethodMatchResult}}(), table)
63+
CachedMethodTable(table::T) where T = CachedMethodTable{T}(IdDict{MethodMatchKey, Union{Missing,MethodMatchResult}}(), table)
5864

5965
"""
6066
findall(sig::Type, view::MethodTableView; limit::Int=typemax(Int)) ->
@@ -109,9 +115,11 @@ function findall(@nospecialize(sig::Type), table::CachedMethodTable; limit::Int=
109115
# as for concrete types, we cache result at on the next level
110116
return findall(sig, table.table; limit)
111117
end
112-
box = Core.Box(sig)
113-
return get!(table.cache, sig) do
114-
findall(box.contents, table.table; limit)
118+
key = MethodMatchKey(sig, limit)
119+
if haskey(table.cache, key)
120+
return table.cache[key]
121+
else
122+
return table.cache[key] = findall(sig, table.table; limit)
115123
end
116124
end
117125

0 commit comments

Comments
 (0)