reflect/protoreflect: optimize Name.IsValid and FullName.IsValid
For simplicity, IsValid was implemented in terms of regular expressions,
which are useful for verifying a string according to a strict grammar,
but performs poorly. Implement the check in terms of hand-written code,
which provides a 20x improvement to validate the name "google.protobuf.Any".
name old time/op new time/op delta
FullNameIsValid-8 683ns ± 2% 35ns ± 1% -94.86% (p=0.000 n=10+10)
Change-Id: I980403befca0b72cea22acd274064a46cb02644b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/238002
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/reflect/protoreflect/proto_test.go b/reflect/protoreflect/proto_test.go
index a62a004..97e55cc 100644
--- a/reflect/protoreflect/proto_test.go
+++ b/reflect/protoreflect/proto_test.go
@@ -72,3 +72,11 @@
}
}
}
+
+var sink bool
+
+func BenchmarkFullNameIsValid(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ sink = FullName("google.protobuf.Any").IsValid()
+ }
+}