Skip to content

Commit e29e203

Browse files
committed
slightly optimize generated code
1 parent a1b6d14 commit e29e203

File tree

4 files changed

+72
-56
lines changed

4 files changed

+72
-56
lines changed

network/vpack/gen.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,14 @@ const parseFuncFooter = `
134134
// parseFuncTemplate decodes a struct encoded as a map.
135135
// It calls `makeNumericConst` for special integer values in a top-level FuncMap.
136136
const parseFuncTemplate = `
137-
cnt, err := p.expectWriteMapMarker({{.MaxFieldCount}}, c)
137+
cnt, err := p.readFixMap()
138138
if err != nil {
139-
return fmt.Errorf("map for {{.TypeName}}: %w", err)
139+
return fmt.Errorf("reading map for {{.TypeName}}: %w", err)
140140
}
141+
if cnt < 1 || cnt > {{.MaxFieldCount}} {
142+
return fmt.Errorf("expected fixmap size for {{.TypeName}} 1 <= cnt <= {{.MaxFieldCount}}, got %d", cnt)
143+
}
144+
c.writeStatic(StaticIdxMapMarker0+cnt)
141145
142146
for range cnt {
143147
key, err := p.readString()
@@ -153,6 +157,7 @@ const parseFuncTemplate = `
153157
c.writeStatic({{$fd.FieldNameConst}})
154158
{{renderParseFunction $fd.SubStructName}}
155159
{{- else if $fd.IsUint64Alias}}
160+
{{ if not $fd.VpackSpecial }}c.writeStatic({{$fd.FieldNameConst}}){{end}}
156161
valBytes, err := p.readUintBytes()
157162
if err != nil {
158163
return fmt.Errorf("reading {{$fd.CodecName}}: %w", err)
@@ -170,9 +175,10 @@ const parseFuncTemplate = `
170175
}
171176
}
172177
{{end}}
173-
c.writeStatic({{$fd.FieldNameConst}})
178+
{{ if $fd.VpackSpecial }}c.writeStatic({{$fd.FieldNameConst}}){{ end }}
174179
c.writeDynamicVaruint(valBytes)
175180
{{- else if gt $fd.ArrayLen 0}}
181+
{{ if and (not $fd.VpackSpecial) (not $fd.IsZeroCheck) }}c.writeStatic({{$fd.FieldNameConst}}){{end}}
176182
val, err := p.readBin{{$fd.ArrayLen}}()
177183
if err != nil {
178184
return fmt.Errorf("reading {{$fd.CodecName}}: %w", err)
@@ -185,10 +191,10 @@ const parseFuncTemplate = `
185191
c.writeLiteralBin{{$fd.ArrayLen}}(val)
186192
}
187193
{{- else if $fd.IsLiteral}}
188-
c.writeStatic({{$fd.FieldNameConst}})
194+
{{ if $fd.VpackSpecial }}c.writeStatic({{$fd.FieldNameConst}}){{ end }}
189195
c.writeLiteralBin{{$fd.ArrayLen}}(val)
190196
{{- else}}
191-
c.writeStatic({{$fd.FieldNameConst}})
197+
{{ if $fd.VpackSpecial }}c.writeStatic({{$fd.FieldNameConst}}){{ end }}
192198
c.writeDynamicBin{{$fd.ArrayLen}}(val)
193199
{{- end}}
194200
{{- else}}
@@ -232,7 +238,7 @@ func (g *codeGenerator) generate(root reflect.Type) error {
232238
// Also define map-marker constants for 1..6
233239
// We don't need more than this, and an error will be thrown if a
234240
// field grows beyond 6 items.
235-
for i := 1; i <= 6; i++ {
241+
for i := 0; i <= 6; i++ {
236242
g.getOrCreateMapMarkerIndex(i)
237243
}
238244

network/vpack/msgp.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -184,30 +184,3 @@ func (p *parser) readUintBytes() ([]byte, error) {
184184
p.pos += dataSize
185185
return p.data[startPos : startPos+dataSize+1], nil
186186
}
187-
188-
func (p *parser) expectWriteMapMarker(maxSz int, c compressWriter) (int, error) {
189-
cnt, err := p.readFixMap()
190-
if err != nil {
191-
return 0, err
192-
}
193-
if cnt < 1 || cnt > maxSz {
194-
return 0, fmt.Errorf("expected fixmap size %d <= cnt <= %d, got %d", 1, maxSz, cnt)
195-
}
196-
switch cnt {
197-
case 1:
198-
c.writeStatic(StaticIdxMapMarker1)
199-
case 2:
200-
c.writeStatic(StaticIdxMapMarker2)
201-
case 3:
202-
c.writeStatic(StaticIdxMapMarker3)
203-
case 4:
204-
c.writeStatic(StaticIdxMapMarker4)
205-
case 5:
206-
c.writeStatic(StaticIdxMapMarker5)
207-
case 6:
208-
c.writeStatic(StaticIdxMapMarker6)
209-
default:
210-
return 0, fmt.Errorf("unexpected fixmap size: %d", cnt)
211-
}
212-
return cnt, nil
213-
}

network/vpack/parse.go

Lines changed: 58 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

network/vpack/static_table.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)