Custom Highlight API: Speed up `Highlight::Add()`
Categories
(Core :: DOM: Selection, task)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox127 | --- | fixed |
People
(Reporter: jjaschke, Assigned: jjaschke)
References
(Blocks 1 open bug)
Details
Attachments
(1 external file, 1 obsolete file)
As pointed out in Bug 1892514, there are situations in which Highlight::Add() shows up in profiles prominently. This is due to the current situation described in Bug 1852908 that requires mirroring the maplike/setlike data structures in the DOM code using nsTArray<>s to be able to iterate its contents.
The elements in the mirrored array have to be sorted by insertion order and unique. Since SetlikeHelpers::Add() does not indicate if it actually inserts something, nsTArray::Contains() is used to determine if a new element needs to be inserted into the array. In the case of many highlighted ranges (such as the described test case in Bug 1892514), this can take up to a few seconds.
While waiting for the real solution of the underlying problem (-> Bug 1852908), removing the need of the mirrored array altogether, the nsTArray<>::Contains() calls can be replaced with SetlikeHelpers::Has(), which (presumably) has a better time complexity due to its underlying data structure.
| Assignee | ||
Comment 1•2 years ago
•
|
||
As expected, implementing the proposed changes reduced the time spent in Highlight::Add() from ~1sec (test case above, on my machine) to ~300ms. The remaining time is mostly spent in NotifySelectionListener() now.
Try run for part 1.
Additionally, there was a small bug which caused NotifySelectionListener() to notify for eNormal selections even when triggered by highlight selections. This change reduced the Highlight::Add() call even further (~200ms now).
Try run for part 2.
| Assignee | ||
Comment 2•2 years ago
|
||
To preserve uniqueness in the mirrored data structure for the JS-setlike object,
nsTArray::Contains() was used. This showed up in profiles due to its O(n) time complexity.
Performance could be improved by replacing it with SetlikeHelpers::Has(), which uses the more efficient version of the underlying data structure of the setlike.
The same idea has been applied to HighlightRegistry, which uses a maplike. However, this is mostly for consistency, since Highlight::Add() will likely be called more often and hence the bigger performance bottleneck.
| Assignee | ||
Comment 3•2 years ago
|
||
Before this patch, the AutoFrameSelectionBatcher helper class would always call NotifySelectionListeners() for SelectionType::eNormal.
| Assignee | ||
Comment 4•2 years ago
|
||
(Fixing broken tests tomorrow)
Updated•2 years ago
|
Updated•2 years ago
|
Updated•2 years ago
|
Updated•2 years ago
|
Comment 6•2 years ago
|
||
| bugherder | ||
Comment 7•2 years ago
|
||
Operation: Copy-paste text, Ctrl+A, delete text
Profile from couple of days back : https://share.firefox.dev/3xJ4kgR
Profile from latest Nightly: https://share.firefox.dev/3QfMxnI
Description
•