Have a special sort variant for nsTArray<RefPtr<T>> that does not affect refcounts and use it
Categories
(Core :: XPCOM, enhancement)
Tracking
()
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).
Assignee | ||
Comment 1•1 year ago
|
||
Depends on D182731
Assignee | ||
Comment 2•1 year ago
|
||
Depends on D182746
Updated•1 year ago
|
Updated•1 year ago
|
Assignee | ||
Comment 3•1 year ago
|
||
Depends on D182746
Assignee | ||
Comment 4•1 year ago
|
||
Depends on D182978
Assignee | ||
Comment 5•1 year ago
|
||
Depends on D182979
Assignee | ||
Comment 6•1 year ago
•
|
||
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.
Updated•1 year ago
|
Assignee | ||
Comment 7•1 year ago
|
||
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 | ||
Comment 8•1 year ago
|
||
Depends on D183257
Assignee | ||
Comment 9•1 year ago
|
||
Depends on D183467
Assignee | ||
Comment 10•1 year ago
|
||
Depends on D183468
Assignee | ||
Comment 11•1 year ago
|
||
Depends on D183469
Assignee | ||
Comment 12•1 year ago
|
||
Depends on D183470
Assignee | ||
Comment 13•1 year ago
|
||
Depends on D183471
Assignee | ||
Comment 14•1 year ago
|
||
Depends on D183472
Assignee | ||
Comment 15•1 year ago
|
||
Depends on D183473
Assignee | ||
Comment 16•1 year ago
|
||
Depends on D183474
Updated•1 year ago
|
Assignee | ||
Comment 17•1 year ago
|
||
Depends on D183475
Assignee | ||
Comment 18•1 year ago
|
||
Depends on D183510
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Comment 19•1 year ago
|
||
Wdyt of naming this something else? It's not really about RefPtr<>
since it also works for UniquePtr
or so right? StandardSortSmartPtr
?
Assignee | ||
Comment 20•1 year ago
|
||
(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 forUniquePtr
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.
Comment 21•1 year ago
|
||
ni? to elaborate on how to remove StandardSortRefPtr
Comment 22•1 year ago
|
||
See the incoming patch, which works and should avoid all these.
Comment 23•1 year ago
|
||
This is on top of D182978, and seems to work.
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Assignee | ||
Comment 24•1 year ago
|
||
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.
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
Description
•