Skip to content

Commit f509a28

Browse files
refactor: split hashconsing caches by vartype
1 parent bd5d147 commit f509a28

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/types.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,13 @@ Base.nameof(s::BasicSymbolic) = issym(s) ? s.name : error("Non-Sym BasicSymbolic
830830
const ENABLE_HASHCONSING = Ref(true)
831831
const AllBasicSymbolics = Union{BasicSymbolic{SymReal}, BasicSymbolic{SafeReal}, BasicSymbolic{TreeReal}}
832832
const WCS_LOCK = ReentrantLock()
833-
const WCS = WeakCacheSet{AllBasicSymbolics}()
833+
const WCS_SYMREAL = WeakCacheSet{BasicSymbolic{SymReal}}()
834+
const WCS_SAFEREAL = WeakCacheSet{BasicSymbolic{SafeReal}}()
835+
const WCS_TREEREAL = WeakCacheSet{BasicSymbolic{TreeReal}}()
836+
837+
@inline wcs_for_vartype(::Type{SymReal}) = WCS_SYMREAL
838+
@inline wcs_for_vartype(::Type{SafeReal}) = WCS_SAFEREAL
839+
@inline wcs_for_vartype(::Type{TreeReal}) = WCS_TREEREAL
834840

835841
function generate_id()
836842
IDType()
@@ -858,12 +864,12 @@ Custom functions `hash2` and `isequal_with_metadata` are used instead of `Base.h
858864
original behavior of those functions.
859865
"""
860866

861-
function hashcons(s::BSImpl.Type)
867+
function hashcons(s::BSImpl.Type{T}) where {T}
862868
if !ENABLE_HASHCONSING[]
863869
return s
864870
end
865871
@manually_scope COMPARE_FULL => true begin
866-
k = (@lock WCS_LOCK getkey!(WCS, s))::typeof(s)
872+
k = (@lock WCS_LOCK getkey!(wcs_for_vartype(T), s))::typeof(s)
867873
if k.id === nothing
868874
k.id = generate_id()
869875
end

0 commit comments

Comments
 (0)