Skip to content

Commit

Permalink
internal_gengo: use unsafe.StringData() to avoid a descriptor copy
Browse files Browse the repository at this point in the history
This means our generated code requires Go 1.20+.
(Go Protobuf currently requires at least Go 1.21.)

Change-Id: Ie65be553bcb5912991d590104ff6b7c6a82b9c38
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/642055
Reviewed-by: Nicolas Hillegeer <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Damien Neil <[email protected]>
  • Loading branch information
stapelberg committed Jan 15, 2025
1 parent cc8d1c2 commit 0c3cc2f
Show file tree
Hide file tree
Showing 147 changed files with 296 additions and 146 deletions.
1 change: 1 addition & 0 deletions cmd/protoc-gen-go/internal_gengo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const (
syncPackage = protogen.GoImportPath("sync")
timePackage = protogen.GoImportPath("time")
utf8Package = protogen.GoImportPath("unicode/utf8")
unsafePackage = protogen.GoImportPath("unsafe")
)

// Protobuf library dependencies.
Expand Down
7 changes: 6 additions & 1 deletion cmd/protoc-gen-go/internal_gengo/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,12 @@ func genReflectFileDescriptor(gen *protogen.Plugin, g *protogen.GeneratedFile, f
g.P("out := ", protoimplPackage.Ident("TypeBuilder"), "{")
g.P("File: ", protoimplPackage.Ident("DescBuilder"), "{")
g.P("GoPackagePath: ", reflectPackage.Ident("TypeOf"), "(x{}).PkgPath(),")
g.P("RawDescriptor: []byte(", rawDescVarName(f), "),")
// Avoid a copy of the descriptor by using an inlined version of
// [strs.UnsafeBytes] (gencode cannot depend on internal/strs).
// This means modification of the RawDescriptor byte slice
// will crash the program. But generated RawDescriptors
// are never supposed to be modified anyway.
g.P("RawDescriptor: ", unsafePackage.Ident("Slice"), "(", unsafePackage.Ident("StringData"), "(", rawDescVarName(f), "), len(", rawDescVarName(f), ")),")
g.P("NumEnums: ", len(f.allEnums), ",")
g.P("NumMessages: ", len(f.allMessages), ",")
g.P("NumExtensions: ", len(f.allExtensions), ",")
Expand Down
3 changes: 2 additions & 1 deletion cmd/protoc-gen-go/testdata/annotations/annotations.pb.go

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

Original file line number Diff line number Diff line change
@@ -1 +1 @@
annotation:{path:5 path:0 source_file:"cmd/protoc-gen-go/testdata/annotations/annotations.proto" begin:512 end:531} annotation:{path:5 path:0 path:2 path:0 source_file:"cmd/protoc-gen-go/testdata/annotations/annotations.proto" begin:548 end:595} annotation:{path:4 path:0 source_file:"cmd/protoc-gen-go/testdata/annotations/annotations.proto" begin:1954 end:1976} annotation:{path:4 path:0 path:2 path:0 source_file:"cmd/protoc-gen-go/testdata/annotations/annotations.proto" begin:2053 end:2073} annotation:{path:4 path:0 path:2 path:1 source_file:"cmd/protoc-gen-go/testdata/annotations/annotations.proto" begin:2256 end:2266} annotation:{path:4 path:0 path:2 path:0 source_file:"cmd/protoc-gen-go/testdata/annotations/annotations.proto" begin:3437 end:3460} annotation:{path:4 path:0 path:2 path:1 source_file:"cmd/protoc-gen-go/testdata/annotations/annotations.proto" begin:3603 end:3607} annotation:{path:4 path:0 path:2 path:1 source_file:"cmd/protoc-gen-go/testdata/annotations/annotations.proto" begin:3770 end:3774 semantic:SET}
annotation:{path:5 path:0 source_file:"cmd/protoc-gen-go/testdata/annotations/annotations.proto" begin:529 end:548} annotation:{path:5 path:0 path:2 path:0 source_file:"cmd/protoc-gen-go/testdata/annotations/annotations.proto" begin:565 end:612} annotation:{path:4 path:0 source_file:"cmd/protoc-gen-go/testdata/annotations/annotations.proto" begin:1971 end:1993} annotation:{path:4 path:0 path:2 path:0 source_file:"cmd/protoc-gen-go/testdata/annotations/annotations.proto" begin:2070 end:2090} annotation:{path:4 path:0 path:2 path:1 source_file:"cmd/protoc-gen-go/testdata/annotations/annotations.proto" begin:2273 end:2283} annotation:{path:4 path:0 path:2 path:0 source_file:"cmd/protoc-gen-go/testdata/annotations/annotations.proto" begin:3454 end:3477} annotation:{path:4 path:0 path:2 path:1 source_file:"cmd/protoc-gen-go/testdata/annotations/annotations.proto" begin:3620 end:3624} annotation:{path:4 path:0 path:2 path:1 source_file:"cmd/protoc-gen-go/testdata/annotations/annotations.proto" begin:3787 end:3791 semantic:SET}
3 changes: 2 additions & 1 deletion cmd/protoc-gen-go/testdata/comments/comments.pb.go

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

3 changes: 2 additions & 1 deletion cmd/protoc-gen-go/testdata/comments/deprecated.pb.go

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

3 changes: 2 additions & 1 deletion cmd/protoc-gen-go/testdata/enumprefix/enumprefix.pb.go

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

3 changes: 2 additions & 1 deletion cmd/protoc-gen-go/testdata/extensions/base/base.pb.go

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

3 changes: 2 additions & 1 deletion cmd/protoc-gen-go/testdata/extensions/ext/ext.pb.go

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

3 changes: 2 additions & 1 deletion cmd/protoc-gen-go/testdata/extensions/extra/extra.pb.go

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

3 changes: 2 additions & 1 deletion cmd/protoc-gen-go/testdata/extensions/proto3/ext3.pb.go

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

3 changes: 2 additions & 1 deletion cmd/protoc-gen-go/testdata/fieldnames/fieldnames.pb.go

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

3 changes: 2 additions & 1 deletion cmd/protoc-gen-go/testdata/import_public/a.pb.go

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

3 changes: 2 additions & 1 deletion cmd/protoc-gen-go/testdata/import_public/b.pb.go

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

3 changes: 2 additions & 1 deletion cmd/protoc-gen-go/testdata/import_public/c.pb.go

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

3 changes: 2 additions & 1 deletion cmd/protoc-gen-go/testdata/import_public/sub/a.pb.go

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

3 changes: 2 additions & 1 deletion cmd/protoc-gen-go/testdata/import_public/sub/b.pb.go

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

3 changes: 2 additions & 1 deletion cmd/protoc-gen-go/testdata/import_public/sub2/a.pb.go

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

3 changes: 2 additions & 1 deletion cmd/protoc-gen-go/testdata/imports/fmt/m.pb.go

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

3 changes: 2 additions & 1 deletion cmd/protoc-gen-go/testdata/imports/test_a_1/m1.pb.go

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

3 changes: 2 additions & 1 deletion cmd/protoc-gen-go/testdata/imports/test_a_1/m2.pb.go

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

3 changes: 2 additions & 1 deletion cmd/protoc-gen-go/testdata/imports/test_a_2/m3.pb.go

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

3 changes: 2 additions & 1 deletion cmd/protoc-gen-go/testdata/imports/test_a_2/m4.pb.go

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

3 changes: 2 additions & 1 deletion cmd/protoc-gen-go/testdata/imports/test_b_1/m1.pb.go

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

Loading

0 comments on commit 0c3cc2f

Please sign in to comment.