Skip to content

Commit 1427190

Browse files
committed
Only cache hash when salt is 0
1 parent f562129 commit 1427190

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

src/types.jl

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,10 @@ hashvec(xs, z) = foldr(hash, xs, init=z)
297297

298298
function Base.hash(t::Term{T}, salt::UInt) where {T}
299299
h = t.hash[]
300-
!iszero(h) && return h
301-
t.hash[] = hashvec(arguments(t), hash(operation(t), hash(T, salt)))
300+
(iszero(salt) && !iszero(h)) && return h
301+
h′ = hashvec(arguments(t), hash(operation(t), hash(T, salt)))
302+
iszero(salt) && (t.hash[] = h′)
303+
return h′
302304
end
303305

304306
_promote_symtype(f::Sym, args) = promote_symtype(f, map(symtype, args)...)
@@ -454,12 +456,6 @@ function arguments(a::Add)
454456
a.sorted_args_cache[] = iszero(a.coeff) ? args : vcat(a.coeff, args)
455457
end
456458

457-
function Base.hash(a::Add, u::UInt64)
458-
h = a.hash[]
459-
!iszero(h) && return h
460-
a.hash[] = hash(0xaddaddaddaddadda, hash(a.coeff, hash(a.dict, u)))
461-
end
462-
463459
Base.isequal(a::Add, b::Add) = isequal(a.coeff, b.coeff) && isequal(a.dict, b.dict)
464460

465461
Base.show(io::IO, a::Add) = show_term(io, a)
@@ -582,15 +578,8 @@ function arguments(a::Mul)
582578
a.sorted_args_cache[] = isone(a.coeff) ? args : vcat(a.coeff, args)
583579
end
584580

585-
function Base.hash(m::Mul, u::UInt64)
586-
h = m.hash[]
587-
!iszero(h) && return h
588-
m.hash[] = hash(0xaaaaaaaaaaaaaaa, hash(m.coeff, hash(m.dict, u)))
589-
end
590-
591581
Base.isequal(a::Mul, b::Mul) = isequal(a.coeff, b.coeff) && isequal(a.dict, b.dict)
592582

593-
594583
Base.show(io::IO, a::Mul) = show_term(io, a)
595584

596585
function makemul(coeff, xs...; d=sdict())
@@ -729,3 +718,12 @@ function similarterm(p::Union{Mul, Add, Pow}, f, args)
729718
f(args...)
730719
end
731720
end
721+
722+
function Base.hash(t::Union{Add,Mul}, u::UInt64)
723+
h = t.hash[]
724+
(iszero(u) && !iszero(h)) && return h
725+
hashoffset = t isa Add ? 0xaddaddaddaddadda : 0xaddaddaddaddadda
726+
h′= hash(hashoffset, hash(t.coeff, hash(t.dict, u)))
727+
iszero(u) && (t.hash[] = h′)
728+
return h′
729+
end

0 commit comments

Comments
 (0)