Skip to content

Commit

Permalink
Deduplicate inlined type info if repeated
Browse files Browse the repository at this point in the history
  • Loading branch information
fxamacker committed Mar 8, 2024
1 parent 6825950 commit 35fdb7e
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 219 deletions.
4 changes: 4 additions & 0 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ func (a *ArrayExtraData) isExtraData() bool {
return true
}

func (a *ArrayExtraData) Type() TypeInfo {
return a.TypeInfo
}

// Encode encodes extra data as CBOR array:
//
// [type info]
Expand Down
67 changes: 20 additions & 47 deletions array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3186,17 +3186,14 @@ func TestArrayEncodeDecode(t *testing.T) {
// inlined extra data
0x82,
// element 0: array of type info
0x81,
// type info
0x18, 0x2b,
0x80,
// element 1: array of extra data
0x81,
// array extra data
0xd8, 0xf7,
0x81,
// array type info ref
0xd8, 0xf6,
0x00,
0x18, 0x2b,

// CBOR encoded array head (fixed size 3 byte)
0x99, 0x00, 0x02,
Expand Down Expand Up @@ -3275,21 +3272,17 @@ func TestArrayEncodeDecode(t *testing.T) {
// inlined extra data
0x82,
// element 0: array of inlined type info
0x82,
0x18, 0x2c,
0x18, 0x2b,
0x80,
// element 1: array of inlined extra data
0x82,
// inlined array extra data
0xd8, 0xf7,
0x81,
0xd8, 0xf6,
0x00,
0x18, 0x2c,
// inlined array extra data
0xd8, 0xf7,
0x81,
0xd8, 0xf6,
0x01,
0x18, 0x2b,

// CBOR encoded array head (fixed size 3 byte)
0x99, 0x00, 0x02,
Expand Down Expand Up @@ -3372,20 +3365,16 @@ func TestArrayEncodeDecode(t *testing.T) {
// inlined extra data
0x82,
// element 0: array of inlined type info
0x82,
0x18, 0x2c,
0x18, 0x2b,
0x80,
// element 1: array of inlined extra data
0x82,
// inlined array extra data
0xd8, 0xf7,
0x81,
0xd8, 0xf6,
0x00,
0x18, 0x2c,
0xd8, 0xf7,
0x81,
0xd8, 0xf6,
0x01,
0x18, 0x2b,

// CBOR encoded array head (fixed size 3 byte)
0x99, 0x00, 0x02,
Expand Down Expand Up @@ -3480,33 +3469,25 @@ func TestArrayEncodeDecode(t *testing.T) {
// inlined extra data
0x82,
// element 0: array of inlined type info
0x84,
0x18, 0x2c,
0x18, 0x2b,
0x18, 0x2e,
0x18, 0x2d,
0x80,
// element 1: array of inlined extra data
0x84,
// typeInfo3
0xd8, 0xf7,
0x81,
0xd8, 0xf6,
0x00,
0x18, 0x2c,
// typeInfo2
0xd8, 0xf7,
0x81,
0xd8, 0xf6,
0x01,
0x18, 0x2b,
// typeInfo5
0xd8, 0xf7,
0x81,
0xd8, 0xf6,
0x02,
0x18, 0x2e,
// typeInfo4
0xd8, 0xf7,
0x81,
0xd8, 0xf6,
0x03,
0x18, 0x2d,

// CBOR encoded array head (fixed size 3 byte)
0x99, 0x00, 0x02,
Expand Down Expand Up @@ -3633,15 +3614,13 @@ func TestArrayEncodeDecode(t *testing.T) {
// inlined extra data
0x82,
// element 0: array of inlined type info
0x81,
0x18, 0x2b,
0x80,
// element 1: array of inlined extra data
0x81,
// inlined array extra data
0xd8, 0xf7,
0x81,
0xd8, 0xf6,
0x00,
0x18, 0x2b,
// CBOR encoded array head (fixed size 3 byte)
0x99, 0x00, 0x0b,
// CBOR encoded array elements
Expand Down Expand Up @@ -3787,20 +3766,16 @@ func TestArrayEncodeDecode(t *testing.T) {
// inlined extra data
0x82,
// element 0: array of inlined extra data
0x82,
0x18, 0x2c,
0x18, 0x2b,
0x80,
// element 1: array of inlined extra data
0x82,
// inlined array extra data
0xd8, 0xf7,
0x81,
0xd8, 0xf6,
0x00,
0x18, 0x2c,
0xd8, 0xf7,
0x81,
0xd8, 0xf6,
0x01,
0x18, 0x2b,
// CBOR encoded array head (fixed size 3 byte)
0x99, 0x00, 0x0b,
// CBOR encoded array elements
Expand Down Expand Up @@ -4117,15 +4092,13 @@ func TestArrayEncodeDecode(t *testing.T) {
// inlined extra data
0x82,
// element 0: array of type info
0x81,
0x18, 0x2c,
0x80,
// element 1: array of extra data
0x81,
// type info
0xd8, 0xf7,
0x81,
0xd8, 0xf6,
0x00,
0x18, 0x2c,

// CBOR encoded array head (fixed size 3 byte)
0x99, 0x00, 0x0b,
Expand Down
4 changes: 4 additions & 0 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,10 @@ func (m *MapExtraData) isExtraData() bool {
return true
}

func (m *MapExtraData) Type() TypeInfo {
return m.TypeInfo
}

// Encode encodes extra data as CBOR array:
//
// [type info, count, seed]
Expand Down
Loading

0 comments on commit 35fdb7e

Please sign in to comment.