Closed Bug 1892589 Opened 4 months ago Closed 4 months ago

Custom Highlight API: Speed up `Highlight::Add()`

Categories

(Core :: DOM: Selection, task)

task

Tracking

()

RESOLVED FIXED
127 Branch
Tracking Status
firefox127 --- fixed

People

(Reporter: jjaschke, Assigned: jjaschke)

References

(Blocks 2 open bugs)

Details

Attachments

(1 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.

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.

Try run hopefully without build errors.

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.

Before this patch, the AutoFrameSelectionBatcher helper class would always call NotifySelectionListeners() for SelectionType::eNormal.

(Fixing broken tests tomorrow)

Attachment #9397729 - Attachment description: Bug 1892589, part 1 - Custom Highlight API: Implemented speedup for `Highlight::Add(). r=edgar!,#dom-core → Bug 1892589- Custom Highlight API: Implemented speedup for `Highlight::Add(). r=edgar!,#dom-core
Attachment #9397730 - Attachment is obsolete: true
Attachment #9397729 - Attachment description: Bug 1892589- Custom Highlight API: Implemented speedup for `Highlight::Add(). r=edgar!,#dom-core → Bug 1892589 - Custom Highlight API: Implemented speedup for `Highlight::Add(). r=edgar!,#dom-core
Attachment #9397729 - Attachment description: Bug 1892589 - Custom Highlight API: Implemented speedup for `Highlight::Add(). r=edgar!,#dom-core → Bug 1892589 - Custom Highlight API: Implemented speedup for `Highlight::Add()`. r=edgar!,#dom-core
Pushed by jjaschke@mozilla.com:
https://hg.mozilla.org/integration/autoland/rev/6614f1cdc59b
Custom Highlight API: Implemented speedup for `Highlight::Add()`. r=edgar,dom-core
Status: ASSIGNED → RESOLVED
Closed: 4 months ago
Resolution: --- → FIXED
Target Milestone: --- → 127 Branch

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

Blocks: 1892216
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: