Skip to content

Commit 526bb73

Browse files
Merge pull request #764 from AayushSabharwal/as/wcs
refactor: use new `WeakCacheSet` for hashconsing
2 parents d6b7167 + f8ec551 commit 526bb73

File tree

4 files changed

+21
-17
lines changed

4 files changed

+21
-17
lines changed

Project.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ TaskLocalValues = "ed4db957-447d-4319-bfb6-7fa9ae7ecf34"
2727
TermInterface = "8ea1fca8-c5ef-4a55-8b96-4e9afe9c9a3c"
2828
TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
2929
Unityper = "a7c27f48-0311-42f6-a7f8-2c11e75eb415"
30-
WeakValueDicts = "897b6980-f191-5a31-bcb0-bf3c4585e0c1"
3130

3231
[weakdeps]
3332
LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800"
@@ -62,7 +61,6 @@ TaskLocalValues = "0.1.2"
6261
TermInterface = "2.0"
6362
TimerOutputs = "0.5"
6463
Unityper = "0.1.2"
65-
WeakValueDicts = "0.1.0"
6664
julia = "1.10"
6765

6866
[extras]

src/SymbolicUtils.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ import TermInterface: iscall, isexpr, head, children,
2222
import ArrayInterface
2323
import ExproniconLite as EL
2424
import TaskLocalValues: TaskLocalValue
25-
using WeakValueDicts: WeakValueDict
2625

27-
# include("WeakCacheSets.jl")
26+
function hash2 end
27+
function isequal_with_metadata end
28+
29+
include("WeakCacheSets.jl")
30+
31+
using .WeakCacheSets
2832

2933
include("cache.jl")
3034
Base.@deprecate istree iscall

src/WeakCacheSets.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Copied from WeakCacheSets.jl, modified to use hash2 and isequal_with_metadata instead of
2+
# the standard `hash` and `isequal`.
3+
module WeakCacheSets
4+
5+
using SymbolicUtils: hash2, isequal_with_metadata
6+
17
# These can be changed, to trade off better performance for space
28
const maxallowedprobe = 16
39
const maxprobeshift = 6
@@ -229,3 +235,7 @@ function Base.show(io::IO, t::WeakCacheSet{K}) where K
229235
end
230236
print(io, ')')
231237
end
238+
239+
export WeakCacheSet, getkey!
240+
241+
end # module WeakCacheSets

src/types.jl

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ end
516516
### Constructors
517517
###
518518

519-
const wvd = TaskLocalValue{WeakValueDict{UInt, BasicSymbolic}}(WeakValueDict{UInt, BasicSymbolic})
519+
const wvd = TaskLocalValue{WeakCacheSet{BasicSymbolic}}(WeakCacheSet{BasicSymbolic})
520520

521521
function generate_id()
522522
return IDType()
@@ -549,19 +549,11 @@ function BasicSymbolic(s::BasicSymbolic)::BasicSymbolic
549549
end
550550

551551
cache = wvd[]
552-
h = hash2(s)
553-
k = get!(cache, h, s)
554-
if isequal_with_metadata(k, s)
555-
if isnothing(k.id[])
556-
k.id[] = generate_id()
557-
end
558-
return k
559-
else
560-
if isnothing(s.id[])
561-
s.id[] = generate_id()
562-
end
563-
return s
552+
k = getkey!(cache, s)
553+
if k.id[] === nothing
554+
k.id[] = generate_id()
564555
end
556+
return k
565557
end
566558

567559
function Sym{T}(name::Symbol; kw...) where {T}

0 commit comments

Comments
 (0)