← Back to Pull requests

Managed view hierarchy: RectTransform corners without GetWorldCorners' transform call #296

Closed
unknown closed this pull request without merging on Nov 26, 2026 12:35 UTC
View on GitHub
unknown unknown commented on Nov 26, 2026 12:15 UTC
Author

What changed

Optimisation #3 of the scene-read research: the managed view hierarchy walker no longer calls RectTransform.GetWorldCorners per UI node. On Unity 2021.3 and 2022.3 that method reads this.transform (a native call) before localToWorldMatrix; the walker now reads rect and localToWorldMatrix and runs the same arithmetic in a pure helper, ManagedViewCorners (Runtime/WrapperPolicy): same four-corner order (bottom-left, top-left, top-right, bottom-right) and the same MultiplyPoint operation order, including the divide by w. On 6000.0 GetWorldCorners already has no transform read, so nothing is saved there.

Also in the sample harness: vhscale takes an optional bugsee_vh_counts list, logs usPerNode and a bit-for-bit corner check; vhscene fails on a corner mismatch.

Optimisation #2 (GetInstanceID -> GetHashCode) was NOT done: the premise is wrong in the player

The research read the editor assembly, where Object.GetHashCode() is return m_InstanceID. The player assemblies differ. In PlaybackEngines/AndroidPlayer/Variations/il2cpp/Managed/UnityEngine.CoreModule.dll of 2021.3.45f1, 2022.3.62f3 and 6000.0.84f1, Object.GetHashCode() is base.GetHashCode() (the CLR object hash), while GetInstanceID() reads the id from the native object's memory (m_CachedPtr + OffsetOfInstanceIDInCPlusPlusObject) with no icall. On the HONOR (IL2CPP release, 2021.3.45f1) a first build that used GetHashCode failed the check for all 1002 objects (idMismatches=1002), so scene-block ids, drawnBy and camera ids would have stopped matching. The ids stay on GetInstanceID(); its real cost there is about 0.37 to 0.40 us per call against 0.06 for GetHashCode, so there was little to gain anyway.

How it was verified

  • Tests~/WrapperPolicy: 2603 passed, including 10 new ManagedViewCornersTests: 8 cases whose expected values were produced by Unity 2021.3.45f1 itself (plain, offset pivot with scale, rotated, mirrored, mirrored and rotated, tilted in 3D, rotated and scaled parent with rotated child, zero size) plus order and 90 degree tests, compared with exact equality.

  • sh Tools~/scripts/compile-check.sh: all 11 configurations ok.

  • HONOR WOD-LX1, IL2CPP release arm64, Unity 2021.3.45f1: [VHSCALE] reads check PASS (401 RectTransforms, 0 corner mismatches, bit for bit against GetWorldCorners).

  • Byte-identical output: vhscene (development build) on the same device with main's walker and with this change: 45 PASS, 0 FAIL each, reply 3438 chars, identical md5 (40af080c...); a second launch of main's build differed only in the frame counter (15 vs 16).

  • Before/after, vhscale, IL2CPP release, 4 launches with data each in alternating order, 8 captures per N per launch (medians of main-thread ms per tree node; the walker reads only part of the scene within its slice budget, so a capture has a few hundred nodes):
  • | N | before | after | change | per-launch medians before | per-launch medians after |
    |---|---|---|---|---|---|
    | 1000 | 25.79 us/node | 24.30 | -5.8% | 23.9, 26.0, 26.0, 26.7 | 23.6, 24.5, 23.6, 28.2 |
    | 5000 | 19.03 us/node | 17.68 | -7.1% | 18.0, 19.8, 17.2, 20.0 | 17.2, 18.3, 17.4, 18.7 |

    The launch-to-launch spread (about 2 to 5 us) is as large as the difference, and the microbenchmark says the real saving is small: GetWorldCorners 2.25 us vs own corners 1.72 to 1.86 us per RectTransform (about 0.4 us, roughly 0.15 us averaged over all nodes). Treat the gain as small and partly within noise. (One launch per variant was lost to an app that did not start and was re-run.)

  • iPhone XS (KRSFT) was offline, so not measured; no Unity 2022.3 or 6000.0 device run.
  • Evaluation of #4 / #10 (bulk enumeration), no production change

    See ENUM-BENCH-RESULTS.md on branch spike/vh-enum-bench (EnumBench.cs, HONOR, IL2CPP release). GetComponentsInChildren<Transform>(false, list) costs 0.9 to 1.2 us per returned node against 1.7 to 2.9 for the bare walk, but rebuilding the tree needs parent (0.55 to 0.7 us) plus an id lookup: 1.7 to 2.3 us, no gain over the walk. The returned order was pre-order and sibling-equal in every configuration (booleans only, not relied on). A bulk call is not sliceable: 5.5 ms at 4889 nodes, 20 ms at 18889 nodes under one root (budget 0.83 ms), 2.7 ms for the largest of 8 roots at 20000. FindObjectsByType<Transform>(None): 1.5 to 5.2 us per node, up to 37 ms in one call, whole process. Recommendation: reject.

    Known gaps

    No iPhone, 2022.3 or 6000.0 device numbers; no Mono build measured. The fresh Opus review found 2 low issues (loose ulp tolerance, README for the vhscale extras), both fixed in the last commit; no second review pass was run for that test and doc only follow-up.

    🤖 Generated with Claude Code

    https://claude.ai/code/session_01WeiJxQSiPP4vp79ZPpBsqf

    Diff: 608 lines (+456 / -152) across 7 files Reviewer: krassx Review duration: 19m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 608 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.384
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #296"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.1% (MEDIUM) · Higher = larger blast radius
    50.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.8% · Safe to merge
    19.8%