Skip to content

Conversation

@acl-cqc
Copy link
Contributor

@acl-cqc acl-cqc commented Aug 12, 2025

closes #2500, #2496

  • New trait methods can insert several subtrees at once at different locations in target Hugr
  • trait-default-implement insert_hugr, insert_view and insert_subgraph using these

Technically I think this is breaking, any external impl HugrMut now has to provide these new methods, but we believe implementations of HugrMut outside of hugr-core to be sufficiently rare not to justify a major-version-number release, and rs-semver-checks does not seem to pick up on this.

@codecov
Copy link

codecov bot commented Aug 12, 2025

Codecov Report

❌ Patch coverage is 96.62921% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.76%. Comparing base (75fd767) to head (57e58e2).
⚠️ Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
hugr-core/src/hugr/views/rerooted.rs 0.00% 2 Missing ⚠️
hugr-core/src/hugr/views/impls.rs 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2518      +/-   ##
==========================================
+ Coverage   82.66%   82.76%   +0.09%     
==========================================
  Files         252      252              
  Lines       46801    47065     +264     
  Branches    42330    42581     +251     
==========================================
+ Hits        38689    38952     +263     
+ Misses       6061     6055       -6     
- Partials     2051     2058       +7     
Flag Coverage Δ
python 91.41% <ø> (+0.02%) ⬆️
rust 81.85% <96.62%> (+0.10%) ⬆️

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.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@acl-cqc acl-cqc requested a review from aborgna-q August 12, 2025 14:50
@acl-cqc acl-cqc marked this pull request as ready for review August 12, 2025 14:50
@acl-cqc acl-cqc requested a review from a team as a code owner August 12, 2025 14:50
@acl-cqc acl-cqc requested a review from aborgna-q August 17, 2025 21:28
@acl-cqc acl-cqc force-pushed the acl/insert_forest branch from 1d23bc8 to cb92e0c Compare August 19, 2025 11:03
Comment on lines 85 to 92
// Note it would be better to use iter_batched to avoid cloning nodes/roots.
let nodes = insert.entry_descendants().chain([defn.node(), decl.node()]);
let roots = [
(insert.entrypoint(), h.entrypoint()),
(defn.node(), h.module_root()),
(decl.node(), h.module_root()),
];
b.iter(|| black_box(h.insert_view_forest(&insert, nodes.clone(), roots.iter().cloned())))
Copy link
Collaborator

Choose a reason for hiding this comment

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

You are modifying h on each iteration, so it becomes bigger and bigger each time!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but I don't think that should matter - we are not, e.g., copying h (or validating it) on each iteration; it's only if resizing nodes arrays etc. where this might hurt....and that should be amortized, right?

Copy link
Contributor Author

@acl-cqc acl-cqc Aug 19, 2025

Choose a reason for hiding this comment

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

I can change it if you think this is important??

Copy link
Collaborator

@aborgna-q aborgna-q Sep 1, 2025

Choose a reason for hiding this comment

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

Each benchmark run should ideally have the same context, to avoid noise.
Re-using the hugr has implications in vector allocations, and cache hits/misses.

We can just use a Hugr::new() here.

@acl-cqc acl-cqc requested a review from aborgna-q August 19, 2025 16:08
#[display(
"Subtree rooted at {subtree} is already being copied as part of that rooted at {parent}"
)]
SubtreeAlreadyCopied {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Possibly this would be better called SubtreesNotDisjoint or SubtreesOverlap or something like that

Copy link
Collaborator

@aborgna-q aborgna-q left a comment

Choose a reason for hiding this comment

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

LGTM, just fix the benchmark before merging

@acl-cqc
Copy link
Contributor Author

acl-cqc commented Sep 2, 2025

Ok after updating benchmarks and making the other factors the same on both sides, this comparison shows the improvement that could be gained by requiring the other_nodes iterator to be in order (and not requiring said iterator to be Clone): https://codspeed.io/CQCL/hugr/runs/compare/68b5d6779504c8fab797415c..68b6d66cc116c5dd4ccf13d0 as in commit e1a9674 (diff)
image
image
image
I think that's small enough that I'm merging.

@acl-cqc acl-cqc added this pull request to the merge queue Sep 2, 2025
Merged via the queue into main with commit 4b38008 Sep 2, 2025
26 checks passed
@acl-cqc acl-cqc deleted the acl/insert_forest branch September 2, 2025 13:16
@hugrbot hugrbot mentioned this pull request Sep 1, 2025
github-merge-queue bot pushed a commit that referenced this pull request Sep 15, 2025
## 🤖 New release

* `hugr-model`: 0.22.2 -> 0.22.3 (✓ API compatible changes)
* `hugr-core`: 0.22.2 -> 0.22.3 (✓ API compatible changes)
* `hugr-llvm`: 0.22.2 -> 0.22.3 (✓ API compatible changes)
* `hugr-passes`: 0.22.2 -> 0.22.3 (✓ API compatible changes)
* `hugr-persistent`: 0.2.2 -> 0.2.3 (✓ API compatible changes)
* `hugr`: 0.22.2 -> 0.22.3 (✓ API compatible changes)
* `hugr-cli`: 0.22.2 -> 0.22.3 (✓ API compatible changes)

<details><summary><i><b>Changelog</b></i></summary><p>

## `hugr-model`

<blockquote>

##
[0.22.2](hugr-model-v0.22.1...hugr-model-v0.22.2)
- 2025-08-06

### New Features

- Type of constants in `core` `Term`s.
([#2411](#2411))
</blockquote>

## `hugr-core`

<blockquote>

##
[0.22.3](hugr-core-v0.22.2...hugr-core-v0.22.3)
- 2025-09-11

### Bug Fixes

- SiblingSubgraph::try_from_nodes not including disconnected components
([#2549](#2549))

### Documentation

- Clarify docs for SiblingSubgraph::{inputs, outputs}
([#2508](#2508))

### New Features

- SiblingSubgraph supports function calls
([#2528](#2528))
- Add unchecked constructor for SiblingSubgraph
([#2526](#2526))
- Add HugrMut::insert(_view)_forest
([#2518](#2518))
- Add extend_inputs function for DFGs
([#2536](#2536))
- Loosen bound on Patch trait
([#2545](#2545))
</blockquote>

## `hugr-llvm`

<blockquote>

##
[0.22.2](hugr-llvm-v0.22.1...hugr-llvm-v0.22.2)
- 2025-08-06

### Bug Fixes

- added public func getter for EmitFuncContext
([#2482](#2482))
- *(hugr-llvm)* Set llvm function linkage based on Visibility hugr node
field ([#2502](#2502))
</blockquote>

## `hugr-passes`

<blockquote>

##
[0.22.3](hugr-passes-v0.22.2...hugr-passes-v0.22.3)
- 2025-09-11

### New Features

- SiblingSubgraph supports function calls
([#2528](#2528))
</blockquote>

## `hugr-persistent`

<blockquote>

##
[0.2.3](hugr-persistent-v0.2.2...hugr-persistent-v0.2.3)
- 2025-09-11

### Documentation

- Clarify docs for SiblingSubgraph::{inputs, outputs}
([#2508](#2508))

### New Features

- SiblingSubgraph supports function calls
([#2528](#2528))
</blockquote>

## `hugr`

<blockquote>

##
[0.22.3](hugr-v0.22.2...hugr-v0.22.3)
- 2025-09-11

### Bug Fixes

- SiblingSubgraph::try_from_nodes not including disconnected components
([#2549](#2549))

### Documentation

- Clarify docs for SiblingSubgraph::{inputs, outputs}
([#2508](#2508))

### New Features

- SiblingSubgraph supports function calls
([#2528](#2528))
- Add unchecked constructor for SiblingSubgraph
([#2526](#2526))
- Add extend_inputs function for DFGs
([#2536](#2536))
- Loosen bound on Patch trait
([#2545](#2545))
- Add HugrMut::insert(_view)_forest
([#2518](#2518))
</blockquote>

## `hugr-cli`

<blockquote>

##
[0.22.3](hugr-cli-v0.22.2...hugr-cli-v0.22.3)
- 2025-09-11

### New Features

- *(hugr-cli)* CliError::validate helper
([#2507](#2507))
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/release-plz/release-plz/).
@acl-cqc acl-cqc mentioned this pull request Sep 17, 2025
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.

HugrMut: add new insertion method(s)

3 participants