Skip to content

Commit 9f3ed37

Browse files
chressiestapelberg
authored andcommitted
internal_gengo: generate a const string literal for the raw descriptor
Putting the raw descriptor into a const string literal has been attempted before (go.dev/cl/638135), but that change has been reverted (go.dev/cl/642857) because the generated string contained non-UTF-8 bytes. This time we let the %q formatting verb handle the conversion of the bytes into a UTF-8-conform string literal. We also found an interesting way to split the bytes into multiple "lines": We use the fact that FileDescriptorProto (and some submessages) have a LEN encoded field (string) with field_number=1, so splitting at 0x0a (incidentally a newline character in ascii) we get a splitting that almost looks readable. As expected, the const string literals are landing in .rodata. % (go test -c google.golang.org/protobuf/internal/reflection_test && strings -tx reflection_test.test | grep -F '2&.goproto.proto.testeditions.ImportEnumB' && readelf -SW reflection_test.test | grep -F .rodata) 321b07 2&.goproto.proto.testeditions.ImportEnumB [ 2] .rodata PROGBITS 000000000065a000 25a000 131fc1 00 A 0 0 32 Change-Id: I77aef7b5032e52c2c485cf662045c9028549fd94 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/657895 Commit-Queue: Michael Stapelberg <[email protected]> Reviewed-by: Cassondra Foesch <[email protected]> Reviewed-by: Nicolas Hillegeer <[email protected]> Reviewed-by: Michael Stapelberg <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 4247b1b commit 9f3ed37

File tree

143 files changed

+9460
-26587
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

143 files changed

+9460
-26587
lines changed

cmd/protoc-gen-go/internal_gengo/reflect.go

+16-15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package internal_gengo
66

77
import (
8+
"bytes"
89
"fmt"
910
"math"
1011
"strings"
@@ -242,22 +243,22 @@ func genFileDescriptor(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileI
242243
return
243244
}
244245

245-
g.P("var ", rawDescVarName(f), " = string([]byte{")
246-
for len(b) > 0 {
247-
n := 16
248-
if n > len(b) {
249-
n = len(b)
250-
}
251-
252-
s := ""
253-
for _, c := range b[:n] {
254-
s += fmt.Sprintf("0x%02x,", c)
255-
}
256-
g.P(s)
257-
258-
b = b[n:]
246+
// Generate the raw descriptor as a kind-of readable const string.
247+
// To not generate a single potentially very long line, we use the 0x0a
248+
// byte to split the string into multiple "lines" and concatenate
249+
// them with "+".
250+
// The 0x0a comes from the observation that the FileDescriptorProto,
251+
// and many of the messages it includes (for example
252+
// DescriptorProto, EnumDescriptorProto, etc.), define a string
253+
// (which is LEN encoded) as field with field_number=1.
254+
// That makes all these messages start with (1<<3 + 2[:LEN])=0x0a
255+
// in the wire-format.
256+
// See also https://protobuf.dev/programming-guides/encoding/#structure.
257+
fmt.Fprint(g, "const ", rawDescVarName(f), `=""`)
258+
for _, line := range bytes.SplitAfter(b, []byte{'\x0a'}) {
259+
g.P("+")
260+
fmt.Fprintf(g, "%q", line)
259261
}
260-
g.P("})")
261262
g.P()
262263

263264
if f.needRawDesc {

cmd/protoc-gen-go/testdata/annotations/annotations.pb.go

+7-21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/protoc-gen-go/testdata/comments/comments.pb.go

+16-30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/protoc-gen-go/testdata/comments/deprecated.pb.go

+8-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/protoc-gen-go/testdata/enumprefix/enumprefix.pb.go

+17-28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/protoc-gen-go/testdata/extensions/base/base.pb.go

+7-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)