Repository navigation
Conversation
There was a problem hiding this comment.
Excellent performance improvement. 👍, good catch, embarassing for me.
Our general coding guideline wrt hashmap iteration order stability across joern and the commercial extension (codescience) is:
We have had really bad bugs caused by non-reproducibility in hashmap iteration order. So never assume that hashmap iteration order is reproducible; tend to use LinkedHashMap or various normalization schemes. I think we even have custom linter rules to help us stick to that.
This change improves the reproducibility for unordered hashmaps of GNode specifically, but not for unordered hashmaps of other objects.
But "look into the hashCode implementation, check whether it ever touches the effectively randomized identity-hash" is maybe not a sustainable coding guideline. If this change ends up promising "hashCode for GNode is deterministic/reproducible", then reviewing code for reproducibility/determinism bugs becomes much harder (no more bright red line in the sand), and we will cry on every scala stdlib update (I think unordered hashmap iteration order is not promised stable on bugfix/point releases).
If this change instead ends up as "keep bright red line, but mitigate the impact for unordered maps of GNode", then we mask bugs: Testing will never see the issue until we e.g. try to update our scala version.
I propose to split this update:
- Better hashCode: Add a
static boolean mixInGraphIdentity = true;and mix the graph's identity in if that is true (predictable branch is approximately free), using murmur/fmix; don't forget to make that safe if the graph is null (ugly ugly legacy hacks in the commercial extension that you can't see). Also add public getter/setter in AccessHelpers.java . I will insta-approve that, awesome change without all the trade-offs, and your projects downstream of flatgraph can simply decide to make that stable, using the public setter. - Second PR: Have a serious design discussion. Do we want to promise hashCode determinism to joern+codescience? Do we want the hashCode coincidentially stable (default
mixInGraphIdentity = false; use the public getter/setter to enable randomization/mix-in for some tests)? For that, I will invite a bunch of people to the second PR discussion+review.
…switch for run-independent hashes Hash a node with MurmurHash3's 64-bit finalizer of its id (kind and seq), with the graph's identity hash in the upper half, folded to 32 bits. The current hash, Objects.hash(graph, nodeKind, _seqId), is linear in kind and seq, so nodes of different kinds share hash values (89.5% of a coreboot graph's 9,680,014 nodes; about 0.2% with the new hash). GNode.mixInGraphIdentity, on by default, mixes in the graph's identity hash, so hash-based collections of nodes iterate in a different order in every run, as before. The graph takes its identity hash the first time a node is hashed and keeps it. With the switch off (system property flatgraph.GNode.mixInGraphIdentity=false, or AccessHelpers.setMixInGraphIdentity), a node hashes the same in every run, and two builds of the same code give byte-identical graphs.
db9712e to
64f627e
Compare
|
Thanks, I really like the solution of creating a property to enable/disable the deterministic hash algorithm. I have been doing a lot of work on optimizing algorithms in Joern in order to deal with much larger methods and repos. Being able to turn on the deterministic hash algorithm makes it easier for both me and others to validate that the new algorithms are building the exact same graph (down to the order of its edges). The PR now does this: |
GNode.hashCodeisObjects.hash(graph, nodeKind, _seqId), since #127. This PR has one commit, which replaces it with MurmurHash3's 64-bit finalizer (fmix64) applied to the node'sid()with the graph's identity hash in the upper half, folded to 32 bits, and adds a switch,GNode.mixInGraphIdentity, that leaves the identity hash out.31 * nodeKind + seqplus a constant, so kind k at seq s + 31 has the same hash as kind k + 1 at seq s, and the nodes of every kind fall into the range of the largest kind. In a coreboot graph of 9,680,014 nodes, 8,661,826 (89.5%) share their hash with another node, up to 28 on one value. With the new hash and the switch off, 21,689 do, at most 3 on one value. With it on, the count depends on the graph's identity hash: for five random values in its place, on the 9,680,012 nodes of the benchmark below, 21,592 to 21,869, at most 4. A random 32-bit hash gives about 21,800.mixInGraphIdentityon, the default, the hash mixes in the graph's identity hash, as 0.1.36 does, so hash-based collections of nodes iterate in a different order in every run. With it off, a node's hash depends only on its kind and seq, so two builds of the same graph give every node the same hash. It is turned off with the system propertyflatgraph.GNode.mixInGraphIdentity=falseor withAccessHelpers.setMixInGraphIdentity(false), before any node is hashed.Why the switch
Code should not depend on the iteration order of a hash map. With the switch on, code that does gives different results from run to run, so tests can catch it. Off is for users who need the same graph from every build of the same code, for example to show that no code has changed between two builds, or that a change meant to leave the output alone does. Whether the default should change is a separate discussion.
Joern's passes build edges while iterating such collections, for example
CdgPassthroughCfgDominatorFrontier'smutable.Mapandmutable.Setof nodes, and the reaching-definitions pass. Eleven C and C++ code bases, each built the same way several times with Joernmaster011ed31bf (the builds of the Performance section below: 6 per version or switch setting for the seven smaller code bases, 2 for the four larger):Cause
Graphhas nohashCodeof its own, soObjects.hash(graph, …)uses the graph object's identity hash, which the JVM assigns afresh in every run. #127 first paired thishashCodewith anequalson(graph, nodeKind, seq); review kept equality by identity and removed theequals, and thehashCodekept the graph.Objects.hashcombines its arguments linearly (multiply by 31, add the next), so kind and seq only shift the hash, and nodes of different kinds land on the same values.Change
core/src/main/java/flatgraph/GNode.java(the now unusedjava.util.Objectsimport is removed):core/src/main/scala/flatgraph/Graph.scala:core/src/main/java/flatgraph/AccessHelpers.javagetsgetMixInGraphIdentity()andsetMixInGraphIdentity(boolean).GNodedoes not overrideequals, so equal nodes are the same object and have the same hash.Graph. CallingSystem.identityHashCode(graph)in everyhashCodemade the random-order lookups below about 45% slower than with the switch off; kept, it costs about 11%, the same as a value stored when the graph is created. Taking it in theGraphconstructor instead gave the same value in every build: six builds of the same code gave six identical graph files. HotSpot draws identity hashes from a per-thread pseudo-random sequence, so a value taken at the same point of the same thread repeats from run to run. Taken at the first hash, it is the value 0.1.36'sObjects.hash(graph, …)mixes in.Performance
A
mutable.HashSetof objects with the node kinds and counts of a coreboot graph (9,680,012 nodes) and identity equality, JDK 21 on an arm64 laptop. Each figure is the median of 7 repetitions; a range covers two separate runs. Random order is a fixed shuffle; id order is ascendingid(), that is by kind, then by seq.In id order, the current hash puts consecutive nodes in consecutive slots of the set; the new hash spreads them out.
Share of Joern's build CPU spent in
GNode.hashCode, in the eleven code bases, on an arm64 Linux machine with JDK 21. Java Flight Recorder sampled every 1 ms. The figure is the share of the samples of thejoern-parseprocess, which runs Joern's passes, that are inGNode.hashCodeor the hashing it calls, pooled over the builds, with a 95% Wilson interval. The C/C++ frontend runs in a process of its own and hashes no nodes. The 0.1.36 builds alternated with builds of an earlier version of this change; the builds with the switch on and off alternated with each other.Objects.hashmeans allocating an array and boxing two of the arguments. It does not include time that hash collections spend walking past colliding entries.Testing
GraphTestscase: with the switch on, 200 nodes of two kinds have 200 different hashes, and two graphs built the same way have different hashes; with it off, they have the same hashes and aHashSetof each graph's nodes iterates in the same order; in both, a node of one graph is not equal to the node with the same kind and seq in the other. It fails ifhashCodeignores the switch either way.sbt scalafmtCheck Test/scalafmtCheck testpasses: 219 succeeded, 0 failed (master: 218).master011ed31bf with flatgraph-core 0.1.36 plus this change, on macOS (arm64):testFullpasses in every module (8,442 succeeded, 0 failed, with Rust 1.99.1 forrust2cpg),c2cpg/stage ; integrationTests/testFullpasses (15 succeeded, 0 failed), andtestDistro.pypasses every stage in 2 of 2 runs.