Skip to content

Fix non-determinism in toposort by using input order as a tie-breaker#998

Merged
copybara-service[bot] merged 1 commit intomainfrom
test_912785204
May 9, 2026
Merged

Fix non-determinism in toposort by using input order as a tie-breaker#998
copybara-service[bot] merged 1 commit intomainfrom
test_912785204

Conversation

@copybara-service
Copy link
Copy Markdown

@copybara-service copybara-service Bot commented May 9, 2026

Fix non-determinism in toposort by using input order as a tie-breaker

When toposort has multiple nodes ready to process (no remaining
predecessors), it puts them in a BinaryHeap and uses the preferred_order
closure to sort them. If the closure says two nodes are equal, their order in
the heap depended on HashMap iteration order, which is non-deterministic.

We fix this by recording each node's index in the input iterator. If the
closure returns Equal, we now fall back to comparing these input indices,
preferring nodes that came first. This makes the sort fully deterministic.

@google-cla
Copy link
Copy Markdown

google-cla Bot commented May 9, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@copybara-service copybara-service Bot changed the title Sort nodes in generate_bindings before passing to toposort Fix non-determinism in toposort by using input order as a tie-breaker May 9, 2026
@copybara-service copybara-service Bot force-pushed the test_912785204 branch 3 times, most recently from f0f1dac to c743b98 Compare May 9, 2026 14:49
When `toposort` has multiple nodes ready to process (no remaining
predecessors), it puts them in a `BinaryHeap` and uses the `preferred_order`
closure to sort them.  If the closure says two nodes are equal, their order in
the heap depended on `HashMap` iteration order, which is non-deterministic.

We fix this by recording each node's index in the input iterator. If the
closure returns `Equal`, we now fall back to comparing these input indices,
preferring nodes that came first. This makes the sort fully deterministic.

PiperOrigin-RevId: 912985573
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.

0 participants