Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add toggle for hashconsing #695

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const EMPTY_HASH = RefValue(UInt(0))
const NOT_SORTED = RefValue(false)
const EMPTY_DICT = sdict()
const EMPTY_DICT_T = typeof(EMPTY_DICT)
const ENABLE_HASHCONSING = Ref(true)

@compactify show_methods=false begin
@abstract mutable struct BasicSymbolic{T} <: Symbolic{T}
Expand Down Expand Up @@ -507,6 +508,9 @@ Custom functions `hash2` and `isequal_with_metadata` are used instead of `Base.h
original behavior of those functions.
"""
function BasicSymbolic(s::BasicSymbolic)::BasicSymbolic
if !ENABLE_HASHCONSING[]
return s
end
h = hash2(s)
t = get!(wvd, h, s)
if t === s || isequal_with_metadata(t, s)
Expand Down
9 changes: 9 additions & 0 deletions test/hash_consing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,12 @@ end
@test !SymbolicUtils.isequal_with_metadata(a, aa)
@test !SymbolicUtils.isequal_with_metadata(2a, 2aa)
end

@testset "Hashconsing can be toggled" begin
SymbolicUtils.ENABLE_HASHCONSING[] = false
name = gensym(:x)
x1 = only(@eval @syms $name)
x2 = only(@eval @syms $name)
@test x1 !== x2
SymbolicUtils.ENABLE_HASHCONSING[] = true
end
Loading