From 3ac7db80f1936a1251092f3913e2c02848a085c9 Mon Sep 17 00:00:00 2001 From: Cole Faust Date: Thu, 12 Jan 2023 10:36:17 -0800 Subject: [PATCH] Optimize isAncestor Remove string copies. Test: go test (there are existing visibility tests) Change-Id: I575dcd8497527da03f88003ff0805f3d1271983e --- android/visibility.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/android/visibility.go b/android/visibility.go index b20959944..5955133d4 100644 --- a/android/visibility.go +++ b/android/visibility.go @@ -155,7 +155,11 @@ func (r subpackagesRule) matches(m qualifiedModuleName) bool { } func isAncestor(p1 string, p2 string) bool { - return strings.HasPrefix(p2+"/", p1+"/") + // Equivalent to strings.HasPrefix(p2+"/", p1+"/"), but without the string copies + // The check for a trailing slash is so that we don't consider sibling + // directories with common prefixes to be ancestors, e.g. "fooo/bar" should not be + // a descendant of "foo". + return strings.HasPrefix(p2, p1) && (len(p2) == len(p1) || p2[len(p1)] == '/') } func (r subpackagesRule) String() string {