Skip to content

Commit c5779f9

Browse files
committed
proto: use vtprotobuf optimized functions when available
Envoy tracking issue: envoyproxy/go-control-plane#824 This will be a NOP until go-control-plane completes ^. Once that happens, we will get the optimizations for free
1 parent fb858ec commit c5779f9

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

pilot/pkg/util/protoconv/protoconv.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828

2929
// MessageToAnyWithError converts from proto message to proto Any
3030
func MessageToAnyWithError(msg proto.Message) (*anypb.Any, error) {
31-
b, err := proto.MarshalOptions{Deterministic: true}.Marshal(msg)
31+
b, err := marshal(msg)
3232
if err != nil {
3333
return nil, err
3434
}
@@ -39,6 +39,16 @@ func MessageToAnyWithError(msg proto.Message) (*anypb.Any, error) {
3939
}, nil
4040
}
4141

42+
func marshal(msg proto.Message) ([]byte, error) {
43+
if vt, ok := msg.(vtStrictMarshal); ok {
44+
// Attempt to use more efficient implementation
45+
// "Strict" is the equivalent to Deterministic=true below
46+
return vt.MarshalVTStrict()
47+
}
48+
// If not available, fallback to normal implementation
49+
return proto.MarshalOptions{Deterministic: true}.Marshal(msg)
50+
}
51+
4252
// MessageToAny converts from proto message to proto Any
4353
func MessageToAny(msg proto.Message) *anypb.Any {
4454
out, err := MessageToAnyWithError(msg)
@@ -82,3 +92,8 @@ func UnmarshalAny[T any](a *anypb.Any) (*T, error) {
8292
}
8393
return any(dst).(*T), nil
8494
}
95+
96+
// https://github.com/planetscale/vtprotobuf#available-features
97+
type vtStrictMarshal interface {
98+
MarshalVTStrict() ([]byte, error)
99+
}

0 commit comments

Comments
 (0)