فهرست منبع

proto: enable/disable fast path with build tags

Remove the Reflection field from MarshalOptions and UnmarshalOptions.
Disable the fast path and use the reflection-based implementation when
the 'protoreflect' build tag is set.

Change-Id: Ic674e3af67501de27fb03ec2712fbed40eae7fef
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/170896
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
Damien Neil 6 سال پیش
والد
کامیت
cc2b078f98
7فایلهای تغییر یافته به همراه32 افزوده شده و 24 حذف شده
  1. 1 0
      integration_test.go
  2. 0 7
      proto/decode.go
  3. 0 7
      proto/encode.go
  4. 0 8
      proto/proto.go
  5. 17 0
      proto/proto_methods.go
  6. 14 0
      proto/proto_reflect.go
  7. 0 2
      runtime/protoiface/methods.go

+ 1 - 0
integration_test.go

@@ -66,6 +66,7 @@ func Test(t *testing.T) {
 			runGo("Build", workDir, "go", "build", "./...")
 			runGo("TestNormal", workDir, "go", "test", "-race", "./...")
 			runGo("TestPureGo", workDir, "go", "test", "-race", "-tags", "purego", "./...")
+			runGo("TestReflect", workDir, "go", "test", "-race", "-tags", "protoreflect", "./...")
 			if v == golangLatest {
 				runGo("TestProto1Legacy", workDir, "go", "test", "-race", "-tags", "proto1_legacy", "./...")
 				runGo("TestProtocGenGo", "cmd/protoc-gen-go/testdata", "go", "test")

+ 0 - 7
proto/decode.go

@@ -25,10 +25,6 @@ type UnmarshalOptions struct {
 	// If DiscardUnknown is set, unknown fields are ignored.
 	DiscardUnknown bool
 
-	// Reflection forces use of the reflection-based decoder, even for
-	// messages which implement fast-path deserialization.
-	Reflection bool
-
 	pragma.NoUnkeyedLiterals
 }
 
@@ -57,9 +53,6 @@ func (o UnmarshalOptions) Unmarshal(b []byte, m Message) error {
 }
 
 func (o UnmarshalOptions) unmarshalMessageFast(b []byte, m Message) error {
-	if o.Reflection {
-		return errInternalNoFast
-	}
 	methods := protoMethods(m)
 	if methods == nil || methods.Unmarshal == nil {
 		return errInternalNoFast

+ 0 - 7
proto/encode.go

@@ -47,10 +47,6 @@ type MarshalOptions struct {
 	// detail and subject to change.
 	Deterministic bool
 
-	// Reflection forces use of the reflection-based encoder, even for
-	// messages which implement fast-path serialization.
-	Reflection bool
-
 	pragma.NoUnkeyedLiterals
 }
 
@@ -84,9 +80,6 @@ func (o MarshalOptions) MarshalAppend(b []byte, m Message) ([]byte, error) {
 }
 
 func (o MarshalOptions) marshalMessageFast(b []byte, m Message) ([]byte, error) {
-	if o.Reflection {
-		return nil, errInternalNoFast
-	}
 	methods := protoMethods(m)
 	if methods == nil ||
 		methods.MarshalAppend == nil ||

+ 0 - 8
proto/proto.go

@@ -7,7 +7,6 @@ package proto
 import (
 	"github.com/golang/protobuf/v2/internal/errors"
 	"github.com/golang/protobuf/v2/reflect/protoreflect"
-	"github.com/golang/protobuf/v2/runtime/protoiface"
 )
 
 // Message is the top-level interface that all messages must implement.
@@ -15,10 +14,3 @@ type Message = protoreflect.ProtoMessage
 
 // errInternalNoFast indicates that fast-path operations are not available for a message.
 var errInternalNoFast = errors.New("BUG: internal error (errInternalNoFast)")
-
-func protoMethods(m Message) *protoiface.Methods {
-	if x, ok := m.(protoiface.Methoder); ok {
-		return x.XXX_Methods()
-	}
-	return nil
-}

+ 17 - 0
proto/proto_methods.go

@@ -0,0 +1,17 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// The protoreflect build tag disables use of fast-path methods.
+// +build !protoreflect
+
+package proto
+
+import "github.com/golang/protobuf/v2/runtime/protoiface"
+
+func protoMethods(m Message) *protoiface.Methods {
+	if x, ok := m.(protoiface.Methoder); ok {
+		return x.XXX_Methods()
+	}
+	return nil
+}

+ 14 - 0
proto/proto_reflect.go

@@ -0,0 +1,14 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// The protoreflect build tag disables use of fast-path methods.
+// +build protoreflect
+
+package proto
+
+import "github.com/golang/protobuf/v2/runtime/protoiface"
+
+func protoMethods(m Message) *protoiface.Methods {
+	return nil
+}

+ 0 - 2
runtime/protoiface/methods.go

@@ -55,7 +55,6 @@ const (
 type MarshalOptions struct {
 	AllowPartial  bool
 	Deterministic bool
-	Reflection    bool
 
 	pragma.NoUnkeyedLiterals
 }
@@ -66,7 +65,6 @@ type MarshalOptions struct {
 type UnmarshalOptions struct {
 	AllowPartial   bool
 	DiscardUnknown bool
-	Reflection     bool
 
 	pragma.NoUnkeyedLiterals
 }