Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: codec use batch alloc #189

Merged
merged 1 commit into from
Apr 11, 2024
Merged
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
41 changes: 37 additions & 4 deletions generator/golang/templates/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,11 @@ func (p *{{$TypeName}}) {{.Reader}}(iprot thrift.TProtocol) error {
if {{if $isBaseVal}}_{{else}}fm{{end}}, ex := p._fieldmask.Field({{.ID}}); ex {
{{- end}}
{{$ctx := (MkRWCtx .).WithFieldMask "fm"}}
{{- $target := print $ctx.Target }}
{{- $ctx = $ctx.WithDecl.WithTarget "_field"}}
{{- template "FieldRead" $ctx}}
{{/* line break */}}
{{- $target}} = _field
{{- if Features.WithFieldMask}}
} else if err := iprot.Skip(thrift.{{.Type | GetTypeIDConstant}}); err != nil {
return err
Expand Down Expand Up @@ -539,7 +543,9 @@ var FieldRead = `
// FieldReadStructLike .
var FieldReadStructLike = `
{{define "FieldReadStructLike"}}
{{- .Target}} {{if .NeedDecl}}:{{end}}= {{.TypeName.Deref.NewFunc}}()
{{- if .NeedDecl}}
{{- .Target}} := {{.TypeName.Deref.NewFunc}}()
{{- end}}
{{- if and (Features.WithFieldMask) .NeedFieldMask}}
{{- if Features.FieldMaskHalfway}}
{{.Target}}.Pass_FieldMask({{.FieldMask}})
Expand Down Expand Up @@ -601,11 +607,15 @@ var FieldReadMap = `
{{- $isStrKey := .KeyCtx.Type | IsStrType -}}
{{- $isBaseVal := .ValCtx.Type | IsBaseType -}}
{{- $curFieldMask := .FieldMask -}}
{{- $isStructVal := .ValCtx.Type.Category.IsStructLike -}}
_, _, size, err := iprot.ReadMapBegin()
if err != nil {
return err
}
{{.Target}} {{if .NeedDecl}}:{{end}}= make({{.TypeName}}, size)
{{- if $isStructVal}}
values := make([]{{.ValCtx.TypeName.Deref}}, size)
{{- end}}
for i := 0; i < size; i++ {
{{- $key := .GenID "_key"}}
{{- $ctx := .KeyCtx.WithDecl.WithTarget $key}}
Expand Down Expand Up @@ -637,7 +647,12 @@ var FieldReadMap = `
{{- end}}{{/* end WithFieldMask */}}
{{/* line break */}}
{{- $val := .GenID "_val"}}
{{- $ctx := (.ValCtx.WithDecl.WithTarget $val).WithFieldMask $curFieldMask}}
{{- $ctx := (.ValCtx.WithTarget $val).WithFieldMask $curFieldMask}}
{{- if $isStructVal}}
{{$val}} := &values[i]
{{- else}}
{{- $ctx = $ctx.WithDecl}}
{{- end}}
{{- template "FieldRead" $ctx}}

{{if and .ValCtx.Type.Category.IsStructLike Features.ValueTypeForSIC}}
Expand All @@ -660,11 +675,15 @@ var FieldReadSet = `
{{define "FieldReadSet"}}
{{- $isBaseVal := .ValCtx.Type | IsBaseType -}}
{{- $curFieldMask := .FieldMask -}}
{{- $isStructVal := .ValCtx.Type.Category.IsStructLike -}}
_, size, err := iprot.ReadSetBegin()
if err != nil {
return err
}
{{.Target}} {{if .NeedDecl}}:{{end}}= make({{.TypeName}}, 0, size)
{{- if $isStructVal}}
values := make([]{{.ValCtx.TypeName.Deref}}, size)
{{- end}}
for i := 0; i < size; i++ {
{{- $val := .GenID "_elem"}}
{{- if Features.WithFieldMask}}
Expand All @@ -676,7 +695,12 @@ var FieldReadSet = `
continue
} else {
{{- end}}
{{- $ctx := (.ValCtx.WithDecl.WithTarget $val).WithFieldMask $curFieldMask}}
{{- $ctx := (.ValCtx.WithTarget $val).WithFieldMask $curFieldMask}}
{{- if $isStructVal}}
{{$val}} := &values[i]
{{- else}}
{{- $ctx = $ctx.WithDecl}}
{{- end}}
{{template "FieldRead" $ctx}}

{{if and .ValCtx.Type.Category.IsStructLike Features.ValueTypeForSIC}}
Expand All @@ -699,11 +723,15 @@ var FieldReadList = `
{{define "FieldReadList"}}
{{- $isBaseVal := .ValCtx.Type | IsBaseType -}}
{{- $curFieldMask := .FieldMask -}}
{{- $isStructVal := .ValCtx.Type.Category.IsStructLike -}}
_, size, err := iprot.ReadListBegin()
if err != nil {
return err
}
{{.Target}} {{if .NeedDecl}}:{{end}}= make({{.TypeName}}, 0, size)
{{- if $isStructVal}}
values := make([]{{.ValCtx.TypeName.Deref}}, size)
{{- end}}
for i := 0; i < size; i++ {
{{- $val := .GenID "_elem"}}
{{- if Features.WithFieldMask}}
Expand All @@ -715,7 +743,12 @@ var FieldReadList = `
continue
} else {
{{- end}}
{{- $ctx := (.ValCtx.WithDecl.WithTarget $val).WithFieldMask $curFieldMask}}
{{- $ctx := .ValCtx.WithTarget $val}}
{{- if $isStructVal}}
{{$val}} := &values[i]
{{- else}}
{{- $ctx = $ctx.WithDecl}}
{{- end}}
{{template "FieldRead" $ctx}}

{{if and .ValCtx.Type.Category.IsStructLike Features.ValueTypeForSIC}}
Expand Down
Loading