Commit da72e34
[ui] Fix MiddleTruncate canvas text-measurement perf regression (#25566)
## Summary
Minimal fix for the perf regression introduced by #25178. **Scoped to the regression only** — a follow-up PR handles the pre-existing forced-reflow cost.
#25178 began copying `letterSpacing`, `textRendering`, `fontKerning`, `wordSpacing`, and `fontStretch` onto the canvas 2D context used to measure text for middle truncation. Assigning text-shaping properties like `letterSpacing`/`textRendering` forces the browser **off its fast text-measurement path**, making every `measureText` call dramatically slower. That cost is multiplied by the per-instance binary search in `calculateMiddleTruncation` and by the number of rendered rows.
A profiled asset-catalog load (group-by-Group, expanding groups) showed a single ~25s main-thread task with **~14s** of self-time in `measureText`.
### Origin
The header tracking that exposed this came from #25091 (`Heading` component → `letter-spacing: -0.02em`). #25178 then made the measuring canvas honor that tracking — correct, but on the slow path.
### Fix
Assign only `font` (which carries font-stretch via the shorthand) to the context, and account for `letterSpacing` **arithmetically** in the measure callback. This preserves #25178's correctness fix (headers no longer mis-wrap) while restoring the fast `measureText` path.
The canvas create/append/remove lifecycle is **intentionally unchanged** here. The remaining ~6s of forced-reflow cost (`document.body` append/remove per measurement) predates #25178 and is addressed in a stacked follow-up.
## Test Plan
- `yarn tsgo`, `yarn lint`, and the `calculateMiddleTruncation` tests pass.
- Re-profiling the affected catalog load shows the `measureText` self-time collapse.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Internal-RevId: 23bc1cf34fc7e4419f99c63a4ad58100c294a1da1 parent 988f668 commit da72e34
1 file changed
Lines changed: 15 additions & 3 deletions
Lines changed: 15 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
91 | 96 | | |
92 | 97 | | |
93 | 98 | | |
| |||
108 | 113 | | |
109 | 114 | | |
110 | 115 | | |
111 | | - | |
112 | | - | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
113 | 124 | | |
114 | 125 | | |
115 | 126 | | |
116 | 127 | | |
117 | 128 | | |
118 | 129 | | |
119 | 130 | | |
120 | | - | |
| 131 | + | |
| 132 | + | |
121 | 133 | | |
122 | 134 | | |
123 | 135 | | |
| |||
0 commit comments