Skip to content

Performance Relational Components Performance

github-actions[bot] edited this page Nov 9, 2026 · 15 revisions

Relational Component Performance Benchmarks

Relational component attributes ([SiblingComponent], [ParentComponent], [ChildComponent]) remove repetitive GetComponent* code. These benchmarks quantify the runtime cost of calling Assign*Components for common field shapes (single component, array, List<T>, and HashSet<T>) against hand-written lookups.

Numbers below are grouped by operating system. A section reading pending means nobody has run this suite on that platform, not that the attributes are slow there.

Parent and child queries use pooled typed buffers. A buffer containing Component appends through List<Component>.AddRange; buffers containing derived components retain typed enumeration. A List<DerivedComponent> does not implement ICollection<Component>, so passing it to AddRange misses the bulk-copy path and boxes its enumerator. Both transfer paths preserve query order and existing destination entries. The benchmark tables below predate this transfer change.

Collection traversal comparison (2026-11-08)

The raw samples, source hashes and process receipts compare the previous implementation with the current collection path on Windows x64, Unity 6000.6.7f1. Each cell uses seven rounds in each of three independent processes. The table gives candidate time divided by baseline time, using the median of process medians. A value below 1 is faster; ranges cover shallow and deep hierarchies.

Candidates Sealed fields, editor Mono Sealed fields, Release IL2CPP
0 0.83–0.84 0.89–0.92
1 0.77–0.86 0.92–0.94
4 0.52–0.52 0.45–0.46
16 0.47–0.47 0.36–0.36
64 0.42–0.42 0.30–0.31
500 0.41–0.43 0.28–0.28

These cells assign an array, list and set together, including the array allocation. They use SpriteRenderer for sealed fields. The corresponding BoxCollider fallback cells span 0.96–1.05 in editor Mono and 0.98–1.09 in IL2CPP; no universal fallback speedup is claimed. A broader fallback rewrite was rejected after a 1.20× regression in one deep nonsealed case; its negative example is in the evidence.

The fast path retains the native query for zero or one match and traverses breadth first for larger results. Its fallback callbacks live in a separate method, so bypassing that method does not create their closures. Three editor profiler controls for warmed sealed list/set fields (0, 1 and 64 matches) observe zero allocation events after verifying positive allocation and negative reuse controls. Arrays and first-use storage are outside that allocation claim.

These are latest-editor observations, not Unity 2021.3 floor, WebGL, cold-plan, retention or whole-audit qualification. Issue #644 remains open. Both arms' editor build logs contain the same temporary-allocator teardown warning; the measured player logs do not. The historical tables below predate these collection changes and use a different harness.

Bounded parent collections (2026-11-09)

Bounded parent queries now stop their walk at the requested depth or match count. Measurements recorded in #644 compare main 99e54b1e with the candidate on Windows x64, Unity 6000.6.7f1 Editor Mono with Release compilation. Each cell assigns an array, list and set together, uses 64 warm calls, calibrates batches to at least 20 ms, and records seven rounds in three fresh processes. Times below are medians of process medians in microseconds. Raw captures remain in ignored local storage. Replay RelationalParentCollectionTimingTests with category Performance to collect new samples. The measured parent source SHA256 is 9369800b3f8ed73471a2cbc6d6778688f30fa6852a45d95500f1eb040de0d4ac.

Ancestors Field type Bound Before After
4 Sealed Depth 2 3.602 4.263
16 Sealed Depth 2 12.862 4.315
500 Sealed Depth 2 7,448.150 4.230
500 Sealed Count 1 72.982 2.282
500 Base Count 1 225.540 3.421

The four-ancestor sealed depth case is 18% slower. Unbounded base-type cases span 1.02–1.08 times the baseline, while unbounded sealed cases span 0.97–1.02. Empty queries span 1.04 times the baseline. These controls limit the claim to bounded workloads; small queries do not receive a universal speedup. An earlier candidate with larger small-query regressions was rejected; its captures remain local alongside the accepted candidate.

Six native allocation checks cover warmed sealed list/set assignments with 0, 1 and 16 ancestors under depth and count bounds. Positive allocation and negative reuse controls run first. All six observe zero allocation events; the original depth-only baseline observes 256 events for the nonempty cases. Arrays, first use and arbitrary field types are outside this allocation claim. Independent hierarchy oracles cover interfaces, base and sealed fields, filters, inactive GameObjects, disabled components, reparenting and destroyed ancestors.

This capture does not qualify Unity 2021.3, IL2CPP, WebGL, cold plans or retention. Issue #644 remains open.

Windows (Editor/Player)

Last updated 2026-09-14 05:38 UTC on Windows 11 (10.0.26200).

Numbers capture repeated Assign*Components calls for one second per scenario. Higher operations per second are better.

Operations per second (higher is better)

Scenario Relational Ops/s Manual Ops/s Rel/Manual Iterations
Parent - Single 1,239,795 10,018,690 0.12x 1,240,000
Parent - Array 746,056 4,764,392 0.16x 750,000
Parent - List 802,040 7,396,326 0.11x 810,000
Parent - HashSet 777,660 3,955,524 0.20x 780,000
Child - Single 665,079 5,341,149 0.12x 670,000
Child - Array 293,349 3,229,543 0.09x 300,000
Child - List 285,215 3,783,105 0.08x 290,000
Child - HashSet 300,276 2,001,901 0.15x 310,000
Sibling - Single 4,636,847 28,450,407 0.16x 4,640,000
Sibling - Array 1,242,712 3,361,155 0.37x 1,250,000
Sibling - List 1,339,612 4,176,273 0.32x 1,340,000
Sibling - HashSet 1,260,522 2,255,225 0.56x 1,270,000

macOS

Pending: run the relational component benchmark suite on macOS to capture results.

Linux

Pending: run the relational component benchmark suite on Linux to capture results.

Other Platforms

Pending: run the relational component benchmark suite on the target platform to capture results.

Refreshing these numbers

Run RelationalComponentBenchmarkTests.Benchmark from Unity's Test Runner. It rewrites the section matching the operating system it ran on and leaves the others alone.

Replaying collection comparisons in a player

The separate player compares complete child assignments (array, list, and set in one call) and IntMap clear/refill cycles. It has 24 child scenarios: 0, 1, 4, 16, 64, or 500 candidates, shallow or deep, with sealed SpriteRenderer and nonsealed BoxCollider fields. Ten IntMap cases include sparse tables and both sides of the clearing threshold. It checks results and emits seven timing rounds per case.

  1. Create a separate Windows Unity project and install the package from the source checkout to measure. Keep the project outside that checkout.
  2. Copy the player into Assets, and copy the builder into Assets/Editor.
  3. Copy the BoxCollider target and the SpriteRenderer target into Assets. Copy these two source files only; the player needs no test assembly.
  4. Run Unity in batch mode with -projectPath pointing to that project and -executeMethod WallstopStudios.UnityHelpers.Benchmarks.RelationalCollectionPlayerBuild.Build. The builder selects a non-development IL2CPP Release build and writes Build/CollectionBenchmark/CollectionBenchmark.exe under the project.
  5. Run that executable with -batchmode -nographics -logFile player.log -uhBenchmarkOutput samples.txt. Require exit zero, the success message, and exactly 238 samples plus the environment line. Keep the build and player logs.
  6. Freeze each arm's full player directory, record source and binary hashes, and alternate three independent baseline and candidate processes with other builds stopped. Compare process medians and the best process for each case. Never mix editor and player absolute times or replace a failed process with leaf samples.

The player warms each case 64 times, calibrates to at least 20 ms, and checks a sleep control against the high-resolution timer. It includes delegate overhead and array allocation in child assignments. This is a scoped workload, not a qualification of every relation, backend, Unity version, or consumer workload.

The equivalent NUnit fixtures are RelationalChildCollectionTimingTests and IntMapSparseClearTimingTests. They belong to the Performance category and do not add timing loops to ordinary correctness CI. Their editor timings use a different harness; compare arms within one harness and runtime.

📚 Documentation

Getting Started

Inspector Features

Effects System

Relational Components

Serialization

Spatial Trees

Logging

Utilities

Editor Tools

Guides

Performance

Project

Clone this wiki locally