Skip to content

Commit

Permalink
inference: also make limit::Int as the key of CachedMethodTable
Browse files Browse the repository at this point in the history
Sometimes `Core.Compiler.findall(::Type, ::CachedMethodTable; limit::Int)`
is called with different `limit` setting (particularly `return_type_tfunc`
will call it with `limit=-1`), and it should return different results
given different `limit` settings.

fix #46722
  • Loading branch information
aviatesk committed Sep 16, 2022
1 parent 03ed550 commit c880b65
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 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,10 @@ 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)
box = RefValue{Type}(sig)
return get!(table.cache, key) do
findall(box[], table.table; limit)
end
end

Expand Down

0 comments on commit c880b65

Please sign in to comment.