Skip to content

Commit

Permalink
Improve lookups in g.memo to search only once (call get(), instead of…
Browse files Browse the repository at this point in the history
… haskey() and getindex()).
  • Loading branch information
gkronber committed Aug 18, 2024
1 parent e4a3f03 commit 2677542
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/EGraphs/egraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ end
function lookup(g::EGraph, n::VecExpr)::Id
canonicalize!(g, n)

haskey(g.memo, n) ? find(g, g.memo[n]) : 0
id = get(g.memo, n, zero(Id))
iszero(id) ? id : find(g, id)
end


Expand All @@ -253,9 +254,10 @@ Inserts an e-node in an [`EGraph`](@ref)
"""
function add!(g::EGraph{ExpressionType,Analysis}, n::VecExpr, should_copy::Bool)::Id where {ExpressionType,Analysis}
canonicalize!(g, n)

haskey(g.memo, n) && return g.memo[n]


id = get(g.memo, n, zero(Id))
iszero(id) || return id

if should_copy
n = copy(n)
end
Expand Down Expand Up @@ -412,9 +414,8 @@ function process_unions!(g::EGraph{ExpressionType,AnalysisType})::Int where {Exp
while !isempty(g.pending)
(node::VecExpr, eclass_id::Id) = pop!(g.pending)
canonicalize!(g, node)
if haskey(g.memo, node)
old_class_id = g.memo[node]
g.memo[node] = eclass_id
old_class_id = get!(g.memo, node, eclass_id)
if old_class_id != eclass_id
did_something = union!(g, old_class_id, eclass_id)
# TODO unique! can node dedup be moved here? compare performance
# did_something && unique!(g[eclass_id].nodes)
Expand Down

0 comments on commit 2677542

Please sign in to comment.