Skip to content

Commit 85a3bd5

Browse files
committed
Cache hash of ConnectionElement
1 parent c10d206 commit 85a3bd5

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/systems/connectors.jl

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,32 @@ struct ConnectionElement
147147
sys::LazyNamespace
148148
v::Any
149149
isouter::Bool
150+
h::UInt
150151
end
151-
Base.nameof(l::ConnectionElement) = renamespace(nameof(l.sys), getname(l.v))
152-
function Base.hash(l::ConnectionElement, salt::UInt)
153-
hash(nameof(l.sys)) hash(l.v) hash(l.isouter) salt
152+
function _hash_impl(sys, v, isouter)
153+
hashcore = hash(nameof(sys)) hash(v)
154+
hashouter = isouter ? hash(true) : hash(false)
155+
hashcore hashouter
156+
end
157+
function ConnectionElement(sys::LazyNamespace, v, isouter::Bool)
158+
ConnectionElement(sys, v, isouter, _hash_impl(sys, v, isouter))
154159
end
160+
Base.nameof(l::ConnectionElement) = renamespace(nameof(l.sys), getname(l.v))
155161
Base.isequal(l1::ConnectionElement, l2::ConnectionElement) = l1 == l2
156162
function Base.:(==)(l1::ConnectionElement, l2::ConnectionElement)
157163
nameof(l1.sys) == nameof(l2.sys) && isequal(l1.v, l2.v) && l1.isouter == l2.isouter
158164
end
165+
Base.hash(e::ConnectionElement, salt::UInt) = e.h salt
159166
namespaced_var(l::ConnectionElement) = states(l, l.v)
160167
states(l::ConnectionElement, v) = states(copy(l.sys), v)
161168

169+
function withtrueouter(e::ConnectionElement)
170+
e.isouter && return e
171+
# we undo the xor
172+
newhash = (e.h hash(false)) hash(true)
173+
ConnectionElement(e.sys, e.v, true, newhash)
174+
end
175+
162176
struct ConnectionSet
163177
set::Vector{ConnectionElement} # namespace.sys, var, isouter
164178
end
@@ -353,9 +367,7 @@ function partial_merge(csets::AbstractVector{<:ConnectionSet}, allouter = false)
353367
merged = false
354368
for (j, cset) in enumerate(csets)
355369
if allouter
356-
cset = ConnectionSet(map(cset.set) do e
357-
@set! e.isouter = true
358-
end)
370+
cset = ConnectionSet(map(withtrueouter, cset.set))
359371
end
360372
idx = nothing
361373
for e in cset.set
@@ -390,7 +402,7 @@ end
390402

391403
function generate_connection_equations_and_stream_connections(csets::AbstractVector{
392404
<:ConnectionSet,
393-
})
405+
})
394406
eqs = Equation[]
395407
stream_connections = ConnectionSet[]
396408

0 commit comments

Comments
 (0)