Skip to content
Open
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
51 changes: 51 additions & 0 deletions gen/decl.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,30 @@ func (g *generator) parseIntType(lhs string, t *ast.IntType) {
}

func (g *generator) parseArray(lhs string, base ast.Type, s ast.LengthConstraint) {
if unconstrainedByteType(base) {
switch s := s.(type) {
case *ast.IntegerConstRef, *ast.IntegerLiteral:
size := g.integer(s)
g.lengthCheck(size)
g.printf("copy(%s[:], %s[:%s])\n", lhs, g.data, size)
g.printf("%s = %s[%s:]\n", g.data, g.data, size)
return
case *ast.IDRef:
ref := g.ref(s)
g.printf("if uint64(%s) > uint64(len(%s)) { return nil, errors.New(\"data too short\") }\n", ref, g.data)
size := fmt.Sprintf("int(%s)", ref)
g.printf("%s = make([]%s, %s)\n", lhs, g.tipe(base), size)
g.printf("copy(%s, %s[:%s])\n", lhs, g.data, size)
g.printf("%s = %s[%s:]\n", g.data, g.data, size)
return
case nil:
g.printf("%s = make([]%s, len(%s))\n", lhs, g.tipe(base), g.data)
g.printf("copy(%s, %s)\n", lhs, g.data)
g.printf("%s = %s[len(%s):]\n", g.data, g.data, g.data)
return
}
}

switch s := s.(type) {
case *ast.IntegerConstRef, *ast.IntegerLiteral:
g.printf("for idx := 0; idx < %s; idx++ {\n", g.integer(s))
Expand Down Expand Up @@ -382,6 +406,19 @@ func elementByteSize(base ast.Type) int {
}
}

// unconstrainedByteType reports whether base can be copied without skipping
// per-element validation.
func unconstrainedByteType(base ast.Type) bool {
switch t := base.(type) {
case *ast.IntType:
return t.Size == 8 && t.Constraint == nil
case *ast.CharType:
return true
default:
return false
}
}

func (g *generator) integer(i ast.Integer) string {
x, err := g.resolver.Integer(i)
if err != nil {
Expand Down Expand Up @@ -537,6 +574,16 @@ func (g *generator) encodeIntType(rhs string, t *ast.IntType) {
}

func (g *generator) encodeArray(rhs string, base ast.Type, s ast.LengthConstraint) {
if unconstrainedByteType(base) {
switch s.(type) {
case *ast.IntegerConstRef, *ast.IntegerLiteral:
g.printf("%s = append(%s, %s[:]...)\n", g.data, g.data, rhs)
default:
g.printf("%s = append(%s, %s...)\n", g.data, g.data, rhs)
}
return
}

switch s := s.(type) {
case *ast.IntegerConstRef, *ast.IntegerLiteral:
g.printf("for idx := 0; idx < %s; idx++ {\n", g.integer(s))
Expand Down Expand Up @@ -662,6 +709,10 @@ func (g *generator) validateArray(rhs string, base ast.Type, s ast.LengthConstra
panic(unexpected(s))
}

if unconstrainedByteType(base) {
return
}

// Validate array elements
g.printf("for idx := 0; idx < len(%s); idx++ {\n", rhs)
g.validateType(rhs+"[idx]", base)
Expand Down
22 changes: 5 additions & 17 deletions gen/tests/contexts/gen-marshallers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 10 additions & 22 deletions gen/tests/fixie/gen-marshallers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions gen/tests/rem/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package rem

import "testing"

func BenchmarkParseByteTail(b *testing.B) {
data := make([]byte, 4+64*1024)
b.SetBytes(int64(len(data)))
b.ReportAllocs()
b.ResetTimer()
for range b.N {
if _, err := ParseRem(data); err != nil {
b.Fatal(err)
}
}
}

func BenchmarkMarshalByteTail(b *testing.B) {
message := &Rem{Tail: make([]byte, 64*1024)}
b.SetBytes(int64(4 + len(message.Tail)))
b.ReportAllocs()
b.ResetTimer()
for range b.N {
if _, err := message.MarshalBinary(); err != nil {
b.Fatal(err)
}
}
}
19 changes: 4 additions & 15 deletions gen/tests/rem/gen-marshallers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions gen/tests/rem/rem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,12 @@ func TestRemParseSuccess(t *testing.T) {
Tail: []byte("thetail"),
}, r)
}

func TestRemParseCopiesTail(t *testing.T) {
b := []byte{0, 1, 2, 3, 4, 5, 6}
r, err := ParseRem(b)
require.NoError(t, err)

b[4] = 99
assert.Equal(t, []byte{4, 5, 6}, r.Tail)
}
16 changes: 5 additions & 11 deletions gen/tests/unionbasic/gen-marshallers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 4 additions & 15 deletions gen/tests/unionlen/gen-marshallers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading