Closed Bug 1842130 Opened 1 year ago Closed 1 year ago

Have a special sort variant for nsTArray<RefPtr<T>> that does not affect refcounts and use it

Categories

(Core :: XPCOM, enhancement)

enhancement

Tracking

()

RESOLVED WONTFIX

People

(Reporter: jstutte, Assigned: jstutte)

Details

Attachments

(17 obsolete files)

The std::sort function uses (sometimes) copy assignments while sorting. In case we have a nsTArray<RefPtr<T>> this can lead to unwanted noise on the pointee's refcount.

Instead of just using HeapSort in these cases (which comes with a performance penalty), we want to have a special variant that takes advantage of knowing that a RefPtr<T> is stored like a T*, such that we can do the sorting pretending to operate on raw pointers.

Furthermore we also want to have the possibility to sort such an array based on the default comparator of T, not just the one from RefPtr<T> (which compares just the pointer values and is also used in some cases).

Attachment #9342670 - Attachment description: WIP: Bug 1842130 - Have StandardSortRefPtrArray<PointeeType> for nsTArray. r?#xpcom-reviewers → WIP: Bug 1842130 - Have StandardSortRefPtrArray<pointee_type> for nsTArray. r?#xpcom-reviewers
Attachment #9342671 - Attachment is obsolete: true

Depends on D182746

Just a note (taken from a chat with Jari):

There might be a potential for a footgun here if the T in nsTArray<RefPtr<T>> HasThreadSafeRefCnt as this might indicate that elements (and thus their comparator result) could change while sorting. Note that at least the refcount should remain always >0 as the array element holds one while sorting.
As nsTArray itself is not thread safe, this is probably true also for the old sort implementation.

With the current state of things we could probably just issue a warning or (static) assert if we want to sort such a T. But all accesses through nsTArray to such T would need to happen under some lock managed elsewhere, anyways, and thus also sorting, IMHO.

Flags: needinfo?(jjalkanen)
Attachment #9342707 - Attachment description: WIP: Bug 1842130 - Use HeapSort in CollectLRUOriginInfosUntil. r?#dom-storage-reviewers → WIP: Bug 1842130 - Use StandardSortRefPtrArray in CollectLRUOriginInfosUntil. r?#dom-storage-reviewers

Our current uses are:

nsTArray<RefPtr<T>> -> StandardSortRefPtrArray
216	aMatches->Sort(AnimationPtrComparator<RefPtr<dom::Animation>>()); // found in mozilla::FindAnimationsForCompositor
616	aAnimations.Sort(AnimationPtrComparator<RefPtr<Animation>>()); // found in mozilla::dom::DocumentOrShadowRoot::GetAnimations
3777	aAnimations.Sort(AnimationPtrComparator<RefPtr<Animation>>()); // found in mozilla::dom::Element::GetAnimationsWithoutFlush
193	clientList.Sort(MatchAllComparator()); // found in mozilla::dom::Clients::MatchAll
155	tracks.Sort(AudioTrackCompare); // found in mozilla::dom::MediaStreamAudioSourceNode::AttachToRightTrack
163	aRetval.Sort(PerformanceEntryComparator()); // found in mozilla::dom::Performance::GetEntries
469	aRetval.Sort(PerformanceEntryComparator()); // found in mozilla::dom::PerformanceMainThread::GetEntries
66	aRetval.Sort(PerformanceEntryComparator()); // found in mozilla::dom::PerformanceObserverEntryList::GetEntries
78	aRetval.Sort(PerformanceEntryComparator()); // found in mozilla::dom::PerformanceObserverEntryList::GetEntriesByType
99	aRetval.Sort(PerformanceEntryComparator()); // found in mozilla::dom::PerformanceObserverEntryList::GetEntriesByName
6589	originInfos.Sort(OriginInfoAccessTimeComparator()); // found in mozilla::dom::quota::QuotaManager::CollectLRUOriginInfosUntil
378	instanceList.Sort(InstanceTimeComparator()); // found in mozilla::SMILTimedElement::UpdateInstanceTime
1534	mAvailableFonts.Sort(FontEntryStandardFaceComparator()); // found in gfxFontFamily::SortAvailableFonts
3437	mRecs.Sort(FrecencyComparator()); // found in mozilla::net::CacheIndex::FrecencyArray::SortIfNeeded
2422	aResult.Sort(CompareCookiesCreationTime()); // found in mozilla::net::CookieService::GetCookiesSince
306	cookiesList->Sort(CompareCookiesForSending()); // found in mozilla::net::CookieServiceChild::GetCookieStringFromDocument
343	accessPoints.Sort([](const RefPtr<nsIWifiAccessPoint>& ia, // found in nsWifiMonitor::DoScan

nsTArray<UniquePtr<T>> -> maybe StandardSortRefPtrArray ?
5316	data->mTimeouts.Sort(comparator); // found in mozilla::dom::WorkerPrivate::RunExpiredTimeouts
Assignee: nobody → jstutte
Status: NEW → ASSIGNED
Attachment #9343650 - Attachment description: WIP: Bug 1842130 - Use StandardSortRefPtrArray<PerformanceEntry> for nsTArray<RefPtr<PerformanceEntry>>, r?#xpcom-reviewers → Bug 1842130 - Use StandardSortRefPtrArray<PerformanceEntry> for nsTArray<RefPtr<PerformanceEntry>>, r?#xpcom-reviewers
Attachment #9343651 - Attachment description: WIP: Bug 1842130 - Use StandardSortRefPtrArray<Client> for nsTArray<RefPtr<Client>>, r?#xpcom-reviewers → Bug 1842130 - Use StandardSortRefPtrArray<Client> for nsTArray<RefPtr<Client>>, r?#xpcom-reviewers
Attachment #9343652 - Attachment description: WIP: Bug 1842130 - Use StandardSortRefPtrArray<AudioStreamTrack> for nsTArray<RefPtr<AudioStreamTrack>>, r?#xpcom-reviewers → Bug 1842130 - Use StandardSortRefPtrArray<AudioStreamTrack> for nsTArray<RefPtr<AudioStreamTrack>>, r?#xpcom-reviewers
Attachment #9343653 - Attachment description: WIP: Bug 1842130 - Use StandardSortRefPtrArray<SMILInstanceTime> for nsTArray<RefPtr<SMILInstanceTime>>, r?#xpcom-reviewers → Bug 1842130 - Use StandardSortRefPtrArray<SMILInstanceTime> for nsTArray<RefPtr<SMILInstanceTime>>, r?#xpcom-reviewers
Attachment #9343654 - Attachment description: WIP: Bug 1842130 - Use StandardSortRefPtrArray<gfxFontEntry> for nsTArray<RefPtr<gfxFontEntry>>, r?#xpcom-reviewers → Bug 1842130 - Use StandardSortRefPtrArray<gfxFontEntry> for nsTArray<RefPtr<gfxFontEntry>>, r?#xpcom-reviewers
Attachment #9343655 - Attachment description: WIP: Bug 1842130 - Use StandardSortRefPtrArray<CacheIndexRecordWrapper> for nsTArray<RefPtr<CacheIndexRecordWrapper>>, r?#xpcom-reviewers → Bug 1842130 - Use StandardSortRefPtrArray<CacheIndexRecordWrapper> for nsTArray<RefPtr<CacheIndexRecordWrapper>>, r?#xpcom-reviewers
Attachment #9343656 - Attachment description: WIP: Bug 1842130 - Use StandardSortRefPtrArray<nsICookie> for nsTArray<RefPtr<nsICookie>>, r?#xpcom-reviewers → Bug 1842130 - Use StandardSortRefPtrArray<nsICookie> for nsTArray<RefPtr<nsICookie>>, r?#xpcom-reviewers
Attachment #9343657 - Attachment description: WIP: Bug 1842130 - Use StandardSortRefPtrArray<Cookie> for nsTArray<RefPtr<Cookie>>, r?#xpcom-reviewers → Bug 1842130 - Use StandardSortRefPtrArray<Cookie> for nsTArray<RefPtr<Cookie>>, r?#xpcom-reviewers
Attachment #9343658 - Attachment description: WIP: Bug 1842130 - Use StandardSortRefPtrArray<nsIWifiAccessPoint> for nsTArray<RefPtr<nsIWifiAccessPoint>> and adjust sort condition. r?#xpcom-reviewers → Bug 1842130 - Use StandardSortRefPtrArray<nsIWifiAccessPoint> for nsTArray<RefPtr<nsIWifiAccessPoint>> and adjust sort condition. r?#xpcom-reviewers
Attachment #9343741 - Attachment description: WIP: Bug 1842130 - Use StandardSortRefPtrArray<TimeoutInfo> for nsTArray<UniquePtr<TimeoutInfo>>. r?#xpcom-reviewers → Bug 1842130 - Use StandardSortRefPtrArray<TimeoutInfo> for nsTArray<UniquePtr<TimeoutInfo>>. r?#xpcom-reviewers
Attachment #9343742 - Attachment description: WIP: Bug 1842130 - Use StandardSortRefPtrArray<CacheEntry> for nsTArray<RefPtr<CacheEntry>>. r?#xpcom-reviewers → Bug 1842130 - Use StandardSortRefPtrArray<CacheEntry> for nsTArray<RefPtr<CacheEntry>>. r?#xpcom-reviewers
Attachment #9342670 - Attachment description: WIP: Bug 1842130 - Have StandardSortRefPtrArray<pointee_type> for nsTArray. r?#xpcom-reviewers → Bug 1842130 - Have StandardSortRefPtrArray<pointee_type> for nsTArray. r?#xpcom-reviewers
Attachment #9342700 - Attachment description: WIP: Bug 1842130 - Use StandardSortRefPtrArray<Animation>. r?#xpcom-reviewers → Bug 1842130 - Use StandardSortRefPtrArray<Animation>. r?#xpcom-reviewers
Attachment #9342701 - Attachment description: WIP: Bug 1842130 - Use StandardSortRefPtrArray<nsAtom> in AtomSetFromRange. r?#xpcom-reviewers → Bug 1842130 - Use StandardSortRefPtrArray<nsAtom> in AtomSetFromRange. r?#xpcom-reviewers
Attachment #9342707 - Attachment description: WIP: Bug 1842130 - Use StandardSortRefPtrArray in CollectLRUOriginInfosUntil. r?#dom-storage-reviewers → Bug 1842130 - Use StandardSortRefPtrArray in CollectLRUOriginInfosUntil. r?#dom-storage-reviewers

Wdyt of naming this something else? It's not really about RefPtr<> since it also works for UniquePtr or so right? StandardSortSmartPtr?

Flags: needinfo?(jstutte)

(In reply to Emilio Cobos Álvarez (:emilio) from comment #19)

Wdyt of naming this something else? It's not really about RefPtr<> since it also works for UniquePtr or so right? StandardSortSmartPtr?

Yeah, it started for RefPtr only but then I had to add the other variants. However it will probably not cover nsCOMPtr as we have nsCOMArray, so I wonder if SmartPtr is the right name here.

Flags: needinfo?(jstutte)

ni? to elaborate on how to remove StandardSortRefPtr

Flags: needinfo?(emilio)

See the incoming patch, which works and should avoid all these.

Flags: needinfo?(emilio) → needinfo?(jstutte)
Attachment #9342670 - Attachment is obsolete: true
Attachment #9342700 - Attachment is obsolete: true
Attachment #9342701 - Attachment is obsolete: true
Attachment #9342707 - Attachment is obsolete: true
Attachment #9343650 - Attachment is obsolete: true
Attachment #9343651 - Attachment is obsolete: true
Attachment #9343652 - Attachment is obsolete: true
Attachment #9343653 - Attachment is obsolete: true
Attachment #9343654 - Attachment is obsolete: true
Attachment #9343656 - Attachment is obsolete: true
Attachment #9343657 - Attachment is obsolete: true
Attachment #9343742 - Attachment is obsolete: true

This bug is obsolete thanks to :emilio's template magic we plumbed into the nsTArray::Sort patch. I'll check the remaining patches for comments/details I still want to address elsewhere, but they are obsolete here.

Status: ASSIGNED → RESOLVED
Closed: 1 year ago
Flags: needinfo?(jstutte)
Flags: needinfo?(jjalkanen)
Resolution: --- → WONTFIX
Attachment #9345865 - Attachment is obsolete: true
No longer depends on: 1839051
Attachment #9343741 - Attachment is obsolete: true
Attachment #9343658 - Attachment is obsolete: true
Attachment #9343655 - Attachment is obsolete: true
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: