Skip to content

Commit 4bad7bd

Browse files
committed
Use one buffer only.
1 parent 83fd8c5 commit 4bad7bd

1 file changed

Lines changed: 60 additions & 45 deletions

File tree

src/components/shared/thread/ActivityGraphFills.tsx

Lines changed: 60 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,6 @@ export type CategoryDrawStyles = ReadonlyArray<{
9191
readonly filteredOutByTransformFillStyle: CanvasPattern | string;
9292
}>;
9393

94-
// These Float32Arrays are mutated in place during the computation step.
95-
// buffers[selectedState] is the buffer for the given SelectedState enum value.
96-
type SelectedPercentageAtPixelBuffers = Int32Array<ArrayBuffer>[];
97-
9894
export type CpuRatioInTimeRange = {
9995
readonly cpuRatio: number;
10096
readonly timeRange: Milliseconds;
@@ -149,7 +145,7 @@ export function precomputePositions(
149145
sampleIndexOffset > 0
150146
? fullThreadSampleTimes[sampleIndexOffset - 1]
151147
: fullThreadSampleTimes[0] - interval;
152-
// Go through the samples and accumulate the category into the percentageBuffers.
148+
// Go through the samples and accumulate the category into the percentageBuffer.
153149
for (let i = 0; i < sampleCount; i++) {
154150
const sampleTime = fullThreadSampleTimes[sampleIndexOffset + i];
155151
samplePositions[i] =
@@ -181,16 +177,16 @@ export function precomputePositions(
181177
export function computeActivityGraphFills(
182178
renderedComponentSettings: RenderedComponentSettings
183179
) {
184-
const mutablePercentageBuffers = _createSelectedPercentageAtPixelBuffers(
180+
const mutablePercentageBuffer = _createSelectedPercentageAtPixelBuffers(
185181
renderedComponentSettings
186182
);
187183
const mutableFills = _getCategoryFills(
188184
renderedComponentSettings.categoryDrawStyles,
189-
mutablePercentageBuffers
185+
mutablePercentageBuffer
190186
);
191187
const activityGraphFills = new ActivityGraphFillComputer(
192188
renderedComponentSettings,
193-
mutablePercentageBuffers,
189+
mutablePercentageBuffer,
194190
mutableFills
195191
);
196192

@@ -216,16 +212,16 @@ export function computeActivityGraphFills(
216212
export class ActivityGraphFillComputer {
217213
readonly renderedComponentSettings: RenderedComponentSettings;
218214
// The fills and percentages are mutated in place.
219-
readonly mutablePercentageBuffers: SelectedPercentageAtPixelBuffers[];
215+
readonly mutablePercentageBuffer: Int32Array;
220216
readonly mutableFills: CategoryFill[];
221217

222218
constructor(
223219
renderedComponentSettings: RenderedComponentSettings,
224-
mutablePercentageBuffers: SelectedPercentageAtPixelBuffers[],
220+
mutablePercentageBuffer: Int32Array,
225221
mutableFills: CategoryFill[]
226222
) {
227223
this.renderedComponentSettings = renderedComponentSettings;
228-
this.mutablePercentageBuffers = mutablePercentageBuffers;
224+
this.mutablePercentageBuffer = mutablePercentageBuffer;
229225
this.mutableFills = mutableFills;
230226
}
231227

@@ -296,18 +292,20 @@ export class ActivityGraphFillComputer {
296292
* with these methods.
297293
*/
298294
_accumulateSampleCategories() {
295+
const { mutablePercentageBuffer: buffer, renderedComponentSettings } = this;
299296
const {
297+
canvasPixelWidth,
300298
rangeFilteredThread: { samples },
301299
sampleSelectedStates,
302300
precomputedPositions,
303-
} = this.renderedComponentSettings;
301+
} = renderedComponentSettings;
304302

305303
if (samples.length === 0) {
306304
// If we have no samples, there's nothing to do.
307305
return;
308306
}
309307

310-
// Go through the samples and accumulate the category into the percentageBuffers.)
308+
// Go through the samples and accumulate the category into the buffer.
311309
const { samplePositions, halfwayPositions } = precomputedPositions;
312310
const { threadCPUPercent } = samples;
313311
let beforeSampleCpuPercent = threadCPUPercent[0];
@@ -319,10 +317,10 @@ export class ActivityGraphFillComputer {
319317
const afterSampleCpuPercent = threadCPUPercent[i + 1];
320318
const category = samples.category[i];
321319

322-
const percentageBuffers = this.mutablePercentageBuffers[category];
323320
const bufferIndex =
324321
sampleSelectedStates[i] & SAMPLE_RELATION_TO_SELECTED_STATE_MASK;
325-
const percentageBuffer = percentageBuffers[bufferIndex];
322+
const bufferRow = category * SELECTED_STATE_BUFFER_COUNT + bufferIndex;
323+
const baseIndex = bufferRow * canvasPixelWidth;
326324
const samplePosition = samplePositions[i];
327325

328326
// Samples have two parts to be able to present the different CPU usages properly.
@@ -365,21 +363,21 @@ export class ActivityGraphFillComputer {
365363
const intEndPos = endPos >> FIXED_POINT_BITS;
366364

367365
if (intStartPos === intEndPos) {
368-
percentageBuffer[intStartPos] += cpuPercent * (endPos - startPos);
366+
buffer[baseIndex + intStartPos] += cpuPercent * (endPos - startPos);
369367
} else {
370368
if (intStartPos + 1 < intEndPos) {
371-
percentageBuffer.fill(
369+
buffer.fill(
372370
cpuPercent << FIXED_POINT_BITS,
373-
intStartPos + 1,
374-
intEndPos
371+
baseIndex + intStartPos + 1,
372+
baseIndex + intEndPos
375373
);
376374
}
377375

378376
const startPosFrac = startPos & FIXED_POINT_MASK;
379-
percentageBuffer[intStartPos] +=
377+
buffer[baseIndex + intStartPos] +=
380378
cpuPercent * ((1 << FIXED_POINT_BITS) - startPosFrac);
381379
const endPosFrac = endPos & FIXED_POINT_MASK;
382-
percentageBuffer[intEndPos] += cpuPercent * endPosFrac;
380+
buffer[baseIndex + intEndPos] += cpuPercent * endPosFrac;
383381
}
384382
}
385383

@@ -392,21 +390,21 @@ export class ActivityGraphFillComputer {
392390
const intEndPos = endPos >> FIXED_POINT_BITS;
393391

394392
if (intStartPos === intEndPos) {
395-
percentageBuffer[intStartPos] += cpuPercent * (endPos - startPos);
393+
buffer[baseIndex + intStartPos] += cpuPercent * (endPos - startPos);
396394
} else {
397395
if (intStartPos + 1 < intEndPos) {
398-
percentageBuffer.fill(
396+
buffer.fill(
399397
cpuPercent << FIXED_POINT_BITS,
400-
intStartPos + 1,
401-
intEndPos
398+
baseIndex + intStartPos + 1,
399+
baseIndex + intEndPos
402400
);
403401
}
404402

405403
const startPosFrac = startPos & FIXED_POINT_MASK;
406-
percentageBuffer[intStartPos] +=
404+
buffer[baseIndex + intStartPos] +=
407405
cpuPercent * ((1 << FIXED_POINT_BITS) - startPosFrac);
408406
const endPosFrac = endPos & FIXED_POINT_MASK;
409-
percentageBuffer[intEndPos] += cpuPercent * endPosFrac;
407+
buffer[baseIndex + intEndPos] += cpuPercent * endPosFrac;
410408
}
411409
}
412410

@@ -770,14 +768,9 @@ function _createSelectedPercentageAtPixelBuffers({
770768
}: {
771769
categoryDrawStyles: CategoryDrawStyles;
772770
canvasPixelWidth: number;
773-
}): SelectedPercentageAtPixelBuffers[] {
774-
return categoryDrawStyles.map(() => {
775-
const percentageBuffers = [];
776-
for (let i = 0; i <= SELECTED_STATE_BUFFER_COUNT; i++) {
777-
percentageBuffers[i] = new Int32Array(canvasPixelWidth);
778-
}
779-
return percentageBuffers;
780-
});
771+
}): Int32Array<ArrayBuffer> {
772+
const rowCount = categoryDrawStyles.length * SELECTED_STATE_BUFFER_COUNT;
773+
return new Int32Array(canvasPixelWidth * rowCount);
781774
}
782775

783776
/**
@@ -791,47 +784,69 @@ function _createSelectedPercentageAtPixelBuffers({
791784
*/
792785
function _getCategoryFills(
793786
categoryDrawStyles: CategoryDrawStyles,
794-
percentageBuffers: SelectedPercentageAtPixelBuffers[]
787+
percentageBuffer: Int32Array<ArrayBuffer>
795788
): CategoryFill[] {
789+
const canvasPixelWidth =
790+
percentageBuffer.length /
791+
categoryDrawStyles.length /
792+
SELECTED_STATE_BUFFER_COUNT;
793+
796794
// Sort all of the categories by their gravity.
797795
const categoryIndexesByGravity = categoryDrawStyles
798796
.map((_, i) => i)
799797
.sort(
800798
(a, b) => categoryDrawStyles[b].gravity - categoryDrawStyles[a].gravity
801799
);
802800

801+
function bufferForCategoryAndSelectedState(
802+
category: IndexIntoCategoryList,
803+
selectedState: SelectedState
804+
): Int32Array<ArrayBuffer> {
805+
const rowIndex = category * SELECTED_STATE_BUFFER_COUNT + selectedState;
806+
const sliceStart = rowIndex * canvasPixelWidth;
807+
const sliceEnd = sliceStart + canvasPixelWidth;
808+
return percentageBuffer.subarray(sliceStart, sliceEnd);
809+
}
810+
803811
const nestedFills: CategoryFill[][] = categoryIndexesByGravity.map(
804812
(categoryIndex) => {
805813
const categoryDrawStyle = categoryDrawStyles[categoryIndex];
806-
const buffer = percentageBuffers[categoryIndex];
807-
const canvasPixelWidth =
808-
buffer[SelectedState.UnselectedOrderedBeforeSelected].length;
809814
// For every category we draw four fills, for the four selection kinds:
810815
return [
811816
{
812817
category: categoryDrawStyle.category,
813818
fillStyle: categoryDrawStyle.getUnselectedFillStyle(),
814-
perPixelContribution:
815-
buffer[SelectedState.UnselectedOrderedBeforeSelected],
819+
perPixelContribution: bufferForCategoryAndSelectedState(
820+
categoryIndex,
821+
SelectedState.UnselectedOrderedBeforeSelected
822+
),
816823
accumulatedUpperEdge: new Float32Array(canvasPixelWidth),
817824
},
818825
{
819826
category: categoryDrawStyle.category,
820827
fillStyle: categoryDrawStyle.getSelectedFillStyle(),
821-
perPixelContribution: buffer[SelectedState.Selected],
828+
perPixelContribution: bufferForCategoryAndSelectedState(
829+
categoryIndex,
830+
SelectedState.Selected
831+
),
822832
accumulatedUpperEdge: new Float32Array(canvasPixelWidth),
823833
},
824834
{
825835
category: categoryDrawStyle.category,
826836
fillStyle: categoryDrawStyle.getUnselectedFillStyle(),
827-
perPixelContribution:
828-
buffer[SelectedState.UnselectedOrderedAfterSelected],
837+
perPixelContribution: bufferForCategoryAndSelectedState(
838+
categoryIndex,
839+
SelectedState.UnselectedOrderedAfterSelected
840+
),
829841
accumulatedUpperEdge: new Float32Array(canvasPixelWidth),
830842
},
831843
{
832844
category: categoryDrawStyle.category,
833845
fillStyle: categoryDrawStyle.filteredOutByTransformFillStyle,
834-
perPixelContribution: buffer[SelectedState.FilteredOutByTransform],
846+
perPixelContribution: bufferForCategoryAndSelectedState(
847+
categoryIndex,
848+
SelectedState.FilteredOutByTransform
849+
),
835850
accumulatedUpperEdge: new Float32Array(canvasPixelWidth),
836851
},
837852
];

0 commit comments

Comments
 (0)