Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better type stability #247

Merged
merged 6 commits into from
Jul 16, 2024
Merged

Better type stability #247

merged 6 commits into from
Jul 16, 2024

Conversation

wouterwln
Copy link
Member

Hash now depends on only the global counter; we use a type-stable version of to_symbol to hash constant nodes to the context. FactorID is parameterized because the type instability of having them in the dictionary is better than the type instability of the hashing and comparison downstream from adding them to the dictionary. The plugin system has a type assertion (which acts as a type hint to the compiler) to make the variable node creation type stable. This is ugly but I couldn't get the compiler to understand that this function always returns a Tuple{NodeLabel, NodeData}.

Everything considered this is a +-25% speedup of the entire model creation, with around 25% less memory consumption

@wouterwln wouterwln requested a review from bvdmitri July 16, 2024 12:05
Copy link
Member

@bvdmitri bvdmitri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments, but LGTM

src/graph_engine.jl Outdated Show resolved Hide resolved
src/graph_engine.jl Outdated Show resolved Hide resolved
plugins = filter(type, getplugins(model))
return foldl(plugins; init = (label, nodedata)) do (label, nodedata), plugin
return preprocess_plugin(plugin, model, context, label, nodedata, options)
end
end::Tuple{NodeLabel, NodeData}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
end::Tuple{NodeLabel, NodeData}
end


Base.show(io::IO, label::NodeLabel) = print(io, label.name, "_", label.global_counter)
Base.:(==)(label1::NodeLabel, label2::NodeLabel) = label1.name == label2.name && label1.global_counter == label2.global_counter
Base.hash(label::NodeLabel, h::UInt) = hash(label.name, hash(label.global_counter, h))
Base.hash(label::NodeLabel, h::UInt) = hash(label.global_counter, h)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it should actually be fine, hash can collide in principle. Also the documentation says

hash(x[, h::UInt]) -> UInt

  Compute an integer hash code such that isequal(x,y) implies
  hash(x)==hash(y). The optional second argument h is another hash code to be
  mixed with the result.

So in principle our a == b implies hash(a) == hash(b) and it follows the specification as documented. The opposite relation is not necessarily required.

src/graph_engine.jl Outdated Show resolved Hide resolved
Copy link

codecov bot commented Jul 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.86%. Comparing base (88ca4ac) to head (f070122).
Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #247   +/-   ##
=======================================
  Coverage   90.85%   90.86%           
=======================================
  Files          15       15           
  Lines        2088     2090    +2     
=======================================
+ Hits         1897     1899    +2     
  Misses        191      191           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

wouterwln and others added 4 commits July 16, 2024 15:08
@wouterwln wouterwln merged commit 8d66ed7 into main Jul 16, 2024
8 checks passed
@wouterwln wouterwln deleted the type-stability branch July 16, 2024 14:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants