Skip to content

Commit

Permalink
use scratch buffers to avoid even more allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping committed May 3, 2020
1 parent 84675a8 commit f946256
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 47 deletions.
44 changes: 39 additions & 5 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func doTemplate(w io.Writer, info interface{}, templ string) error {
t := template.Must(template.New("").
Funcs(template.FuncMap{
"MajorType": func(wname string, tname string, val string) string {
return fmt.Sprintf(`if err := cbg.WriteMajorTypeHeader(%s, %s, uint64(%s)); err != nil {
return fmt.Sprintf(`if err := cbg.WriteMajorTypeHeaderBuf(scratch, %s, %s, uint64(%s)); err != nil {
return err
}`, wname, tname, val)
},
Expand Down Expand Up @@ -94,6 +94,34 @@ type GenTypeInfo struct {
Fields []Field
}

func (gti *GenTypeInfo) NeedsScratch() bool {
for _, f := range gti.Fields {
switch f.Type.Kind() {
case reflect.String,
reflect.Uint64,
reflect.Int64,
reflect.Uint8,
reflect.Array,
reflect.Slice,
reflect.Map:
return true

case reflect.Struct:
fname := f.Type.PkgPath() + "." + f.Type.Name()
switch fname {
case "math/big.Int":
return true
case "github.com/ipfs/go-cid.Cid":
return true
}
// nope
case reflect.Bool:
// nope
}
}
return false
}

func nameIsExported(name string) bool {
return strings.ToUpper(name[0:1]) == name[0:1]
}
Expand Down Expand Up @@ -168,7 +196,7 @@ func emitCborMarshalStringField(w io.Writer, f Field) error {
}
{{ MajorType "w" "cbg.MajTextString" (print "len(" .Name ")") }}
if _, err := w.Write([]byte({{ .Name }})); err != nil {
if _, err := io.WriteString(w, {{ .Name }}); err != nil {
return err
}
`)
Expand Down Expand Up @@ -204,12 +232,12 @@ func emitCborMarshalStructField(w io.Writer, f Field) error {
return err
}
} else {
if err := cbg.WriteCid(w, *{{ .Name }}); err != nil {
if err := cbg.WriteCidBuf(scratch, w, *{{ .Name }}); err != nil {
return xerrors.Errorf("failed to write cid field {{ .Name }}: %w", err)
}
}
{{ else }}
if err := cbg.WriteCid(w, {{ .Name }}); err != nil {
if err := cbg.WriteCidBuf(scratch, w, {{ .Name }}); err != nil {
return xerrors.Errorf("failed to write cid field {{ .Name }}: %w", err)
}
{{ end }}
Expand Down Expand Up @@ -372,7 +400,7 @@ func emitCborMarshalSliceField(w io.Writer, f Field) error {
switch fname {
case "github.com/ipfs/go-cid.Cid":
err := doTemplate(w, f, `
if err := cbg.WriteCid(w, v); err != nil {
if err := cbg.WriteCidBuf(scratch, w, v); err != nil {
return xerrors.Errorf("failed writing cid field {{ .Name }}: %w", err)
}
`)
Expand Down Expand Up @@ -436,6 +464,9 @@ func (t *{{ .Name }}) MarshalCBOR(w io.Writer) error {
if _, err := w.Write(lengthBuf{{ .Name }}); err != nil {
return err
}
{{ if .NeedsScratch }}
scratch := make([]byte, 9)
{{ end }}
`)
if err != nil {
return err
Expand Down Expand Up @@ -1039,6 +1070,9 @@ func emitCborMarshalStructMap(w io.Writer, gti *GenTypeInfo) error {
if _, err := w.Write({{ .MapHeaderAsByteString }}); err != nil {
return err
}
{{ if .NeedsScratch }}
scratch := make([]byte, 9)
{{ end }}
`)
if err != nil {
return err
Expand Down
48 changes: 28 additions & 20 deletions testing/cbor_gen.go

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

Loading

0 comments on commit f946256

Please sign in to comment.