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

perf: Faster singleton SiblingSubgraph construction #1654

Merged
merged 4 commits into from
Nov 13, 2024
Merged

Conversation

aborgna-q
Copy link
Collaborator

@aborgna-q aborgna-q commented Nov 13, 2024

Wraps the topoConvexChecker used by SiblingSubgraph in a OnceCell so we can delay initialization until it's needed.

This lets us avoid traversing the graph (and computing a topological order) when creating sibling subgraphs with zero or one nodes.

This partially helps with #1652.

See benchmark results:

$ critcmp before after -f '.*subgraph'
group                      after                                  before
-----                      -----                                  ------
singleton_subgraph/10      1.00      3.0±0.08µs        ? ?/sec    2.71      8.1±0.18µs        ? ?/sec
singleton_subgraph/100     1.00      4.8±0.07µs        ? ?/sec    9.81     47.4±1.16µs        ? ?/sec
singleton_subgraph/1000    1.00     23.2±1.86µs        ? ?/sec    18.94  439.3±17.04µs        ? ?/sec
multinode_subgraph/10      1.01     17.9±0.25µs        ? ?/sec    1.00     17.7±0.29µs        ? ?/sec
multinode_subgraph/100     1.01    170.0±3.72µs        ? ?/sec    1.00    168.5±3.04µs        ? ?/sec
multinode_subgraph/1000    1.02      2.4±0.02ms        ? ?/sec    1.00      2.3±0.04ms        ? ?/sec

singleton_subgraph creates a single-node subgraph in a region with around k * 3 nodes.
multinode_subgraph creates a subgraph with 1/3rd of the nodes for the same region.

This PR is quite noisy since it's adding those two new benchmarks, and tidying up the files in the process.

drive-by: Add bench = false for all the targets in the workspace. Otherwise, the auto-added test harness threw errors when passing criterion flags to cargo bench.

@aborgna-q aborgna-q requested a review from a team as a code owner November 13, 2024 15:26
Copy link

codecov bot commented Nov 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.51%. Comparing base (935c61b) to head (1c993b4).
Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1654   +/-   ##
=======================================
  Coverage   85.50%   85.51%           
=======================================
  Files         136      136           
  Lines       25252    25264   +12     
  Branches    22164    22176   +12     
=======================================
+ Hits        21592    21604   +12     
  Misses       2456     2456           
  Partials     1204     1204           
Flag Coverage Δ
python 92.42% <ø> (ø)
rust 84.55% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

Copy link
Collaborator

@doug-q doug-q left a comment

Choose a reason for hiding this comment

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

Thank you!

@aborgna-q aborgna-q added this pull request to the merge queue Nov 13, 2024
@hugrbot
Copy link
Collaborator

hugrbot commented Nov 13, 2024

This PR contains breaking changes to the public Rust API.
Please deprecate the old API instead (if possible), or mark the PR with a ! to indicate a breaking change.

cargo-semver-checks summary

--- failure auto_trait_impl_removed: auto trait no longer implemented ---

Description:
A public type has stopped implementing one or more auto traits. This can break downstream code that depends on the traits being implemented.
      ref: https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits
     impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.36.0/src/lints/auto_trait_impl_removed.ron

Failed in:
type TopoConvexChecker is no longer Sync, in /home/runner/work/hugr/hugr/PR_BRANCH/hugr-core/src/hugr/views/sibling_subgraph.rs:457
type TopoConvexChecker is no longer RefUnwindSafe, in /home/runner/work/hugr/hugr/PR_BRANCH/hugr-core/src/hugr/views/sibling_subgraph.rs:457

Merged via the queue into main with commit e63878f Nov 13, 2024
22 of 23 checks passed
@aborgna-q aborgna-q deleted the ab/subgraph-perf branch November 13, 2024 15:53
@aborgna-q
Copy link
Collaborator Author

Uh, looks like I introduced a breaking change -.-
(TopoConvexChecker is no longer Sync now that it uses an OnceCell internally...)

@hugrbot hugrbot mentioned this pull request Nov 13, 2024
@doug-q
Copy link
Collaborator

doug-q commented Nov 13, 2024

Uh, looks like I introduced a breaking change -.- (TopoConvexChecker is no longer Sync now that it uses an OnceCell internally...)

Awk. I think we can put it in a Mutex to restore unbrokenness?

github-merge-queue bot pushed a commit that referenced this pull request Nov 15, 2024
Complementary improvement to #1654.
Creating a k-node subgraph in an n-node graph should ideally be `O(k)`.
However, due to CQCL/portgraph#155 this ends
up being `O(n)`.

For `k=1`, this results in a linear cost overhead.
This PR adds a special case (written by @doug-q) that completely skips
the unnecessary checks.

```
group                         before                                 from_node
-----                         ------                                 ---------
multinode_subgraph/10         1.01     17.7±0.26µs        ? ?/sec    1.00     17.5±0.23µs        ? ?/sec
multinode_subgraph/100        1.00   169.1±11.34µs        ? ?/sec    1.00    168.8±4.37µs        ? ?/sec
multinode_subgraph/1000       1.01      2.3±0.46ms        ? ?/sec    1.00      2.3±0.34ms        ? ?/sec
singleton_subgraph/10         12.26     3.0±0.06µs        ? ?/sec    1.00   245.6±21.24ns        ? ?/sec
singleton_subgraph/100        20.01     4.7±0.06µs        ? ?/sec    1.00    234.4±6.50ns        ? ?/sec
singleton_subgraph/1000       93.34    22.0±0.25µs        ? ?/sec    1.00    235.6±4.93ns        ? ?/sec
```

---------

Co-authored-by: Douglas Wilson <141026920+doug-q@users.noreply.github.com>
@hugrbot hugrbot mentioned this pull request Nov 15, 2024
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.

3 participants