Skip to content

Commit

Permalink
inference: make limit::Int as a caching key of CachedMethodTable (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
aviatesk committed Sep 17, 2022
1 parent 81f6c23 commit e24dd3b
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions base/compiler/methodtable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,23 @@ struct OverlayMethodTable <: MethodTableView
mt::Core.MethodTable
end

struct MethodMatchKey
sig # ::Type
limit::Int
MethodMatchKey(@nospecialize(sig), limit::Int) = new(sig, limit)
end

"""
struct CachedMethodTable <: MethodTableView
Overlays another method table view with an additional local fast path cache that
can respond to repeated, identical queries faster than the original method table.
"""
struct CachedMethodTable{T} <: MethodTableView
cache::IdDict{Any, Union{Missing, MethodMatchResult}}
cache::IdDict{MethodMatchKey, Union{Missing,MethodMatchResult}}
table::T
end
CachedMethodTable(table::T) where T = CachedMethodTable{T}(IdDict{Any, Union{Missing, MethodMatchResult}}(), table)
CachedMethodTable(table::T) where T = CachedMethodTable{T}(IdDict{MethodMatchKey, Union{Missing,MethodMatchResult}}(), table)

"""
findall(sig::Type, view::MethodTableView; limit::Int=typemax(Int)) ->
Expand Down Expand Up @@ -109,9 +115,11 @@ function findall(@nospecialize(sig::Type), table::CachedMethodTable; limit::Int=
# as for concrete types, we cache result at on the next level
return findall(sig, table.table; limit)
end
box = Core.Box(sig)
return get!(table.cache, sig) do
findall(box.contents, table.table; limit)
key = MethodMatchKey(sig, limit)
if haskey(table.cache, key)
return table.cache[key]
else
return table.cache[key] = findall(sig, table.table; limit)
end
end

Expand Down

0 comments on commit e24dd3b

Please sign in to comment.