-
Notifications
You must be signed in to change notification settings - Fork 5
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
Conversation
There was a problem hiding this 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
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} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
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.
Codecov ReportAll modified and coverable lines are covered by tests ✅
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. |
Co-authored-by: Bagaev Dmitry <[email protected]>
Co-authored-by: Bagaev Dmitry <[email protected]>
Co-authored-by: Bagaev Dmitry <[email protected]>
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 aTuple{NodeLabel, NodeData}
.Everything considered this is a +-25% speedup of the entire model creation, with around 25% less memory consumption