From 14e0f30e6959d8222b8b230d6146b81dafefea04 Mon Sep 17 00:00:00 2001 From: Nicolas Savoire Date: Fri, 27 Jan 2023 09:30:01 +0100 Subject: [PATCH] Fix encoding of length delimited fields / packed arrays (#10) Code was incorrectly assuming that LEN could always be encoded on 1 byte. LEN is encoded as a varint and should be properly measured. --- src/index.test.ts | 4 ++-- src/index.ts | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/index.test.ts b/src/index.test.ts index d4cedef..f7a21fc 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -227,13 +227,13 @@ tap.test('Mapping', (t: TestSuite) => { const sampleData = { locationId: [1, 2, 3], - value: [4, 5, 0, 6], + value: [...Array(180).keys()], label: [labelData] } const sampleEncodings = [ { field: 'locationId', value: '0a03010203' }, - { field: 'value', value: '120404050006' }, + { field: 'value', value: embeddedField('12', sampleData.value.map(x => ({field: '', value: hexVarInt(x)}))) }, { field: 'label', value: embeddedField('1a', labelEncodings) }, ] diff --git a/src/index.ts b/src/index.ts index db83f03..4fc38d7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -184,12 +184,16 @@ function measureNumberArrayField(values: Numeric[]): number { // Arrays should always include zeros to keep positions consistent total += measureNumber(value) || 1 } - return total ? 2 + total : 0 + // Packed arrays are encoded as Tag,Len,ConcatenatedElements + // Tag is only one byte because field number is always < 16 in pprof + return total ? 1 + measureNumber(total) + total : 0 } function measureLengthDelimField(value: T): number { const length = measureValue(value) - return length ? 2 + length : 0 + // Length delimited records / submessages are encoded as Tag,Len,EncodedRecord + // Tag is only one byte because field number is always < 16 in pprof + return length ? 1 + measureNumber(length) + length : 0 } function measureLengthDelimArrayField(values: T[]): number {