diff --git a/ts/src/profile-serializer.ts b/ts/src/profile-serializer.ts index aecac7a2..a93739ca 100644 --- a/ts/src/profile-serializer.ts +++ b/ts/src/profile-serializer.ts @@ -17,7 +17,6 @@ import { Function, Label, - LabelInput, Line, Location, Profile, @@ -122,9 +121,9 @@ function serialize( const functionIdMap = new Map(); const packColumns = columnNumbers === 'pack'; const emitColumns = columnNumbers === 'emit'; - const labelCaches: Map[] = []; + const labelCaches: Map[] = []; for (const l of lowCardinalityLabels) { - labelCaches[stringTable.dedup(l)] = new Map(); + labelCaches[stringTable.dedup(l)] = new Map(); } let profile: ProfileInput = {}; @@ -157,7 +156,7 @@ function serialize( new Sample({ locationId: stacks[sample.stackId], value: sample.values, - label: dedupLabels(buildLabels(sample.labels)), + label: buildLabels(sample.labels), }), ); }, @@ -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; diff --git a/ts/test/test-profile-serializer.ts b/ts/test/test-profile-serializer.ts index 7beed1f7..584b8e9b 100644 --- a/ts/test/test-profile-serializer.ts +++ b/ts/test/test-profile-serializer.ts @@ -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', () => {