Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 15 additions & 29 deletions ts/src/profile-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import {
Function,
Label,
LabelInput,
Line,
Location,
Profile,
Expand Down Expand Up @@ -122,9 +121,9 @@ function serialize(
const functionIdMap = new Map<string, number>();
const packColumns = columnNumbers === 'pack';
const emitColumns = columnNumbers === 'emit';
const labelCaches: Map<number | bigint, Label>[] = [];
const labelCaches: Map<number, Label>[] = [];
for (const l of lowCardinalityLabels) {
labelCaches[stringTable.dedup(l)] = new Map<number | bigint, Label>();
labelCaches[stringTable.dedup(l)] = new Map<number, Label>();
}
let profile: ProfileInput = {};

Expand Down Expand Up @@ -157,7 +156,7 @@ function serialize(
new Sample({
locationId: stacks[sample.stackId],
value: sample.values,
label: dedupLabels(buildLabels(sample.labels)),
label: buildLabels(sample.labels),
}),
);
},
Expand Down Expand Up @@ -226,33 +225,20 @@ function serialize(
const labels: Label[] = new Array(sampleLabels.length);
for (let i = 0; i < sampleLabels.length; i++) {
const [key, value] = sampleLabels[i];
const labelInput: LabelInput = {key: stringTable.dedup(key)};
if (typeof value === 'string') {
labelInput.str = stringTable.dedup(value);
const keyIndex = stringTable.dedup(key);
const isString = typeof value === 'string';
const str = isString ? stringTable.dedup(value) : 0;
const num = isString ? 0 : value || 0;
const cache = labelCaches[keyIndex];
const cached = cache?.get(str);
if (cached?.num === num) {
labels[i] = cached;
} else {
labelInput.num = value;
}
labels[i] = new Label(labelInput);
}
return labels;
}

function dedupLabels(labels: Label[]) {
for (let i = 0; i < labels.length; i++) {
const label = labels[i];
const cache = labelCaches[Number(label.key)];
if (cache !== undefined) {
const key = label.str ?? label.num;
const exlabel = cache.get(key);
if (exlabel === undefined) {
cache.set(key, label);
} else if (
label.str === exlabel.str &&
label.num === exlabel.num &&
label.numUnit === exlabel.numUnit
) {
labels[i] = exlabel;
const label = new Label({key: keyIndex, str, num});
if (cache !== undefined && cached === undefined) {
cache.set(str, label);
}
labels[i] = label;
}
}
return labels;
Expand Down
53 changes: 53 additions & 0 deletions ts/test/test-profile-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,59 @@ describe('profile-serializer', () => {
new Label({key: st.dedup('someBigint'), num: 18446744073709551557n}),
]);
});

it('shares cached labels without changing label values or order', () => {
const values = [42, 7n, 7n, 42, 'same', 'same'];
const zeroValues = ['', 0n, NaN, -0, '', 0];
const {topDownRoot} = labelEncodingProfile;
const leaf: TimeProfileNode = {
...topDownRoot.children[0],
hitCount: values.length,
contexts: values.map((value, index) => ({
context: {cached: value, zero: zeroValues[index], uncached: 'same'},
timestamp: BigInt(index + 1),
})),
};
const profile = serializeTimeProfile(
{
...labelEncodingProfile,
topDownRoot: {...topDownRoot, children: [leaf]},
},
1000,
undefined,
false,
undefined,
['cached', 'zero'],
);
const labels = profile.sample.map(sample => sample.label);
const strings = profile.stringTable.strings;
assert.deepStrictEqual(
labels.map(sample =>
sample.map(label => [
strings[Number(label.key)],
strings[Number(label.str)],
label.num,
]),
),
values.map(value => [
[
'cached',
typeof value === 'string' ? value : '',
typeof value === 'string' ? 0 : value,
],
['zero', '', 0],
['uncached', 'same', 0],
]),
);
assert.strictEqual(labels[0][0], labels[3][0]);
assert.strictEqual(labels[4][0], labels[5][0]);
assert.notStrictEqual(labels[1][0], labels[2][0]);
assert.ok(labels.every(sample => sample[1] === labels[0][1]));
assert.strictEqual(
new Set(labels.map(sample => sample[2])).size,
values.length,
);
});
});

describe('serializeHeapProfile', () => {
Expand Down
Loading