Skip to content

Commit

Permalink
Fix encoding of length delimited fields / packed arrays (#10)
Browse files Browse the repository at this point in the history
Code was incorrectly assuming that LEN could always be encoded on 1 byte.
LEN is encoded as a varint and should be properly measured.
  • Loading branch information
nsavoire committed Jan 27, 2023
1 parent b6cc0ba commit 14e0f30
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
]

Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(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<T>(values: T[]): number {
Expand Down

0 comments on commit 14e0f30

Please sign in to comment.