threads / patch / 46236

patch, 3 partswildmatch refactoring

Subject: [PATCH 0/3] wildmatch refactoring

## tl;dr

7 messages between Jun 22, 2017 and Jun 24, 2017. Diffs are folded; open one to read it.

replies: 6people: 2as markdown or json

Ævar Arnfjörð Bjarmason· Jun 22, 2017, 21:38 UTC · lore

The first patch here should be applied, but 2 & 3 trail along as RFCs to show where this is going.

The RFC patches work, but I'm sure there'll be critiques of the interface / other suggestions, so they're being sent as RFC so Junio doesn't need to worry about picking them up / tracking them.

This is prep work for a local series of mine which compiles the wildmatch pattern into JIT-ed PCRE v2 patterns. That may or may not end up going anywhere, but as 2/3 notes just making these changes makes it easier to optimize wildmatch() further down the road.

Ævar Arnfjörð Bjarmason (3):
  wildmatch: remove unused wildopts parameter
  wildmatch: add interface for precompiling wildmatch() patterns
  wildmatch: make use of the interface for precompiling wildmatch()
    patterns
 apply.c                   |  2 +-
 builtin/describe.c        |  4 ++--
 builtin/ls-remote.c       |  2 +-
 builtin/name-rev.c        |  6 +++++-
 builtin/reflog.c          |  2 +-
 builtin/replace.c         |  7 ++++---
 builtin/show-branch.c     |  2 +-
 config.c                  |  8 ++++++--
 diffcore-order.c          |  2 +-
 dir.c                     | 20 +++++++++++++++-----
 ref-filter.c              |  4 ++--
 refs.c                    |  7 ++++---
 revision.c                |  2 +-
 t/helper/test-wildmatch.c |  6 +++---
 wildmatch.c               | 23 +++++++++++++++++++++--
 wildmatch.h               | 13 +++++++++----
 16 files changed, 77 insertions(+), 33 deletions(-)
-- 
2.13.1.611.g7e3b11ae1
Ævar Arnfjörð Bjarmason· Jun 22, 2017, 21:38 UTC · re: Ævar Arnfjörð Bjarmason · lore

[PATCH 1/3] wildmatch: remove unused wildopts parameter

Remove the unused wildopts placeholder struct from being passed to all wildmatch() invocations, or rather remove all the boilerplate NULL parameters.

This parameter was added back in commit 9b3497cab9 ("wildmatch: rename constants and update prototype", 2013-01-01) as a placeholder for future use. Over 4 years later nothing has made use of it, let's just remove it. It can be added in the future if we find some reason to start using such a parameter.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 apply.c                   | 2 +-
 builtin/describe.c        | 4 ++--
 builtin/ls-remote.c       | 2 +-
 builtin/name-rev.c        | 2 +-
 builtin/reflog.c          | 2 +-
 builtin/replace.c         | 2 +-
 builtin/show-branch.c     | 2 +-
 config.c                  | 2 +-
 diffcore-order.c          | 2 +-
 dir.c                     | 8 +++-----
 ref-filter.c              | 4 ++--
 refs.c                    | 2 +-
 revision.c                | 2 +-
 t/helper/test-wildmatch.c | 6 +++---
 wildmatch.c               | 3 +--
 wildmatch.h               | 6 +-----
 16 files changed, 22 insertions(+), 29 deletions(-)
Show changes to 16 files +22 −29

apply.c, builtin/describe.c, builtin/ls-remote.c, builtin/name-rev.c, builtin/reflog.c, builtin/replace.c, builtin/show-branch.c, config.c, diffcore-order.c, dir.c, ref-filter.c, refs.c, revision.c, t/helper/test-wildmatch.c, wildmatch.c, wildmatch.h

diff --git a/apply.c b/apply.c
index 854faa6779..cc3278978a 100644
--- a/apply.c
+++ b/apply.c
@@ -2088,7 +2088,7 @@ static int use_patch(struct apply_state *state, struct patch *p)
 	/* See if it matches any of exclude/include rule */
 	for (i = 0; i < state->limit_by_name.nr; i++) {
 		struct string_list_item *it = &state->limit_by_name.items[i];
-		if (!wildmatch(it->string, pathname, 0, NULL))
+		if (!wildmatch(it->string, pathname, 0))
 			return (it->util != NULL);
 	}
 
diff --git a/builtin/describe.c b/builtin/describe.c
index 893c8789f4..2fe05f5324 100644
--- a/builtin/describe.c
+++ b/builtin/describe.c
@@ -142,7 +142,7 @@ static int get_name(const char *path, const struct object_id *oid, int flag, voi
 			return 0;
 
 		for_each_string_list_item(item, &exclude_patterns) {
-			if (!wildmatch(item->string, path + 10, 0, NULL))
+			if (!wildmatch(item->string, path + 10, 0))
 				return 0;
 		}
 	}
@@ -158,7 +158,7 @@ static int get_name(const char *path, const struct object_id *oid, int flag, voi
 			return 0;
 
 		for_each_string_list_item(item, &patterns) {
-			if (!wildmatch(item->string, path + 10, 0, NULL))
+			if (!wildmatch(item->string, path + 10, 0))
 				break;
 
 			/* If we get here, no pattern matched. */
diff --git a/builtin/ls-remote.c b/builtin/ls-remote.c
index b2d7d5ce68..c4be98ab9e 100644
--- a/builtin/ls-remote.c
+++ b/builtin/ls-remote.c
@@ -24,7 +24,7 @@ static int tail_match(const char **pattern, const char *path)
 
 	pathbuf = xstrfmt("/%s", path);
 	while ((p = *(pattern++)) != NULL) {
-		if (!wildmatch(p, pathbuf, 0, NULL)) {
+		if (!wildmatch(p, pathbuf, 0)) {
 			free(pathbuf);
 			return 1;
 		}
diff --git a/builtin/name-rev.c b/builtin/name-rev.c
index 7fc7e66e85..d21a5002a7 100644
--- a/builtin/name-rev.c
+++ b/builtin/name-rev.c
@@ -129,7 +129,7 @@ static int subpath_matches(const char *path, const char *filter)
 	const char *subpath = path;
 
 	while (subpath) {
-		if (!wildmatch(filter, subpath, 0, NULL))
+		if (!wildmatch(filter, subpath, 0))
 			return subpath - path;
 		subpath = strchr(subpath, '/');
 		if (subpath)
diff --git a/builtin/reflog.c b/builtin/reflog.c
index 920c16dac0..96c82b4f5e 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -485,7 +485,7 @@ static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, int slot, c
 		return; /* both given explicitly -- nothing to tweak */
 
 	for (ent = reflog_expire_cfg; ent; ent = ent->next) {
-		if (!wildmatch(ent->pattern, ref, 0, NULL)) {
+		if (!wildmatch(ent->pattern, ref, 0)) {
 			if (!(slot & EXPIRE_TOTAL))
 				cb->expire_total = ent->expire_total;
 			if (!(slot & EXPIRE_UNREACH))
diff --git a/builtin/replace.c b/builtin/replace.c
index c921bc976f..89262df5a1 100644
--- a/builtin/replace.c
+++ b/builtin/replace.c
@@ -40,7 +40,7 @@ static int show_reference(const char *refname, const struct object_id *oid,
 {
 	struct show_data *data = cb_data;
 
-	if (!wildmatch(data->pattern, refname, 0, NULL)) {
+	if (!wildmatch(data->pattern, refname, 0)) {
 		if (data->format == REPLACE_FORMAT_SHORT)
 			printf("%s\n", refname);
 		else if (data->format == REPLACE_FORMAT_MEDIUM)
diff --git a/builtin/show-branch.c b/builtin/show-branch.c
index 4a6cc6f490..bfce303cf9 100644
--- a/builtin/show-branch.c
+++ b/builtin/show-branch.c
@@ -444,7 +444,7 @@ static int append_matching_ref(const char *refname, const struct object_id *oid,
 			slash--;
 	if (!*tail)
 		return 0;
-	if (wildmatch(match_ref_pattern, tail, 0, NULL))
+	if (wildmatch(match_ref_pattern, tail, 0))
 		return 0;
 	if (starts_with(refname, "refs/heads/"))
 		return append_head_ref(refname, oid, flag, cb_data);
diff --git a/config.c b/config.c
index 34a139c40b..260caf27b8 100644
--- a/config.c
+++ b/config.c
@@ -245,7 +245,7 @@ static int include_by_gitdir(const struct config_options *opts,
 	}
 
 	ret = !wildmatch(pattern.buf + prefix, text.buf + prefix,
-			 icase ? WM_CASEFOLD : 0, NULL);
+			 icase ? WM_CASEFOLD : 0);
 
 	if (!ret && !already_tried_absolute) {
 		/*
diff --git a/diffcore-order.c b/diffcore-order.c
index 1957f822a5..19e73311f9 100644
--- a/diffcore-order.c
+++ b/diffcore-order.c
@@ -67,7 +67,7 @@ static int match_order(const char *path)
 		strbuf_addstr(&p, path);
 		while (p.buf[0]) {
 			char *cp;
-			if (!wildmatch(order[i], p.buf, 0, NULL))
+			if (!wildmatch(order[i], p.buf, 0))
 				return i;
 			cp = strrchr(p.buf, '/');
 			if (!cp)
diff --git a/dir.c b/dir.c
index 1759063817..544056b37a 100644
--- a/dir.c
+++ b/dir.c
@@ -82,13 +82,11 @@ int git_fnmatch(const struct pathspec_item *item,
 	if (item->magic & PATHSPEC_GLOB)
 		return wildmatch(pattern, string,
 				 WM_PATHNAME |
-				 (item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0),
-				 NULL);
+				 (item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0));
 	else
 		/* wildmatch has not learned no FNM_PATHNAME mode yet */
 		return wildmatch(pattern, string,
-				 item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0,
-				 NULL);
+				 item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0);
 }
 
 static int fnmatch_icase_mem(const char *pattern, int patternlen,
@@ -112,7 +110,7 @@ static int fnmatch_icase_mem(const char *pattern, int patternlen,
 
 	if (ignore_case)
 		flags |= WM_CASEFOLD;
-	match_status = wildmatch(use_pat, use_str, flags, NULL);
+	match_status = wildmatch(use_pat, use_str, flags);
 
 	strbuf_release(&pat_buf);
 	strbuf_release(&str_buf);
diff --git a/ref-filter.c b/ref-filter.c
index ab32bc9c31..718def443a 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -1624,7 +1624,7 @@ static int match_pattern(const struct ref_filter *filter, const char *refname)
 	       skip_prefix(refname, "refs/", &refname));
 
 	for (; *patterns; patterns++) {
-		if (!wildmatch(*patterns, refname, flags, NULL))
+		if (!wildmatch(*patterns, refname, flags))
 			return 1;
 	}
 	return 0;
@@ -1655,7 +1655,7 @@ static int match_name_as_path(const struct ref_filter *filter, const char *refna
 		     refname[plen] == '/' ||
 		     p[plen-1] == '/'))
 			return 1;
-		if (!wildmatch(p, refname, WM_PATHNAME, NULL))
+		if (!wildmatch(p, refname, WM_PATHNAME))
 			return 1;
 	}
 	return 0;
diff --git a/refs.c b/refs.c
index f0685c9251..32626a4cd1 100644
--- a/refs.c
+++ b/refs.c
@@ -229,7 +229,7 @@ static int filter_refs(const char *refname, const struct object_id *oid,
 {
 	struct ref_filter *filter = (struct ref_filter *)data;
 
-	if (wildmatch(filter->pattern, refname, 0, NULL))
+	if (wildmatch(filter->pattern, refname, 0))
 		return 0;
 	return filter->fn(refname, oid, flags, filter->cb_data);
 }
diff --git a/revision.c b/revision.c
index 12eb332350..dc075f0e48 100644
--- a/revision.c
+++ b/revision.c
@@ -1142,7 +1142,7 @@ int ref_excluded(struct string_list *ref_excludes, const char *path)
 	if (!ref_excludes)
 		return 0;
 	for_each_string_list_item(item, ref_excludes) {
-		if (!wildmatch(item->string, path, 0, NULL))
+		if (!wildmatch(item->string, path, 0))
 			return 1;
 	}
 	return 0;
diff --git a/t/helper/test-wildmatch.c b/t/helper/test-wildmatch.c
index 52be876fed..921d7b3e7e 100644
--- a/t/helper/test-wildmatch.c
+++ b/t/helper/test-wildmatch.c
@@ -11,11 +11,11 @@ int cmd_main(int argc, const char **argv)
 			argv[i] += 3;
 	}
 	if (!strcmp(argv[1], "wildmatch"))
-		return !!wildmatch(argv[3], argv[2], WM_PATHNAME, NULL);
+		return !!wildmatch(argv[3], argv[2], WM_PATHNAME);
 	else if (!strcmp(argv[1], "iwildmatch"))
-		return !!wildmatch(argv[3], argv[2], WM_PATHNAME | WM_CASEFOLD, NULL);
+		return !!wildmatch(argv[3], argv[2], WM_PATHNAME | WM_CASEFOLD);
 	else if (!strcmp(argv[1], "pathmatch"))
-		return !!wildmatch(argv[3], argv[2], 0, NULL);
+		return !!wildmatch(argv[3], argv[2], 0);
 	else
 		return 1;
 }
diff --git a/wildmatch.c b/wildmatch.c
index 57c8765805..d074c1be10 100644
--- a/wildmatch.c
+++ b/wildmatch.c
@@ -272,8 +272,7 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 }
 
 /* Match the "pattern" against the "text" string. */
-int wildmatch(const char *pattern, const char *text,
-	      unsigned int flags, struct wildopts *wo)
+int wildmatch(const char *pattern, const char *text, unsigned int flags)
 {
 	return dowild((const uchar*)pattern, (const uchar*)text, flags);
 }
diff --git a/wildmatch.h b/wildmatch.h
index 4090c8f4bb..b8c826aa68 100644
--- a/wildmatch.h
+++ b/wildmatch.h
@@ -10,9 +10,5 @@
 #define WM_ABORT_ALL -1
 #define WM_ABORT_TO_STARSTAR -2
 
-struct wildopts;
-
-int wildmatch(const char *pattern, const char *text,
-	      unsigned int flags,
-	      struct wildopts *wo);
+int wildmatch(const char *pattern, const char *text, unsigned int flags);
 #endif
-- 
2.13.1.611.g7e3b11ae1
Ævar Arnfjörð Bjarmason· Jun 22, 2017, 21:38 UTC · re: Ævar Arnfjörð Bjarmason · lore

[RFC/PATCH 2/3] wildmatch: add interface for precompiling wildmatch() patterns

Add the scaffolding necessary for precompiling wildmatch() patterns.

There is currently no point in doing this with the wildmatch() function we have now, since it can't make any use of precompiling the pattern.

But adding this interface and making use of it will make it easy to refactor the wildmatch() function to e.g. parse the pattern into opcode as the BSD glob() implementation does, or to drop an alternate wildmatch() backend in which trades parsing slowness for faster matching.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 wildmatch.c | 20 ++++++++++++++++++++
 wildmatch.h |  9 +++++++++
 2 files changed, 29 insertions(+)
Show changes to 2 files +29 −0

wildmatch.c, wildmatch.h

diff --git a/wildmatch.c b/wildmatch.c
index d074c1be10..ba6a92a393 100644
--- a/wildmatch.c
+++ b/wildmatch.c
@@ -276,3 +276,23 @@ int wildmatch(const char *pattern, const char *text, unsigned int flags)
 {
 	return dowild((const uchar*)pattern, (const uchar*)text, flags);
 }
+
+struct wildmatch_compiled *wildmatch_compile(const char *pattern, unsigned int flags)
+{
+	struct wildmatch_compiled *code = xmalloc(sizeof(struct wildmatch_compiled));
+	code->pattern = xstrdup(pattern);
+	code->flags = flags;
+
+	return code;
+}
+
+int wildmatch_match(struct wildmatch_compiled *code, const char *text)
+{
+	return wildmatch(code->pattern, text, code->flags);
+}
+
+void wildmatch_free(struct wildmatch_compiled *code)
+{
+	free((void *)code->pattern);
+	free(code);
+}
diff --git a/wildmatch.h b/wildmatch.h
index b8c826aa68..6156d46a33 100644
--- a/wildmatch.h
+++ b/wildmatch.h
@@ -10,5 +10,14 @@
 #define WM_ABORT_ALL -1
 #define WM_ABORT_TO_STARSTAR -2
 
+struct wildmatch_compiled {
+	const char *pattern;
+	unsigned int flags;
+};
+
 int wildmatch(const char *pattern, const char *text, unsigned int flags);
+struct wildmatch_compiled *wildmatch_compile(const char *pattern, unsigned int flags);
+int wildmatch_match(struct wildmatch_compiled *code, const char *text);
+void wildmatch_free(struct wildmatch_compiled *code);
+
 #endif
-- 
2.13.1.611.g7e3b11ae1
Junio C Hamano· Jun 24, 2017, 01:39 UTC · re: Ævar Arnfjörð Bjarmason · lore

Re: [RFC/PATCH 2/3] wildmatch: add interface for precompiling wildmatch() patterns

Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:
Show 13 quoted lines
> +struct wildmatch_compiled *wildmatch_compile(const char *pattern, unsigned int flags)
> +{
> +	struct wildmatch_compiled *code = xmalloc(sizeof(struct wildmatch_compiled));
> +	code->pattern = xstrdup(pattern);
> +	code->flags = flags;
> +
> +	return code;
> +}
> +
> +int wildmatch_match(struct wildmatch_compiled *code, const char *text)
> +{
> +	return wildmatch(code->pattern, text, code->flags);
> +}

Is the far-in-the-future vision to make this the other way around? That is, this being scaffolding, wildmatch_match() which is supposed to be precompiled match actually uses wildmatch() as its underlying engine, but when a viable compilation machinery is plugged in, the wildmatch_match() that takes a precompiled pattern will call into the machinery to execute the compiled pattern, and wildmatch() will be reimplemented as "compile, call wildmatch_match() once and discard" sequence?

Otherwise I'd be worried about wildmatch() vs wildmatch_match() introducing subtle behaviour differences that leads to hard to debug problems.

Ævar Arnfjörð Bjarmason· Jun 24, 2017, 10:59 UTC · re: Junio C Hamano · lore

Re: [RFC/PATCH 2/3] wildmatch: add interface for precompiling wildmatch() patterns

On Sat, Jun 24 2017, Junio C. Hamano jotted:
Show 24 quoted lines
> Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:
>
>> +struct wildmatch_compiled *wildmatch_compile(const char *pattern, unsigned int flags)
>> +{
>> +	struct wildmatch_compiled *code = xmalloc(sizeof(struct wildmatch_compiled));
>> +	code->pattern = xstrdup(pattern);
>> +	code->flags = flags;
>> +
>> +	return code;
>> +}
>> +
>> +int wildmatch_match(struct wildmatch_compiled *code, const char *text)
>> +{
>> +	return wildmatch(code->pattern, text, code->flags);
>> +}
>
> Is the far-in-the-future vision to make this the other way around?
> That is, this being scaffolding, wildmatch_match() which is supposed
> to be precompiled match actually uses wildmatch() as its underlying
> engine, but when a viable compilation machinery is plugged in, the
> wildmatch_match() that takes a precompiled pattern will call into
> the machinery to execute the compiled pattern, and wildmatch() will
> be reimplemented as "compile, call wildmatch_match() once and
> discard" sequence?

Exactly there would be no functional difference in the results, only fixed overhead.

wildmatch() would be the one-off lazy interface and wildmatch_{compile,match,free}(), just like how you can have a wrapper function that calls regcomp() followed by regexec() & regfree(), but it's better to structure your code so you're not compiling & freeing the pattern all the time.

Right now of course there's no difference in the behavior, and a trivially more overhead with the extra xstrdup() & free(), but I wanted to split up the discussion of the semantics of the interface from any actual behavior change in wildmatch() which would make use of it further down the line.

> Otherwise I'd be worried about wildmatch() vs wildmatch_match()
> introducing subtle behaviour differences that leads to hard to debug
> problems.
Junio C Hamano· Jun 24, 2017, 18:13 UTC · re: Ævar Arnfjörð Bjarmason · lore

Re: [RFC/PATCH 2/3] wildmatch: add interface for precompiling wildmatch() patterns

Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:
Show 13 quoted lines
> On Sat, Jun 24 2017, Junio C. Hamano jotted:
>
>> Is the far-in-the-future vision to make this the other way around?
>> That is, this being scaffolding, wildmatch_match() which is supposed
>> to be precompiled match actually uses wildmatch() as its underlying
>> engine, but when a viable compilation machinery is plugged in, the
>> wildmatch_match() that takes a precompiled pattern will call into
>> the machinery to execute the compiled pattern, and wildmatch() will
>> be reimplemented as "compile, call wildmatch_match() once and
>> discard" sequence?
>
> Exactly there would be no functional difference in the results, only
> fixed overhead.
OK, good!
Thanks.
Ævar Arnfjörð Bjarmason· Jun 22, 2017, 21:38 UTC · re: Ævar Arnfjörð Bjarmason · lore

[RFC/PATCH 3/3] wildmatch: make use of the interface for precompiling wildmatch() patterns

Make use of the batch wildmatch() interface. As noted in the comment here the main hot codepath is not being touched, but some other invocations where we repeatedly match the same glob against multiple strings have been migrated.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/name-rev.c |  6 +++++-
 builtin/replace.c  |  7 ++++---
 config.c           |  8 ++++++--
 dir.c              | 12 ++++++++++++
 refs.c             |  7 ++++---
 5 files changed, 31 insertions(+), 9 deletions(-)
Show changes to 5 files +31 −9

builtin/name-rev.c, builtin/replace.c, config.c, dir.c, refs.c

diff --git a/builtin/name-rev.c b/builtin/name-rev.c
index d21a5002a7..a8f8010e1e 100644
--- a/builtin/name-rev.c
+++ b/builtin/name-rev.c
@@ -127,14 +127,18 @@ static void name_rev(struct commit *commit,
 static int subpath_matches(const char *path, const char *filter)
 {
 	const char *subpath = path;
+	struct wildmatch_compiled *code = wildmatch_compile(filter, 0);
 
 	while (subpath) {
-		if (!wildmatch(filter, subpath, 0))
+		if (!wildmatch_match(code, subpath)) {
+			wildmatch_free(code);
 			return subpath - path;
+		}
 		subpath = strchr(subpath, '/');
 		if (subpath)
 			subpath++;
 	}
+	wildmatch_free(code);
 	return -1;
 }
 
diff --git a/builtin/replace.c b/builtin/replace.c
index 89262df5a1..70e7af72ce 100644
--- a/builtin/replace.c
+++ b/builtin/replace.c
@@ -31,7 +31,7 @@ enum replace_format {
 };
 
 struct show_data {
-	const char *pattern;
+	struct wildmatch_compiled *code;
 	enum replace_format format;
 };
 
@@ -40,7 +40,7 @@ static int show_reference(const char *refname, const struct object_id *oid,
 {
 	struct show_data *data = cb_data;
 
-	if (!wildmatch(data->pattern, refname, 0)) {
+	if (!wildmatch_match(data->code, refname)) {
 		if (data->format == REPLACE_FORMAT_SHORT)
 			printf("%s\n", refname);
 		else if (data->format == REPLACE_FORMAT_MEDIUM)
@@ -69,7 +69,7 @@ static int list_replace_refs(const char *pattern, const char *format)
 
 	if (pattern == NULL)
 		pattern = "*";
-	data.pattern = pattern;
+	data.code = wildmatch_compile(pattern, 0);
 
 	if (format == NULL || *format == '\0' || !strcmp(format, "short"))
 		data.format = REPLACE_FORMAT_SHORT;
@@ -83,6 +83,7 @@ static int list_replace_refs(const char *pattern, const char *format)
 		    format);
 
 	for_each_replace_ref(show_reference, (void *)&data);
+	wildmatch_free(data.code);
 
 	return 0;
 }
diff --git a/config.c b/config.c
index 260caf27b8..2ec45e9790 100644
--- a/config.c
+++ b/config.c
@@ -215,6 +215,7 @@ static int include_by_gitdir(const struct config_options *opts,
 	int ret = 0, prefix;
 	const char *git_dir;
 	int already_tried_absolute = 0;
+	struct wildmatch_compiled *code = NULL;
 
 	if (opts->git_dir)
 		git_dir = opts->git_dir;
@@ -244,8 +245,10 @@ static int include_by_gitdir(const struct config_options *opts,
 			goto done;
 	}
 
-	ret = !wildmatch(pattern.buf + prefix, text.buf + prefix,
-			 icase ? WM_CASEFOLD : 0);
+	if (!code)
+		code = wildmatch_compile(pattern.buf + prefix,
+					 icase ? WM_CASEFOLD : 0);
+	ret = !wildmatch_match(code, text.buf + prefix);
 
 	if (!ret && !already_tried_absolute) {
 		/*
@@ -264,6 +267,7 @@ static int include_by_gitdir(const struct config_options *opts,
 done:
 	strbuf_release(&pattern);
 	strbuf_release(&text);
+	wildmatch_free(code);
 	return ret;
 }
 
diff --git a/dir.c b/dir.c
index 544056b37a..f4b5b93956 100644
--- a/dir.c
+++ b/dir.c
@@ -79,6 +79,18 @@ int git_fnmatch(const struct pathspec_item *item,
 			ps_strcmp(item, pattern,
 				  string + string_len - pattern_len);
 	}
+
+	/*
+	 * TODO: This is the main hot path, but untangling this whole
+	 * munging of the prefix is a PITA. We take e.g. the pattern
+	 * "t/**.sh" and then conclude that there's a directory "t",
+	 * and then match its entries (recursively) against "**.sh".
+	 *
+	 * We should try to just always match the full glob against
+	 * the full pattern. See my "BUG: wildmatches ... don't match
+	 * properly due to internal optimizations" on the mailing list
+	 * (<CACBZZX5u5fF4fJBJ3CwH0DmLBw4D32jN5o=Om-iqwQdPG93DFg@mail.gmail.com>).
+	 */
 	if (item->magic & PATHSPEC_GLOB)
 		return wildmatch(pattern, string,
 				 WM_PATHNAME |
diff --git a/refs.c b/refs.c
index 32626a4cd1..ce83b3fccf 100644
--- a/refs.c
+++ b/refs.c
@@ -194,7 +194,7 @@ char *resolve_refdup(const char *refname, int resolve_flags,
 
 /* The argument to filter_refs */
 struct ref_filter {
-	const char *pattern;
+	struct wildmatch_compiled *code;
 	each_ref_fn *fn;
 	void *cb_data;
 };
@@ -229,7 +229,7 @@ static int filter_refs(const char *refname, const struct object_id *oid,
 {
 	struct ref_filter *filter = (struct ref_filter *)data;
 
-	if (wildmatch(filter->pattern, refname, 0))
+	if (wildmatch_match(filter->code, refname))
 		return 0;
 	return filter->fn(refname, oid, flags, filter->cb_data);
 }
@@ -390,12 +390,13 @@ int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
 		strbuf_addch(&real_pattern, '*');
 	}
 
-	filter.pattern = real_pattern.buf;
+	filter.code = wildmatch_compile(real_pattern.buf, 0);
 	filter.fn = fn;
 	filter.cb_data = cb_data;
 	ret = for_each_ref(filter_refs, &filter);
 
 	strbuf_release(&real_pattern);
+	wildmatch_free(filter.code);
 	return ret;
 }
 
-- 
2.13.1.611.g7e3b11ae1

← back to recent threads