Title: Proposal: batch relationship handling for ExtractElements / append_asset (measured ~200x on a real model) · Issue #8952 · IfcOpenShell/IfcOpenShell · GitHub
Open Graph Title: Proposal: batch relationship handling for ExtractElements / append_asset (measured ~200x on a real model) · Issue #8952 · IfcOpenShell/IfcOpenShell
X Title: Proposal: batch relationship handling for ExtractElements / append_asset (measured ~200x on a real model) · Issue #8952 · IfcOpenShell/IfcOpenShell
Description: Raising this as a design question rather than a patch, because the interesting parts are decisions that belong to you rather than to us. Found while investigating #8945, where extracting 2,004 walls from a 495 MB IFC2X3 model takes 12.8 ...
Open Graph Description: Raising this as a design question rather than a patch, because the interesting parts are decisions that belong to you rather than to us. Found while investigating #8945, where extracting 2,004 wall...
X Description: Raising this as a design question rather than a patch, because the interesting parts are decisions that belong to you rather than to us. Found while investigating #8945, where extracting 2,004 wall...
Opengraph URL: https://github.com/IfcOpenShell/IfcOpenShell/issues/8952
X: @github
Domain: github.com
IPv4 address: 140.82.113.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Proposal: batch relationship handling for ExtractElements / append_asset (measured ~200x on a real model)","articleBody":"Raising this as a design question rather than a patch, because the interesting parts are decisions that belong to you rather than to us.\n\nFound while investigating #8945, where extracting 2,004 walls from a 495 MB IFC2X3 model takes 12.8 minutes.\n\n## The measured problem\n\n`ExtractElements` extracts a subset by calling `ifcopenshell.api.project.append_asset()` once per selected element. That function is correct for appending one asset, but it re-walks the inverse relationship graph on every invocation, including relationships shared by many elements.\n\nOn the real model, 2,004 walls take 769.9s, about 369 ms per wall, and the cost is flat from N=25 to N=2004. So it is linear with a very large constant, not a scaling problem.\n\ncProfile at N=250 shows where the constant goes:\n\n- **57.1%** in `is_another_asset` and the `by_guid` calls it triggers, walking the full `RelatedObjects` tuple of every shared relationship once per participating element. In this file one wall type's defining relationship has about 1,300 members, and each of the 2,004 selected walls re-walks it in full.\n- **18.8%** in `SafeRemovalContext.__exit__`, caused by a separate issue: the internal recursive type-append in `append_product` omits `assume_asset_uniqueness_by_name`, so it silently falls back to the default `True` even though the recipe explicitly sets `False`. We have a separate fix in flight for that one.\n- The remaining ~24% is Python/C++ boundary overhead proportional to the same re-walk.\n\nRoughly 76% of the time is work that only needs doing once per shared relationship rather than once per element.\n\n## Proposed mechanism\n\nGroup by relationship id up front instead of by element:\n\n1. For each selected element, read its own inverse relationships (`HasAssociations`, `IsDefinedBy`, type assignment). These are small, cheap, per-element lookups that never touch the large member side.\n2. Accumulate a map from relationship id to the set of selected elements referencing it.\n3. Build each filtered relationship once, at the end, from that map.\n4. Let `file.add()` handle bulk copying. It already deduplicates shared forward-reachable entities at the C++ level, so no engine work is needed. It deliberately does not touch the relationship side, which is correct, since doing so would drag in every other member.\n\n## Evidence\n\nAll on the real 495 MB file, running unmodified `append_asset.py` and `ExtractElements.py` (diffed byte-identical against source to rule out a stale sandbox).\n\nFull N=2004: closure 0.86s (339,006 entities, 15,567 relationship groups), copy and relate 1.60s, write 1.16s. **Total 3.62s against roughly 755s of algorithmic work today**, excluding the ~15s parse that is unavoidable either way.\n\nSame process, N=100, both paths back to back: **47.16s versus 0.76s**.\n\nCorrectness at N=100: 100/100 walls present in both. GlobalId sets 1,756 versus 1,716, 95.8% match. Of 67 classes, 32 differ in count, and **every difference is smaller in the batch output, never larger**. No fan-out to sibling walls of a shared type, no duplication of shared types or materials.\n\nThe 4.2% gap is fully accounted for, not unexplained: the prototype does not yet implement `StyledByItem`, `LayerAssignment(s)`, material `HasRepresentation`, or `HasShapeAspects`, which explains the missing styling and shape-aspect entities and the geometry that cascades from them. Those are mechanical additions to a pattern already working for materials, property sets and types.\n\n## The questions that are actually yours\n\n**1. `IfcOwnerHistory` semantics.** The current path mints a fresh `IfcOwnerHistory` on each type assignment via `assign_type`. The prototype reuses the original. Reusing arguably reflects reality better for an extraction, since nothing was edited and a synthetic edit trail is misleading, but that is a decision about what an extracted file's provenance should say. We did not want it decided implicitly inside a performance change.\n\n**2. Where should this live?** Inside `append_asset.py` as a batch entry point, or in `ExtractElements.py` calling a lower-level primitive? `append_asset` is used well beyond ifcpatch, including Bonsai's own append flows, so this needs your steer. Note there is also a separate constant-factor optimisation pass in flight on that same file; this is a different lever and the two are not mutually exclusive, but they need sequencing.\n\n**3. Which relationship classes are in scope?** The prototype handles only those present in this one file. A real version needs the full whitelist from `append_asset.py` and confirmation that each batches the same way without behaviour change.\n\n## Honesty check\n\nThis is a prototype built to answer \"is this worth proposing\", not production code. It has been run against exactly one model, never against Bonsai's append flows, and four whitelist branches are knowingly missing rather than merely untested. The speed numbers are real, measured and reproducible. The correctness claim is \"structurally right, demonstrably incomplete in specific enumerated ways\", not \"done\".\n\nIf the mechanism looks sound we are happy to build it properly. We would rather have your answer on the owner-history question and the placement question before writing anything real.\n\ncc @aothms @Moult\n\nThis proposal was prepared with the assistance of an AI tool.\n","author":{"url":"https://github.com/petruconduraru","@type":"Person","name":"petruconduraru"},"datePublished":"2026-07-25T07:07:47Z","url":"https://github.com/IfcOpenShell/IfcOpenShell/issues/8952"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:c5a97d4c-0407-ad63-ad10-21c823e48f89 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E6AA:2D6F70:4E6773:6E2CC8:6B0472B1 |
| html-safe-nonce | b9b140e4c80f3a2cdfe76f273ef8b82c32d99a2a7c260fb01bebb8dce0e4f370 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNkFBOjJENkY3MDo0RTY3NzM6NkUyQ0M4OjZCMDQ3MkIxIiwidmlzaXRvcl9pZCI6IjY4NzUwMzEzODg4OTk1Mzc1ODUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | fec506008b7a1eeb7a28afbda8eefb7c42edf56507b6556e16d3c9c640dc05fc |
| hovercard-subject-tag | issue:4974694820 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/IfcOpenShell/IfcOpenShell/8952/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d61c52e171815a8868f135ac7043751cc3623ad79788ff317b709981525c6d33/IfcOpenShell/IfcOpenShell/issues/8952 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d61c52e171815a8868f135ac7043751cc3623ad79788ff317b709981525c6d33/IfcOpenShell/IfcOpenShell/issues/8952 |
| og:image:alt | Raising this as a design question rather than a patch, because the interesting parts are decisions that belong to you rather than to us. Found while investigating #8945, where extracting 2,004 wall... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | petruconduraru |
| hostname | github.com |
| expected-hostname | github.com |
| None | ae8802492f12d3ba4f6b0d814054e617a2a5616b6327bf8555c10cd5b0dc2a6f |
| turbo-cache-control | no-preview |
| go-import | github.com/IfcOpenShell/IfcOpenShell git https://github.com/IfcOpenShell/IfcOpenShell.git |
| octolytics-dimension-user_id | 13692563 |
| octolytics-dimension-user_login | IfcOpenShell |
| octolytics-dimension-repository_id | 40473201 |
| octolytics-dimension-repository_nwo | IfcOpenShell/IfcOpenShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 40473201 |
| octolytics-dimension-repository_network_root_nwo | IfcOpenShell/IfcOpenShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | cfb5f24c6e01d85f70804652a5107e5cac03a161 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width