Skip to content

Commit

Permalink
Cache hash of ConnectionElement
Browse files Browse the repository at this point in the history
  • Loading branch information
chriselrod committed Dec 13, 2023
1 parent c10d206 commit 85a3bd5
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/systems/connectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,32 @@ struct ConnectionElement
sys::LazyNamespace
v::Any
isouter::Bool
h::UInt
end
Base.nameof(l::ConnectionElement) = renamespace(nameof(l.sys), getname(l.v))
function Base.hash(l::ConnectionElement, salt::UInt)
hash(nameof(l.sys)) hash(l.v) hash(l.isouter) salt
function _hash_impl(sys, v, isouter)
hashcore = hash(nameof(sys)) hash(v)
hashouter = isouter ? hash(true) : hash(false)
hashcore hashouter

Check warning on line 155 in src/systems/connectors.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/connectors.jl#L152-L155

Added lines #L152 - L155 were not covered by tests
end
function ConnectionElement(sys::LazyNamespace, v, isouter::Bool)
ConnectionElement(sys, v, isouter, _hash_impl(sys, v, isouter))

Check warning on line 158 in src/systems/connectors.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/connectors.jl#L157-L158

Added lines #L157 - L158 were not covered by tests
end
Base.nameof(l::ConnectionElement) = renamespace(nameof(l.sys), getname(l.v))

Check warning on line 160 in src/systems/connectors.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/connectors.jl#L160

Added line #L160 was not covered by tests
Base.isequal(l1::ConnectionElement, l2::ConnectionElement) = l1 == l2
function Base.:(==)(l1::ConnectionElement, l2::ConnectionElement)
nameof(l1.sys) == nameof(l2.sys) && isequal(l1.v, l2.v) && l1.isouter == l2.isouter
end
Base.hash(e::ConnectionElement, salt::UInt) = e.h salt

Check warning on line 165 in src/systems/connectors.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/connectors.jl#L165

Added line #L165 was not covered by tests
namespaced_var(l::ConnectionElement) = states(l, l.v)
states(l::ConnectionElement, v) = states(copy(l.sys), v)

function withtrueouter(e::ConnectionElement)
e.isouter && return e

Check warning on line 170 in src/systems/connectors.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/connectors.jl#L169-L170

Added lines #L169 - L170 were not covered by tests
# we undo the xor
newhash = (e.h hash(false)) hash(true)
ConnectionElement(e.sys, e.v, true, newhash)

Check warning on line 173 in src/systems/connectors.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/connectors.jl#L172-L173

Added lines #L172 - L173 were not covered by tests
end

struct ConnectionSet
set::Vector{ConnectionElement} # namespace.sys, var, isouter
end
Expand Down Expand Up @@ -353,9 +367,7 @@ function partial_merge(csets::AbstractVector{<:ConnectionSet}, allouter = false)
merged = false
for (j, cset) in enumerate(csets)
if allouter
cset = ConnectionSet(map(cset.set) do e
@set! e.isouter = true
end)
cset = ConnectionSet(map(withtrueouter, cset.set))

Check warning on line 370 in src/systems/connectors.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/connectors.jl#L370

Added line #L370 was not covered by tests
end
idx = nothing
for e in cset.set
Expand Down Expand Up @@ -390,7 +402,7 @@ end

function generate_connection_equations_and_stream_connections(csets::AbstractVector{
<:ConnectionSet,
})
})
eqs = Equation[]
stream_connections = ConnectionSet[]

Expand Down

0 comments on commit 85a3bd5

Please sign in to comment.