summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Cole Faust <colefaust@google.com> 2023-01-12 10:36:17 -0800
committer Cole Faust <colefaust@google.com> 2023-01-12 11:00:40 -0800
commit3ac7db80f1936a1251092f3913e2c02848a085c9 (patch)
tree875a2e1053b9a1143666301c07abbdbda34604bf
parent2b4bc2fddbf61d6ccb7b54782b425e4c8aba7e08 (diff)
Optimize isAncestor
Remove string copies. Test: go test (there are existing visibility tests) Change-Id: I575dcd8497527da03f88003ff0805f3d1271983e
-rw-r--r--android/visibility.go6
1 files changed, 5 insertions, 1 deletions
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 {