[tools][system-analyzer] Improve timeline.mjs

Make Timeline.selectTimeRange run in O(log(n)) instead of
O(n) comparisons.

Drive-by-fix:
- Use *Index for variable names in Timeline

Bug: v8:10644
Change-Id: I65f3be9f259e6bebcec489526a54712daffd4d15
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2507714
Commit-Queue: Camillo Bruni <cbruni@chromium.org>
Auto-Submit: Camillo Bruni <cbruni@chromium.org>
Reviewed-by: Shu-yu Guo <syg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#71461}
This commit is contained in:
Camillo Bruni 2020-11-27 19:43:26 +01:00 committed by Commit Bot
parent bba4817bf0
commit 8547ac388e
2 changed files with 42 additions and 20 deletions

View File

@ -8,23 +8,43 @@ import { LogEntry} from "../../../tools/system-analyzer/log/log.mjs";
(function testTimeline() {
let timeline = new Timeline();
let id1 = "0x3e7e082470cd";
let id2 = "0x3e7e082470ad";
let time = 12;
let entry1 = new LogEntry(id1, time);
let time = 10;
let entry1 = new LogEntry(id1, time + 0);
let entry2 = new LogEntry(id1, time + 1);
let entry3 = new LogEntry(id1, time + 2);
let entry4 = new LogEntry(id1, time + 3);
let entry5 = new LogEntry(id2, time + 3);
let entry6 = new LogEntry(id1, time + 4);
timeline.push(entry1);
timeline.push(entry2);
timeline.push(entry3);
timeline.push(entry4);
timeline.push(entry5);
timeline.push(entry6);
assertEquals(timeline.find(time + 0), 0);
assertEquals(timeline.find(time + 1), 1);
assertEquals(timeline.find(time + 2), 2);
assertEquals(timeline.find(time + 3), 3);
assertEquals(timeline.find(time + 4), 5);
assertEquals(timeline.find(time + 5), 5);
assertEquals(timeline.find(time + 2.00), 2);
assertEquals(timeline.find(time + 2.01), 3);
assertEquals(timeline.find(time + 2.90), 3);
assertEquals(timeline.find(time + 3.01), 5);
assertEquals(timeline.find(time + 3.90), 5);
assertEquals(timeline.find(time + 4.00), 5);
let startTime = time;
let endTime = time + 2;
timeline.selectTimeRange(startTime, endTime);
assertArrayEquals(timeline.selection, [entry1, entry2, entry3]);
assertArrayEquals(timeline.selection, [entry1, entry2]);
let entryIdx = timeline.find(time + 1);
let entry = timeline.at(entryIdx);
assertEquals(entry.time, time + 1);

View File

@ -34,8 +34,8 @@ class Timeline {
this._selection = value;
}
selectTimeRange(start, end) {
this._selection = this.filter(e => e.time >= start && e.time <= end);
selectTimeRange(startTime, endTime) {
this._selection = this.range(startTime, endTime);
}
getChunks(windowSizeMs) {
@ -134,30 +134,32 @@ class Timeline {
return chunks;
}
range(start, end) {
const first = this.find(start);
if (first < 0) return [];
const last = this.find(end, first);
return this._values.slice(first, last);
// Return all entries in ({startTime}, {endTime}]
range(startTime, endTime) {
const firstIndex = this.find(startTime);
if (firstIndex < 0) return [];
const lastIndex = this.find(endTime, firstIndex + 1);
return this._values.slice(firstIndex, lastIndex);
}
// Return the first index for the first element at {time}.
find(time, offset = 0) {
return this._find(this._values, each => each.time - time, offset);
}
_find(array, cmp, offset = 0) {
let min = offset;
let max = array.length;
while (min < max) {
let mid = min + Math.floor((max - min) / 2);
let result = cmp(array[mid]);
if (result > 0) {
max = mid - 1;
// Return the first index for which compareFn(item) is >= 0;
_find(array, compareFn, offset = 0) {
let minIndex = offset;
let maxIndex = array.length - 1;
while (minIndex < maxIndex) {
const midIndex = minIndex + (((maxIndex - minIndex) / 2) | 0);
if (compareFn(array[midIndex]) < 0) {
minIndex = midIndex + 1;
} else {
min = mid + 1;
maxIndex = midIndex;
}
}
return min;
return minIndex;
}
initializeTypes() {