From 26775420c86814b7e52ce02324e127a8acd3887e Mon Sep 17 00:00:00 2001 From: Gabriel Kronberger Date: Sun, 18 Aug 2024 14:00:36 +0200 Subject: [PATCH] Improve lookups in g.memo to search only once (call get(), instead of haskey() and getindex()). --- src/EGraphs/egraph.jl | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/EGraphs/egraph.jl b/src/EGraphs/egraph.jl index d3ea68c2..0dcb73e2 100644 --- a/src/EGraphs/egraph.jl +++ b/src/EGraphs/egraph.jl @@ -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 @@ -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 @@ -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)