From 79597962690714abcbeba34b477d2191f431138f Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 6 Jul 2019 22:12:24 +0200 Subject: [PATCH] filter: Special case for absolute paths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit name old time/op new time/op delta FilterPatterns/Relative-4 23.6ms ±20% 22.7ms ± 5% ~ (p=0.684 n=10+10) FilterPatterns/Absolute-4 32.3ms ± 8% 14.2ms ±13% -56.01% (p=0.000 n=10+10) FilterPatterns/Wildcard-4 334ms ±17% 266ms ±16% -20.56% (p=0.000 n=10+10) FilterPatterns/ManyNoMatch-4 709ms ± 7% 554ms ± 6% -21.89% (p=0.000 n=10+10) name old alloc/op new alloc/op delta FilterPatterns/Relative-4 3.57MB ± 0% 3.57MB ± 0% +0.00% (p=0.046 n=9+10) FilterPatterns/Absolute-4 3.57MB ± 0% 3.57MB ± 0% ~ (p=0.464 n=10+10) FilterPatterns/Wildcard-4 141MB ± 0% 141MB ± 0% ~ (p=0.163 n=9+10) FilterPatterns/ManyNoMatch-4 3.57MB ± 0% 3.57MB ± 0% ~ (all equal) name old allocs/op new allocs/op delta FilterPatterns/Relative-4 22.2k ± 0% 22.2k ± 0% ~ (all equal) FilterPatterns/Absolute-4 22.2k ± 0% 22.2k ± 0% ~ (all equal) FilterPatterns/Wildcard-4 1.63M ± 0% 1.63M ± 0% ~ (p=0.072 n=10+10) FilterPatterns/ManyNoMatch-4 22.2k ± 0% 22.2k ± 0% ~ (all equal) --- internal/filter/filter.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/internal/filter/filter.go b/internal/filter/filter.go index 67aa70f3e..cb4fb1702 100644 --- a/internal/filter/filter.go +++ b/internal/filter/filter.go @@ -149,8 +149,13 @@ func match(patterns Pattern, strs []string) (matched bool, err error) { } if len(patterns) <= len(strs) { + maxOffset := len(strs) - len(patterns) + // special case absolute patterns + if patterns[0] == "" { + maxOffset = 0 + } outer: - for offset := len(strs) - len(patterns); offset >= 0; offset-- { + for offset := maxOffset; offset >= 0; offset-- { for i := len(patterns) - 1; i >= 0; i-- { ok, err := filepath.Match(patterns[i], strs[offset+i])