wildmatch: advance faster in <asterisk> + <literal> patterns
Normally when we match "*X" on "abcX", we call dowild("X", "abcX"), dowild("X", "bcX"), dowild("X", "cX") and dowild("X", "X"). Only the last call may have a chance of matching. By skipping the text before "X", we can eliminate the first three useless calls. compat, '*/*/*' on linux-2.6.git file list 2000 times, before: wildmatch 7s 985049us fnmatch 2s 735541us or 34.26% faster and after: wildmatch 4s 492549us fnmatch 0s 888263us or 19.77% slower Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Jan 1, 2013 at 09:44 UTC
6f1a31f0aae6dbd5414b81fb6c0f4ff087146fc5
2 files changed
+31
t/t3070-wildmatch.sh
+8
@@ -207,6 +207,11 @@ match 0 x foo '*/*/*'
207
match 0 x foo/bar '*/*/*'
208
match 1 x foo/bba/arr '*/*/*'
209
match 0 x foo/bb/aa/rr '*/*/*'
210
+match 1 x foo/bb/aa/rr '**/**/**'
211
+match 1 x abcXdefXghi '*X*i'
212
+match 0 x ab/cXd/efXg/hi '*X*i'
213
+match 1 x ab/cXd/efXg/hi '*/*X*/*/*i'
214
+match 1 x ab/cXd/efXg/hi '**/*X*/**/*i'
215
216
pathmatch 1 foo foo
217
pathmatch 0 foo fo
@@ -226,5 +231,8 @@ pathmatch 0 foo '*/*/*'
231
pathmatch 0 foo/bar '*/*/*'
232
pathmatch 1 foo/bba/arr '*/*/*'
233
pathmatch 1 foo/bb/aa/rr '*/*/*'
234
+pathmatch 1 abcXdefXghi '*X*i'
235
+pathmatch 1 ab/cXd/efXg/hi '*/*X*/*/*i'
236
+pathmatch 1 ab/cXd/efXg/hi '*Xg*i'
237
238
test_done
wildmatch.c
+23
@@ -133,6 +133,29 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
133
while (1) {
134
if (t_ch == '\0')
135
break;
136
+ /*
137
+ * Try to advance faster when an asterisk is
138
+ * followed by a literal. We know in this case
139
+ * that the the string before the literal
140
+ * must belong to "*".
141
+ * If match_slash is false, do not look past
142
+ * the first slash as it cannot belong to '*'.
143
+ */
144
+ if (!is_glob_special(*p)) {
145
+ p_ch = *p;
146
+ if ((flags & WM_CASEFOLD) && ISUPPER(p_ch))
147
+ p_ch = tolower(p_ch);
148
+ while ((t_ch = *text) != '\0' &&
149
+ (match_slash || t_ch != '/')) {
150
+ if ((flags & WM_CASEFOLD) && ISUPPER(t_ch))
151
+ t_ch = tolower(t_ch);
152
+ if (t_ch == p_ch)
153
+ break;
154
+ text++;
155
+ }
156
+ if (t_ch != p_ch)
157
+ return WM_NOMATCH;
158
+ }
159
if ((matched = dowild(p, text, flags)) != WM_NOMATCH) {
160
if (!match_slash || matched != WM_ABORT_TO_STARSTAR)
161
return matched;