Skip to content

Commit

Permalink
Avoid allocation on the heap for most type wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
actgardner committed Oct 11, 2021
1 parent 1e763a9 commit cb870fb
Show file tree
Hide file tree
Showing 101 changed files with 1,120 additions and 665 deletions.
15 changes: 12 additions & 3 deletions v9/container/avro/avro_container_block.go

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

15 changes: 12 additions & 3 deletions v9/container/avro/avro_container_header.go

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

30 changes: 15 additions & 15 deletions v9/container/avro/magic.go

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

30 changes: 15 additions & 15 deletions v9/container/avro/sync.go

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

25 changes: 20 additions & 5 deletions v9/example/avro/demo_schema.go

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

30 changes: 15 additions & 15 deletions v9/generator/flat/templates/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,32 @@ type {{ .WrapperType }} struct {
Target *{{ .GoType }}
}
func (_ *{{ .WrapperType }}) SetBoolean(v bool) { panic("Unsupported operation") }
func (_ *{{ .WrapperType }}) SetInt(v int32) { panic("Unsupported operation") }
func (_ *{{ .WrapperType }}) SetLong(v int64) { panic("Unsupported operation") }
func (_ *{{ .WrapperType }}) SetFloat(v float32) { panic("Unsupported operation") }
func (_ *{{ .WrapperType }}) SetDouble(v float64) { panic("Unsupported operation") }
func (_ *{{ .WrapperType }}) SetBytes(v []byte) { panic("Unsupported operation") }
func (_ *{{ .WrapperType }}) SetString(v string) { panic("Unsupported operation") }
func (_ *{{ .WrapperType }}) SetUnionElem(v int64) { panic("Unsupported operation") }
func (_ *{{ .WrapperType }}) Get(i int) types.Field { panic("Unsupported operation") }
func (_ *{{ .WrapperType }}) AppendMap(key string) types.Field { panic("Unsupported operation") }
func (_ *{{ .WrapperType }}) Finalize() { }
func (_ *{{ .WrapperType }}) SetDefault(i int) { panic("Unsupported operation") }
func (r *{{ .WrapperType }}) HintSize(s int) {
func (_ {{ .WrapperType }}) SetBoolean(v bool) { panic("Unsupported operation") }
func (_ {{ .WrapperType }}) SetInt(v int32) { panic("Unsupported operation") }
func (_ {{ .WrapperType }}) SetLong(v int64) { panic("Unsupported operation") }
func (_ {{ .WrapperType }}) SetFloat(v float32) { panic("Unsupported operation") }
func (_ {{ .WrapperType }}) SetDouble(v float64) { panic("Unsupported operation") }
func (_ {{ .WrapperType }}) SetBytes(v []byte) { panic("Unsupported operation") }
func (_ {{ .WrapperType }}) SetString(v string) { panic("Unsupported operation") }
func (_ {{ .WrapperType }}) SetUnionElem(v int64) { panic("Unsupported operation") }
func (_ {{ .WrapperType }}) Get(i int) types.Field { panic("Unsupported operation") }
func (_ {{ .WrapperType }}) AppendMap(key string) types.Field { panic("Unsupported operation") }
func (_ {{ .WrapperType }}) Finalize() { }
func (_ {{ .WrapperType }}) SetDefault(i int) { panic("Unsupported operation") }
func (r {{ .WrapperType }}) HintSize(s int) {
if len(*r.Target) == 0 {
*r.Target = make({{ .GoType }}, 0, s)
}
}
func (r *{{ .WrapperType }}) NullField(i int) {
func (r {{ .WrapperType }}) NullField(i int) {
{{ if isNullable .ItemType -}}
(*r.Target)[len(*r.Target)-1] = nil
{{ else -}}
panic("Unsupported operation")
{{ end -}}
}
func (r *{{ .WrapperType }}) AppendArray() types.Field {
func (r {{ .WrapperType }}) AppendArray() types.Field {
var v {{ .ItemType.GoType }}
{{ if .ItemConstructable -}}
{{ .ItemConstructable }}
Expand Down
30 changes: 15 additions & 15 deletions v9/generator/flat/templates/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,62 +63,62 @@ type {{ .WrapperType }} struct {
Target *{{ .GoType }}
}
func (b *{{ .WrapperType }}) SetBoolean(v bool) {
func (b {{ .WrapperType }}) SetBoolean(v bool) {
panic("Unable to assign boolean to int field")
}
func (b *{{ .WrapperType }}) SetInt(v int32) {
func (b {{ .WrapperType }}) SetInt(v int32) {
*(b.Target) = {{ .GoType }}(v)
}
func (b *{{ .WrapperType }}) SetLong(v int64) {
func (b {{ .WrapperType }}) SetLong(v int64) {
panic("Unable to assign long to int field")
}
func (b *{{ .WrapperType }}) SetFloat(v float32) {
func (b {{ .WrapperType }}) SetFloat(v float32) {
panic("Unable to assign float to int field")
}
func (b *{{ .WrapperType }}) SetUnionElem(v int64) {
func (b {{ .WrapperType }}) SetUnionElem(v int64) {
panic("Unable to assign union elem to int field")
}
func (b *{{ .WrapperType }}) SetDouble(v float64) {
func (b {{ .WrapperType }}) SetDouble(v float64) {
panic("Unable to assign double to int field")
}
func (b *{{ .WrapperType }}) SetBytes(v []byte) {
func (b {{ .WrapperType }}) SetBytes(v []byte) {
panic("Unable to assign bytes to int field")
}
func (b *{{ .WrapperType }}) SetString(v string) {
func (b {{ .WrapperType }}) SetString(v string) {
panic("Unable to assign string to int field")
}
func (b *{{ .WrapperType }}) Get(i int) types.Field {
func (b {{ .WrapperType }}) Get(i int) types.Field {
panic("Unable to get field from int field")
}
func (b *{{ .WrapperType }}) SetDefault(i int) {
func (b {{ .WrapperType }}) SetDefault(i int) {
panic("Unable to set default on int field")
}
func (b *{{ .WrapperType }}) AppendMap(key string) types.Field {
func (b {{ .WrapperType }}) AppendMap(key string) types.Field {
panic("Unable to append map key to from int field")
}
func (b *{{ .WrapperType }}) AppendArray() types.Field {
func (b {{ .WrapperType }}) AppendArray() types.Field {
panic("Unable to append array element to from int field")
}
func (b *{{ .WrapperType }}) NullField(int) {
func (b {{ .WrapperType }}) NullField(int) {
panic("Unable to null field in int field")
}
func (b *{{ .WrapperType }}) HintSize(int) {
func (b {{ .WrapperType }}) HintSize(int) {
panic("Unable to hint size in int field")
}
func (b *{{ .WrapperType }}) Finalize() {}
func (b {{ .WrapperType }}) Finalize() {}
`
30 changes: 15 additions & 15 deletions v9/generator/flat/templates/fixed.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ func (b {{ .GoType }}) MarshalJSON() ([]byte, error) {
return []byte(util.EncodeByteString(b[:])), nil
}
func (_ *{{ .WrapperType }}) SetBoolean(v bool) { panic("Unsupported operation") }
func (_ *{{ .WrapperType }}) SetInt(v int32) { panic("Unsupported operation") }
func (_ *{{ .WrapperType }}) SetLong(v int64) { panic("Unsupported operation") }
func (_ *{{ .WrapperType }}) SetFloat(v float32) { panic("Unsupported operation") }
func (_ *{{ .WrapperType }}) SetDouble(v float64) { panic("Unsupported operation") }
func (r *{{ .WrapperType }}) SetBytes(v []byte) {
func (_ {{ .WrapperType }}) SetBoolean(v bool) { panic("Unsupported operation") }
func (_ {{ .WrapperType }}) SetInt(v int32) { panic("Unsupported operation") }
func (_ {{ .WrapperType }}) SetLong(v int64) { panic("Unsupported operation") }
func (_ {{ .WrapperType }}) SetFloat(v float32) { panic("Unsupported operation") }
func (_ {{ .WrapperType }}) SetDouble(v float64) { panic("Unsupported operation") }
func (r {{ .WrapperType }}) SetBytes(v []byte) {
copy((*r.Target)[:], v)
}
func (_ *{{ .WrapperType }}) SetString(v string) { panic("Unsupported operation") }
func (_ *{{ .WrapperType }}) SetUnionElem(v int64) { panic("Unsupported operation") }
func (_ *{{ .WrapperType }}) Get(i int) types.Field { panic("Unsupported operation") }
func (_ *{{ .WrapperType }}) AppendMap(key string) types.Field { panic("Unsupported operation") }
func (_ *{{ .WrapperType }}) AppendArray() types.Field { panic("Unsupported operation") }
func (_ *{{ .WrapperType }}) NullField(int) { panic("Unsupported operation") }
func (_ *{{ .WrapperType }}) HintSize(int) { panic("Unsupported operation") }
func (_ *{{ .WrapperType }}) Finalize() { }
func (_ *{{ .WrapperType }}) SetDefault(i int) { panic("Unsupported operation") }
func (_ {{ .WrapperType }}) SetString(v string) { panic("Unsupported operation") }
func (_ {{ .WrapperType }}) SetUnionElem(v int64) { panic("Unsupported operation") }
func (_ {{ .WrapperType }}) Get(i int) types.Field { panic("Unsupported operation") }
func (_ {{ .WrapperType }}) AppendMap(key string) types.Field { panic("Unsupported operation") }
func (_ {{ .WrapperType }}) AppendArray() types.Field { panic("Unsupported operation") }
func (_ {{ .WrapperType }}) NullField(int) { panic("Unsupported operation") }
func (_ {{ .WrapperType }}) HintSize(int) { panic("Unsupported operation") }
func (_ {{ .WrapperType }}) Finalize() { }
func (_ {{ .WrapperType }}) SetDefault(i int) { panic("Unsupported operation") }
`
7 changes: 6 additions & 1 deletion v9/generator/flat/templates/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ func (r *{{ .GoType }}) Get(i int) types.Field {
{{ $.ConstructableForField $field }}
{{ end -}}
{{ if ne $field.Type.WrapperType "" -}}
return &{{ $field.Type.WrapperType }}{Target: &r.{{ $field.GoName }}}
w := {{ $field.Type.WrapperType }}{Target: &r.{{ $field.GoName }}}
{{ if $field.Type.WrapperPointer }}
return &w
{{ else }}
return w
{{ end }}
{{ else -}}
return r.{{ $field.GoName }}
{{ end -}}
Expand Down
4 changes: 4 additions & 0 deletions v9/schema/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func (s *ArrayField) WrapperType() string {
return fmt.Sprintf("%vWrapper", s.Name())
}

func (s *ArrayField) WrapperPointer() bool {
return false
}

func (s *ArrayField) IsReadableBy(f AvroType) bool {
if union, ok := f.(*UnionField); ok {
for _, t := range union.AvroTypes() {
Expand Down
2 changes: 2 additions & 0 deletions v9/schema/avro_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ type AvroType interface {

// WrapperType is the VM type to wrap this value in, if applicable
WrapperType() string
// WrapperPointer is whether the VM wrapper type needs to be a pointer
WrapperPointer() bool
IsReadableBy(f AvroType) bool
}
2 changes: 2 additions & 0 deletions v9/schema/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ func (s *EnumDefinition) WrapperType() string {
return fmt.Sprintf("%vWrapper", s.GoType())
}

func (s *EnumDefinition) WrapperPointer() bool { return false }

func (s *EnumDefinition) Children() []AvroType {
return []AvroType{}
}
2 changes: 2 additions & 0 deletions v9/schema/file_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ func (f *FileRoot) IsReadableBy(_ Definition) bool {
func (f *FileRoot) WrapperType() string {
return ""
}

func (f *FileRoot) WrapperPointer() bool { return false }
2 changes: 2 additions & 0 deletions v9/schema/fixed.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ func (s *FixedDefinition) WrapperType() string {
return fmt.Sprintf("%vWrapper", s.GoType())
}

func (s *FixedDefinition) WrapperPointer() bool { return false }

func (s *FixedDefinition) Children() []AvroType {
return []AvroType{}
}
4 changes: 4 additions & 0 deletions v9/schema/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func (s *MapField) WrapperType() string {
return fmt.Sprintf("%vWrapper", s.Name())
}

func (s *MapField) WrapperPointer() bool {
return true
}

func (s *MapField) IsReadableBy(f AvroType) bool {
if union, ok := f.(*UnionField); ok {
for _, t := range union.AvroTypes() {
Expand Down
2 changes: 2 additions & 0 deletions v9/schema/primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ func (s *PrimitiveField) Children() []AvroType {
func (s *PrimitiveField) UnionKey() string {
return s.unionKey
}

func (s *PrimitiveField) WrapperPointer() bool { return false }
Loading

0 comments on commit cb870fb

Please sign in to comment.