Micro-optimize `EventTargetChainItem::GetEventTargetParent()`
Categories
(Core :: DOM: Events, task)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox162 | --- | fixed |
People
(Reporter: jjaschke, Assigned: jjaschke)
References
(Blocks 2 open bugs)
Details
(Keywords: perf-alert)
Attachments
(1 external file)
EventTargetChainItem::GetEventTargetParent copies seven bools from the EventChainPreVisitor into the chain item's EventTargetChainFlags bitfield, through one setter per flag (SetForceContentDispatch(aVisitor.mForceContentDispatch), and so on). The visitor members are bool, and the bitfield members are bool too. So the compiler has to assume that each bitfield store may alias the next bool it loads from the visitor, and it can't combine the stores. That runs once per item in every event target chain, for every event dispatched.
Optimised Linux x86-64 build, disassembly of EventTargetChainItem::GetEventTargetParent after the virtual mTarget->GetEventTargetParent(aVisitor) call. r14 is the visitor and [rbx+0x30] is the chain item's flags word:
movzx eax,BYTE PTR [r14+0x2a] ; load visitor bool
mov ecx,DWORD PTR [rbx+0x30] ; load flags
and ecx,0xfffffffe
or ecx,eax
mov WORD PTR [rbx+0x30],cx ; store flags (1)
movzx eax,BYTE PTR [r14+0x2d] ; next visitor bool, reloaded after the store
and ecx,0xfffffffd
lea eax,[rcx+rax*2]
mov WORD PTR [rbx+0x30],ax ; store flags (2)
movzx ecx,BYTE PTR [r14+0x2e]
... ; same pattern
mov WORD PTR [rbx+0x30],ax ; store flags (7)
That is seven read-modify-write stores to the same 16-bit word, each followed by a reload from the visitor. Reading all visitor fields into locals first, then calling the setters, compiles to:
movzx ecx,BYTE PTR [r14+0x2a]
movzx esi,BYTE PTR [r14+0x2d]
movzx eax,BYTE PTR [r14+0x2e]
movzx edi,BYTE PTR [r14+0x2f]
movzx r8d,BYTE PTR [r14+0x28]
mov r9d,0xfffffe18
and r9d,DWORD PTR [rbx+0x30] ; load flags once
movzx edx,BYTE PTR [r14+0x30]
... ; shifts and ors in registers
mov WORD PTR [rbx+0x30],ax ; single store
The function goes from 122 to 110 instructions, and from 7 stores to 1.
Micro-benchmark (headless, local opt build). The page is a TodoMVC-like list of 100 items: <li><div><input type=checkbox><label><button></div><input></li>, about 10 ancestors per target. It reports the median ns per dispatch. Ten interleaved rounds, comparing the build without and with the patch (binaries otherwise identical, each run twice), mean of paired deltas with 95% intervals:
| Case | patched | patched, 2nd copy | unpatched, 2nd copy |
|---|---|---|---|
checkbox.click(), click+change listeners on document |
-3.6% [-4.5, -2.6] | -2.7% [-4.0, -1.0] | -0.1% [-0.8, +0.7] |
| same, root capture+bubble listeners (React-style) | -2.9% [-3.6, -2.2] | -2.2% [-3.3, -0.9] | -0.2% [-0.7, +0.5] |
| same, listener keeps the event | -4.5% [-6.7, -2.8] | -3.6% [-6.2, -1.1] | -1.9% [-3.9, -0.1] |
dispatchEvent(new Event("x", {bubbles: true})) |
-2.2% [-2.8, -1.5] | -2.0% [-2.8, -1.0] | -0.5% [-1.1, +0.3] |
dispatchEvent of a reused Event |
-2.7% [-3.1, -2.2] | -3.2% [-3.6, -2.8] | -0.3% [-1.1, +0.4] |
button.click() |
-1.5% [-2.5, -0.5] | +1.2% [-1.9, +5.6] | -0.4% [-1.1, +0.5] |
keydown on an input |
-0.6% [-2.3, +1.5] | -1.0% [-2.0, +0.1] | -0.6% [-1.2, -0.0] |
The "unpatched, 2nd copy" column is the noise floor.
In a local Speedometer 3 profile, dispatch work outside the listeners themselves is about 2% of scored time. So the expected Speedometer 3 effect is on the order of 0.05%, too small to resolve on try by itself.
| Assignee | ||
Comment 1•15 days ago
|
||
The bool bitfield stores may alias the bool loads from the visitor, so the
compiler stored the flags word once per flag. Loading the visitor fields first
lets it combine them into a single store.
Updated•15 days ago
|
Comment 3•11 days ago
|
||
| bugherder | ||
Comment 4•10 days ago
|
||
Perfherder has detected a browsertime performance change from push aecb91e0505dad530dcc75992c25088eba70ac72.
No action is required from the author; this comment is provided for informational purposes only.
| Improvement | Test | Platform | Options | Absolute values [old vs new] | Performance Profiles |
|---|---|---|---|---|---|
| 1% | speedometer3 TodoMVC-Svelte-Complex-DOM/total (doc) | macosx1500-aarch64-shippable | fission webrender | 6.90 ms -> 6.84 ms | Before/After |
Need Help or Information?
If you have any questions, please reach out to acseh@mozilla.com. Alternatively, you can find help on Slack by joining #perf-help, and on Matrix you can find help by joining #perftest.
Details of the alert can be found in the alert summary, including links to graphs and comparisons for each of the affected tests.
Updated•1 day ago
|
Description
•