Skip to content

Commit 96d9000

Browse files
Improve range creation (#1133)
https://bugs.webkit.org/show_bug.cgi?id=258866 Reviewed by Xabier Rodriguez-Calvar. Before this change, finding the place where new range should be added/merged/created was done with linear approach. This change provides logarithmic approach to find the place for new range. * Source/WebCore/platform/graphics/PlatformTimeRanges.cpp: (WebCore::PlatformTimeRanges::add): (WebCore::PlatformTimeRanges::findLastRangeIndexBefore const): * Source/WebCore/platform/graphics/PlatformTimeRanges.h: Canonical link: https://commits.webkit.org/266355@main
1 parent bae1097 commit 96d9000

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

Source/WebCore/platform/graphics/PlatformTimeRanges.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,15 @@ void PlatformTimeRanges::add(const MediaTime& start, const MediaTime& end)
154154
#endif
155155
ASSERT(start <= end);
156156

157-
unsigned overlappingArcIndex;
157+
size_t overlappingArcIndex;
158158
Range addedRange(start, end);
159159

160160
// For each present range check if we need to:
161161
// - merge with the added range, in case we are overlapping or contiguous
162162
// - Need to insert in place, we we are completely, not overlapping and not contiguous
163163
// in between two ranges.
164-
//
165-
// TODO: Given that we assume that ranges are correctly ordered, this could be optimized.
166164

167-
for (overlappingArcIndex = 0; overlappingArcIndex < m_ranges.size(); overlappingArcIndex++) {
165+
for (overlappingArcIndex = findLastRangeIndexBefore(start, end); overlappingArcIndex < m_ranges.size(); overlappingArcIndex++) {
168166
if (addedRange.isOverlappingRange(m_ranges[overlappingArcIndex]) || addedRange.isContiguousWithRange(m_ranges[overlappingArcIndex])) {
169167
// We need to merge the addedRange and that range.
170168
addedRange = addedRange.unionWithOverlappingOrContiguousRange(m_ranges[overlappingArcIndex]);
@@ -298,4 +296,31 @@ String PlatformTimeRanges::toString() const
298296
return result.toString();
299297
}
300298

299+
size_t PlatformTimeRanges::findLastRangeIndexBefore(const MediaTime& start, const MediaTime& end) const
300+
{
301+
ASSERT(start <= end);
302+
303+
if (m_ranges.isEmpty())
304+
return 0;
305+
306+
const Range range(start, end);
307+
size_t first, last, middle;
308+
size_t index = 0;
309+
310+
first = 0;
311+
last = m_ranges.size() - 1;
312+
middle = first + ((last - first) / 2);
313+
314+
while (first < last && middle > 0) {
315+
if (m_ranges[middle].isBeforeRange(range)) {
316+
index = middle;
317+
first = middle + 1;
318+
} else
319+
last = middle - 1;
320+
321+
middle = first + ((last - first) / 2);
322+
}
323+
return index;
324+
325+
}
301326
}

Source/WebCore/platform/graphics/PlatformTimeRanges.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ class WEBCORE_EXPORT PlatformTimeRanges {
145145

146146
PlatformTimeRanges(Vector<Range>&&);
147147

148+
size_t findLastRangeIndexBefore(const MediaTime& start, const MediaTime& end) const;
149+
148150
Vector<Range> m_ranges;
149151
};
150152

0 commit comments

Comments
 (0)