Skip to content

Performance enhancements: startup and visualization toggles - #303

Merged
diegopinate merged 14 commits into
masterfrom
diegop/performance-enhancements
Nov 4, 2026
Merged

diegopinate merged 14 commits into
masterfrom
diegop/performance-enhancements

Conversation

@diegopinate

@diegopinate diegopinate commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Version compare performance improvements

Five independent fixes to the comparison startup and interaction paths. Measured against a ~10 GB iModel and a 3+ million element comparison result (860 MB full / 265 MB property-less payload).

Changes

  • ModelsCategoryCache: stop querying the model of every updated element when results already carry modelId.
  • ChangedElementEntryCache: replace full-cache scans in getEntries / per-model lookups with Map lookups and a modelId → ids index, this enhances the performance of the tree provider and visualization.
  • VersionCompareTiles: build secondary-iModel overrides lazily (once per invalidation instead of twice per filter change) and partition entries by opcode once instead of on every pass.
  • parseJsonResponse: use native JSON.parse for identity-encoded payloads under the 256 MiB property threshold; keep the incremental parser for larger or encoded bodies.
  • ChangedElementsManager: release the raw accumulated change Map after entries are generated; the entry cache is the single source of truth.

Benchmark

Stage Before After
ModelsCategoryCache.load 46.4 s 0.9 s
getEntries(1 id) × 20 6.5 s < 1 ms
getChangeElementIdsFromModelId × 10 2.5 s < 1 ms
Expand largest model node (~275 k elements) 13.4 s 6.1 s
Provider.addFeatureOverrides 2.3 s 1.0 s
Filter toggle (override rebuild) 8.0 s 4.6 s
Retained heap after initialize 830 MB 469 MB

Comparison startup on the tested dataset drops from ~90 s to ~40 s.

diegopinate and others added 7 commits October 30, 2026 16:54
…elId

ModelsCategoryCache.load queried the target iModel for the model of every
updated element (800 ids per ECSQL statement) even when the changed
elements data already carried the modelId. Deleted elements already had
this shortcut; updated elements did not.

Apply the same split to updated elements: take the modelId from the change
data when present and only query for elements that lack it.

For comparisons where the Changed Elements API returns modelIds (the
normal case) this removes one round-trip per 800 updated elements, which
on multi-million-element comparisons was ~half of comparison startup time
(tens of seconds, thousands of queries). With modelIds present the cost
is now a single pass over the entries.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
ChangedElementEntryCache.getEntries materialized every cached entry via
getAll() and filtered it linearly, and getChangeElementIdsFromModelId did
the same per model. The changes tree provider and the visualization also
scanned the full entry collection each time a single model was expanded or
toggled. On multi-million-element comparisons each of these calls cost
hundreds of milliseconds, and they are issued per row / per model, so
expanding a model node or rendering the inspector list could spend seconds
in scans alone.

- getEntries now resolves ids directly from the entry Map (children too).
- The cache maintains a modelId -> element ids index, updated wherever
  entries are written (initialize, _overrideEntriesInCache, cleanup), and
  exposes getEntriesWithModel for per-model queries.
- ChangesTreeDataProvider.getModelElementEntries,
  getModelAllChildElementEntries and _getChangedModelChildrenById use the
  index instead of scanning all entries / all root entries.
- VersionCompareVisualization builds a one-time modelId -> entries map
  instead of scanning its entry array on every model visibility check.

Expected savings on large comparisons: per-id lookups drop from ~100-300 ms
to sub-millisecond, per-model id lookups drop from ~250 ms to effectively
free, and expanding a model node in the changes tree roughly halves (the
remainder is the ECSQL load of its children).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The tile Provider rebuilt the secondary iModel FeatureSymbology.Overrides
eagerly in its constructor, in updateOptions, and again from the viewport's
areFeatureOverridesDirty callback that updateOptions itself triggers. A
single inspector filter change (setFocusedElements followed by
toggleUnchangedVisibility) therefore rebuilt the overrides at least twice,
and each rebuild plus every addFeatureOverrides call re-filtered the whole
visible entry array by opcode and allocated fresh id Sets. On
multi-million-element comparisons each rebuild costs several seconds and
over a hundred MB of transient allocations.

- secondaryIModelOverrides is now a getter that rebuilds only on first read
  after an invalidation; constructor, updateOptions, setTransparency,
  setChangedElems and the viewport dirty callback just invalidate, so
  back-to-back changes collapse into one build per frame.
- visibleChangedElems is a setter that partitions entries into
  inserted/updated/deleted once; addFeatureOverrides and initOverrides use
  the partitions instead of filtering the full array on every call.
- The unfiltered deleted/updated id Sets are cached per partition and reused
  when no never-drawn / hidden-deleted filters apply.

Expected savings on large comparisons: roughly 30% off every primary
override pass, one full secondary override rebuild removed per filter
change (about a third of the filter toggle time), and the corresponding
Set churn eliminated. Remaining cost is the per-element override
registration in core-frontend.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
parseJsonResponse always streamed the body through the incremental JSON
parser whenever a body stream was present. That parser exists only to get
past V8's ~512 MiB string limit, but it is several times slower than
native JSON.parse, so every comparison result, including ones far below
the limit, paid the penalty on the critical path of opening a comparison.

When Content-Length is known and under 480 MiB (UTF-8 bytes >= UTF-16 code
units, so the decoded text always fits) the body is now parsed with
response.json(); readChangedElementsPayload already strips the omitted
property keys afterwards for oversized payloads. Larger or unsized bodies
keep using the incremental parser.

Expected savings: payload parsing drops to roughly a third of its previous
time for results below the limit (several seconds on multi-hundred-MB
payloads); results above the limit are unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
ChangedElementsManager accumulated the service payload into a
Map<string, ChangedElement>, then ChangedElementEntryCache copied every
element into a ChangedElementEntry. The raw Map was kept alive for the
whole comparison session: the manager held it, the children cache held a
second reference to it for id membership checks, and
getAllChangedPropertyNames iterated it. For multi-million-element
comparisons that is a full duplicate of the change data (hundreds of MB)
retained with no reader other than those two lookups.

- The children cache now receives the entry map (same key set) instead of
  the raw Map.
- getAllChangedPropertyNames reads properties from the entry cache.
- ChangedElementsManager.initialize clears the raw Map after
  generateEntries; the entry cache is the single source of truth from then
  on. Entry objects share the per-element property Maps by reference, so
  nothing is lost.

Expected savings: roughly 40% less retained heap after comparison startup
on large results, which also lowers the peak reached during initialLoad and
leaves more headroom for the viewport.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@changeset-bot

changeset-bot Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e37bacc

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@itwin/changed-elements-react Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved issues can misclassify unchanged elements, hide original geometry, fail comparisons involving large cross-origin downloads, and break assignments to a previously writable public property.

5 open issues
What changed in this PR

Targets faster startup and visualization toggles for large version comparisons in changed-elements-react.

Changes:

  • Adds ID/model indexes and reuses supplied model IDs to reduce scans and queries.
  • Lazily rebuilds visualization overrides and caches opcode/model groupings.
  • Adds a native JSON parsing fast path, releases raw change data, and adds focused tests.
File Description
packages/​changed-elements-react/​src/​tests/​VersionCompareTiles.test.ts Tests opcode partitions and lazy override rebuilding.
packages/​changed-elements-react/​src/​tests/​ParseJsonResponse.test.ts Tests native parsing selection and incremental fallback.
packages/​changed-elements-react/​src/​tests/​ModelsCategoryCache.test.ts Tests skipping queries when model IDs are supplied.
packages/​changed-elements-react/​src/​tests/​ChangedElementEntryCache.test.ts Tests indexed lookups, reassignment, and cleanup.
packages/​changed-elements-react/​src/​clients/​common.ts Selects native parsing for eligible responses.
packages/​changed-elements-react/​src/​api/​VersionCompareVisualization.ts Caches changed entries grouped by model.
packages/​changed-elements-react/​src/​api/​VersionCompareTiles.ts Defers override construction and caches opcode partitions.
packages/​changed-elements-react/​src/​api/​ModelsCategoryCache.ts Reuses supplied model IDs for updated elements.
packages/​changed-elements-react/​src/​api/​ChangesTreeDataProvider.ts Uses indexed per-model entry lookups.
packages/​changed-elements-react/​src/​api/​ChangedElementsManager.ts Releases raw changes and exposes entry-cache data afterward.
packages/​changed-elements-react/​src/​api/​ChangedElementsChildrenCache.ts Accepts a read-only map for child-change checks.
packages/​changed-elements-react/​src/​api/​ChangedElementEntryCache.ts Adds model indexing, direct lookups, and entry-map sharing.

Review effort: Max

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The change still has a critical unknown-model path that throws instead of returning an empty set, and mutable element replacements can leave opcode partitions stale and produce incorrect override behavior.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

High severity Handle unknown models in the Set constructor

packages/​changed-elements-react/​src/​api/​ChangedElementEntryCache.ts:479

Map.get returns undefined for an unknown model, and new Set(undefined) throws a TypeError instead of returning the empty set promised by this method and exercised by the new 0xnone test. Use an empty iterable when the index has no entry.

4 resolved since last review

Review effort: Lite

@diegopinate
diegopinate marked this pull request as ready for review November 3, 2026 14:11
@diegopinate
diegopinate requested a review from a team as a code owner November 3, 2026 14:11

@mattbjordan mattbjordan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Seems like a lot of solid performance improvements based on knowledge we've built up over time. I'm not seeing anything out of line, except for one comment that I've added.

@diegopinate
diegopinate merged commit bec9a08 into master Nov 4, 2026
4 checks passed
@diegopinate
diegopinate deleted the diegop/performance-enhancements branch November 4, 2026 13:43
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