Skip to content

Commit 0cf99a4

Browse files
committed
Bug 1974237 - part2 : use nsTHashSet to perform O(1) search in order to improve the performance. r=media-playback-reviewers,padenot
In a test scenario with approximately 40000 cues, the speed improvement is dropping from around 200ms to about 5ms. Differential Revision: https://phabricator.services.mozilla.com/D256513
1 parent bc75bd8 commit 0cf99a4

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

dom/media/webvtt/TextTrackCueList.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ TextTrackCue* TextTrackCueList::GetCueById(const nsAString& aId) {
6868
}
6969

7070
void TextTrackCueList::AddCue(TextTrackCue& aCue) {
71-
if (mList.Contains(&aCue)) {
71+
if (mCueSet.Contains(&aCue)) {
7272
return;
7373
}
7474
mList.InsertElementSorted(&aCue, CompareCuesByTime());
75+
mCueSet.Insert(&aCue);
7576
}
7677

7778
void TextTrackCueList::RemoveCue(TextTrackCue& aCue, ErrorResult& aRv) {
78-
if (!mList.Contains(&aCue)) {
79+
if (!mCueSet.Contains(&aCue)) {
7980
aRv.Throw(NS_ERROR_DOM_NOT_FOUND_ERR);
8081
return;
8182
}
@@ -84,6 +85,7 @@ void TextTrackCueList::RemoveCue(TextTrackCue& aCue, ErrorResult& aRv) {
8485

8586
void TextTrackCueList::RemoveCue(TextTrackCue& aCue) {
8687
mList.RemoveElement(&aCue);
88+
mCueSet.Remove(&aCue);
8789
}
8890

8991
void TextTrackCueList::GetArray(nsTArray<RefPtr<TextTrackCue>>& aCues) {
@@ -104,7 +106,7 @@ void TextTrackCueList::NotifyCueUpdated(TextTrackCue* aCue) {
104106
}
105107

106108
bool TextTrackCueList::IsCueExist(TextTrackCue* aCue) {
107-
if (aCue && mList.Contains(aCue)) {
109+
if (aCue && mCueSet.Contains(aCue)) {
108110
return true;
109111
}
110112
return false;

dom/media/webvtt/TextTrackCueList.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "nsTArray.h"
1111
#include "nsCOMPtr.h"
1212
#include "nsCycleCollectionParticipant.h"
13+
#include "nsTHashSet.h"
1314
#include "nsWrapperCache.h"
1415

1516
namespace mozilla {
@@ -64,6 +65,9 @@ class TextTrackCueList final : public nsISupports, public nsWrapperCache {
6465
// A sorted list of TextTrackCues sorted by earliest start time. If the start
6566
// times are equal then it will be sorted by end time, earliest first.
6667
nsTArray<RefPtr<TextTrackCue>> mList;
68+
69+
// Utilized for rapid cue existence verification.
70+
nsTHashSet<TextTrackCue*> mCueSet;
6771
};
6872

6973
} // namespace dom

0 commit comments

Comments
 (0)