Skip to content

Commit

Permalink
std: optimize malloc in encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
pulsejet committed Dec 25, 2024
1 parent 469a1ef commit 4134db7
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 48 deletions.
12 changes: 8 additions & 4 deletions std/encoding/codegen/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (m *TlvModel) GenInitEncoder(buf *bytes.Buffer) error {
encoder.length = l
{{if .NoCopy}}
wirePlan := make([]uint, 0)
wirePlan := make([]uint, 0, 16)
l = uint(0)
{{- range $f := .Fields}}
{{$f.GenEncodingWirePlan}}
Expand Down Expand Up @@ -106,11 +106,15 @@ func (m *TlvModel) GenEncodeInto(buf *bytes.Buffer) error {
func (encoder *{{.Name}}Encoder) Encode(value *{{.Name}}) enc.Wire {
{{if .NoCopy}}
total := uint(0)
for _, l := range encoder.wirePlan {
total += l
}
inner := make([]byte, total)
wire := make(enc.Wire, len(encoder.wirePlan))
for i, l := range encoder.wirePlan {
if l > 0 {
wire[i] = make([]byte, l)
}
wire[i] = inner[:l]
inner = inner[l:]
}
encoder.EncodeInto(value, wire)
{{else}}
Expand Down
12 changes: 8 additions & 4 deletions std/encoding/tests/gen_basic/zz_generated.go

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

36 changes: 24 additions & 12 deletions std/encoding/tests/gen_composition/zz_generated.go

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

24 changes: 16 additions & 8 deletions std/encoding/tests/gen_signature/zz_generated.go

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

48 changes: 32 additions & 16 deletions std/ndn/spec_2022/zz_generated.go

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

12 changes: 8 additions & 4 deletions std/schema/demosec/zz_generated.go

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

0 comments on commit 4134db7

Please sign in to comment.