Skip to content

Commit

Permalink
merged 3fac2a27c94f99f4379551928df388fcb0ad37ce from golang/protobuf.…
Browse files Browse the repository at this point in the history
… ptypes: optimize Is to avoid prefix scan (#425)
  • Loading branch information
jmarais authored and awalterschulze committed Jul 15, 2018
1 parent a2967a4 commit ebc0565
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
10 changes: 6 additions & 4 deletions types/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,12 @@ func UnmarshalAny(any *Any, pb proto.Message) error {

// Is returns true if any value contains a given message type.
func Is(any *Any, pb proto.Message) bool {
aname, err := AnyMessageName(any)
if err != nil {
// The following is equivalent to AnyMessageName(any) == proto.MessageName(pb),
// but it avoids scanning TypeUrl for the slash.
if any == nil {
return false
}

return aname == proto.MessageName(pb)
name := proto.MessageName(pb)
prefix := len(any.TypeUrl) - len(name)
return prefix >= 1 && any.TypeUrl[prefix-1] == '/' && any.TypeUrl[prefix:] == name
}
41 changes: 41 additions & 0 deletions types/any_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,13 @@ func TestIs(t *testing.T) {
t.Fatal(err)
}
if Is(a, &pb.DescriptorProto{}) {
// No spurious match for message names of different length.
t.Error("FileDescriptorProto is not a DescriptorProto, but Is says it is")
}
if Is(a, &pb.EnumDescriptorProto{}) {
// No spurious match for message names of equal length.
t.Error("FileDescriptorProto is not an EnumDescriptorProto, but Is says it is")
}
if !Is(a, &pb.FileDescriptorProto{}) {
t.Error("FileDescriptorProto is indeed a FileDescriptorProto, but Is says it is not")
}
Expand All @@ -74,6 +79,21 @@ func TestIsDifferentUrlPrefixes(t *testing.T) {
}
}

func TestIsCornerCases(t *testing.T) {
m := &pb.FileDescriptorProto{}
if Is(nil, m) {
t.Errorf("message with nil type url incorrectly claimed to be %q", proto.MessageName(m))
}
noPrefix := &Any{TypeUrl: proto.MessageName(m)}
if Is(noPrefix, m) {
t.Errorf("message with type url %q incorrectly claimed to be %q", noPrefix.TypeUrl, proto.MessageName(m))
}
shortPrefix := &Any{TypeUrl: "/" + proto.MessageName(m)}
if !Is(shortPrefix, m) {
t.Errorf("message with type url %q didn't satisfy Is for type %q", shortPrefix.TypeUrl, proto.MessageName(m))
}
}

func TestUnmarshalDynamic(t *testing.T) {
want := &pb.FileDescriptorProto{Name: proto.String("foo")}
a, err := MarshalAny(want)
Expand Down Expand Up @@ -110,3 +130,24 @@ func TestEmpty(t *testing.T) {
t.Errorf("got no error for an attempt to create a message of type %q, which shouldn't be linked in", a.TypeUrl)
}
}

func TestEmptyCornerCases(t *testing.T) {
_, err := EmptyAny(nil)
if err == nil {
t.Error("expected Empty for nil to fail")
}
want := &pb.FileDescriptorProto{}
noPrefix := &Any{TypeUrl: proto.MessageName(want)}
_, err = EmptyAny(noPrefix)
if err == nil {
t.Errorf("expected Empty for any type %q to fail", noPrefix.TypeUrl)
}
shortPrefix := &Any{TypeUrl: "/" + proto.MessageName(want)}
got, err := EmptyAny(shortPrefix)
if err != nil {
t.Errorf("Empty for any type %q failed: %s", shortPrefix.TypeUrl, err)
}
if !proto.Equal(got, want) {
t.Errorf("Empty for any type %q differs, got %q, want %q", shortPrefix.TypeUrl, got, want)
}
}

0 comments on commit ebc0565

Please sign in to comment.