HeatmapNG: pre-allocate arrays during calc (#51465)

This commit is contained in:
Leon Sorokin
2022-06-27 18:43:59 -05:00
committed by GitHub
parent cfbfacb152
commit 89b150ab58
2 changed files with 23 additions and 8 deletions

View File

@@ -244,9 +244,6 @@ export function prepBucketFrames(frames: DataFrame[]): DataFrame[] {
export function calculateHeatmapFromData(frames: DataFrame[], options: HeatmapCalculationOptions): DataFrame {
//console.time('calculateHeatmapFromData');
let xs: number[] = [];
let ys: number[] = [];
// optimization
//let xMin = Infinity;
//let xMax = -Infinity;
@@ -254,6 +251,20 @@ export function calculateHeatmapFromData(frames: DataFrame[], options: HeatmapCa
let xField: Field | undefined = undefined;
let yField: Field | undefined = undefined;
let dataLen = 0;
// pre-allocate arrays
for (let frame of frames) {
// TODO: assumes numeric timestamps, ordered asc, without nulls
const x = frame.fields.find((f) => f.type === FieldType.time);
if (x) {
dataLen += frame.length;
}
}
let xs: number[] = Array(dataLen);
let ys: number[] = Array(dataLen);
let j = 0;
for (let frame of frames) {
// TODO: assumes numeric timestamps, ordered asc, without nulls
const x = frame.fields.find((f) => f.type === FieldType.time);
@@ -268,8 +279,12 @@ export function calculateHeatmapFromData(frames: DataFrame[], options: HeatmapCa
const xValues = x.values.toArray();
for (let field of frame.fields) {
if (field !== x && field.type === FieldType.number) {
xs = xs.concat(xValues);
ys = ys.concat(field.values.toArray());
const yValues = field.values.toArray();
for (let i = 0; i < xValues.length; i++, j++) {
xs[j] = xValues[i];
ys[j] = yValues[i];
}
if (!yField) {
yField = field;