Skip to content

Commit 1f70991

Browse files
Merge pull request #695 from AayushSabharwal/as/toggle-hashconsing
feat: add toggle for hashconsing
2 parents e0d7d72 + f74472f commit 1f70991

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/types.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const EMPTY_HASH = RefValue(UInt(0))
2121
const NOT_SORTED = RefValue(false)
2222
const EMPTY_DICT = sdict()
2323
const EMPTY_DICT_T = typeof(EMPTY_DICT)
24+
const ENABLE_HASHCONSING = Ref(true)
2425

2526
@compactify show_methods=false begin
2627
@abstract mutable struct BasicSymbolic{T} <: Symbolic{T}
@@ -507,6 +508,9 @@ Custom functions `hash2` and `isequal_with_metadata` are used instead of `Base.h
507508
original behavior of those functions.
508509
"""
509510
function BasicSymbolic(s::BasicSymbolic)::BasicSymbolic
511+
if !ENABLE_HASHCONSING[]
512+
return s
513+
end
510514
h = hash2(s)
511515
t = get!(wvd, h, s)
512516
if t === s || isequal_with_metadata(t, s)

test/hash_consing.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,12 @@ end
128128
@test !SymbolicUtils.isequal_with_metadata(a, aa)
129129
@test !SymbolicUtils.isequal_with_metadata(2a, 2aa)
130130
end
131+
132+
@testset "Hashconsing can be toggled" begin
133+
SymbolicUtils.ENABLE_HASHCONSING[] = false
134+
name = gensym(:x)
135+
x1 = only(@eval @syms $name)
136+
x2 = only(@eval @syms $name)
137+
@test x1 !== x2
138+
SymbolicUtils.ENABLE_HASHCONSING[] = true
139+
end

0 commit comments

Comments
 (0)