FieldValues: Use plain arrays instead of Vector (part 1 of 2) (#66187)

Co-authored-by: Leon Sorokin <leeoniya@gmail.com>
Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
This commit is contained in:
Ryan McKinley
2023-04-14 05:36:53 -07:00
committed by GitHub
parent 4f5b80095e
commit b8188eead4
37 changed files with 531 additions and 292 deletions

View File

@@ -1,7 +1,6 @@
import { mergeMap, from } from 'rxjs';
import {
ArrayVector,
DataFrame,
DataTransformerID,
Field,
@@ -53,12 +52,12 @@ export function addFieldsFromGazetteer(frames: DataFrame[], gaz: Gazetteer, matc
//if the field matches
if (matcher(field, frame, frames)) {
const values = field.values.toArray();
const values = field.values;
const sub: any[][] = [];
for (const f of src) {
const buffer = new Array(length);
sub.push(buffer);
fields.push({ ...f, values: new ArrayVector(buffer) });
fields.push({ ...f, values: buffer });
}
// Add all values to the buffer

View File

@@ -15,8 +15,8 @@ describe('timeSeriesTableTransformer', () => {
const result = results[0];
expect(result.refId).toBe('A');
expect(result.fields).toHaveLength(3);
expect(result.fields[0].values.toArray()).toEqual(['A', 'A', 'A']);
expect(result.fields[1].values.toArray()).toEqual(['B', 'C', 'D']);
expect(result.fields[0].values).toEqual(['A', 'A', 'A']);
expect(result.fields[1].values).toEqual(['B', 'C', 'D']);
assertDataFrameField(result.fields[2], series);
});
@@ -33,8 +33,8 @@ describe('timeSeriesTableTransformer', () => {
expect(results[0]).toEqual(series[0]);
expect(results[1].refId).toBe('A');
expect(results[1].fields).toHaveLength(3);
expect(results[1].fields[0].values.toArray()).toEqual(['A', 'A']);
expect(results[1].fields[1].values.toArray()).toEqual(['B', 'C']);
expect(results[1].fields[0].values).toEqual(['A', 'A']);
expect(results[1].fields[1].values).toEqual(['B', 'C']);
expect(results[2]).toEqual(series[3]);
});
@@ -51,14 +51,14 @@ describe('timeSeriesTableTransformer', () => {
expect(results).toHaveLength(2);
expect(results[0].refId).toBe('A');
expect(results[0].fields).toHaveLength(3);
expect(results[0].fields[0].values.toArray()).toEqual(['A', 'A', 'A']);
expect(results[0].fields[1].values.toArray()).toEqual(['B', 'C', 'D']);
expect(results[0].fields[0].values).toEqual(['A', 'A', 'A']);
expect(results[0].fields[1].values).toEqual(['B', 'C', 'D']);
assertDataFrameField(results[0].fields[2], series.slice(0, 3));
expect(results[1].refId).toBe('B');
expect(results[1].fields).toHaveLength(4);
expect(results[1].fields[0].values.toArray()).toEqual(['B', 'B']);
expect(results[1].fields[1].values.toArray()).toEqual(['F', 'G']);
expect(results[1].fields[2].values.toArray()).toEqual(['A', 'B']);
expect(results[1].fields[0].values).toEqual(['B', 'B']);
expect(results[1].fields[1].values).toEqual(['F', 'G']);
expect(results[1].fields[2].values).toEqual(['A', 'B']);
assertDataFrameField(results[1].fields[3], series.slice(3, 5));
});
});
@@ -66,12 +66,12 @@ describe('timeSeriesTableTransformer', () => {
function assertFieldsEqual(field1: Field, field2: Field) {
expect(field1.type).toEqual(field2.type);
expect(field1.name).toEqual(field2.name);
expect(field1.values.toArray()).toEqual(field2.values.toArray());
expect(field1.values).toEqual(field2.values);
expect(field1.labels ?? {}).toEqual(field2.labels ?? {});
}
function assertDataFrameField(field: Field, matchesFrames: DataFrame[]) {
const frames: DataFrame[] = field.values.toArray();
const frames: DataFrame[] = field.values;
expect(frames).toHaveLength(matchesFrames.length);
frames.forEach((frame, idx) => {
const matchingFrame = matchesFrames[idx];

View File

@@ -68,7 +68,11 @@ export function timeSeriesToTableTransform(options: TimeSeriesTableTransformerOp
values: new ArrayVector(),
};
refId2frameField[refId] = frameField;
const table = new MutableDataFrame();
// NOTE: MutableDataFrame.addField() makes copies, including any .values buffers
// since we do .values.add() later on the *originals*, we pass a custom MutableVectorCreator
// which will re-use the existing empty .values buffer by reference
const table = new MutableDataFrame(undefined, (buffer) => buffer ?? []);
for (const label of Object.values(labelFields)) {
table.addField(label);
}
@@ -81,7 +85,7 @@ export function timeSeriesToTableTransform(options: TimeSeriesTableTransformerOp
const labels = frame.fields[1].labels;
for (const labelKey of Object.keys(labelFields)) {
const labelValue = labels?.[labelKey] ?? null;
labelFields[labelKey].values.add(labelValue);
labelFields[labelKey].values.add(labelValue!);
}
frameField.values.add(frame);