@matheusbsbs / matheus-bagliano / commits / ccc744abbb
Fork 0

tree-diff: avoid strncmp()

If we already know that some of the pathspecs can match later entries in the tree we are looking at, we do not have to do more expensive strncmp() upfront before comparing the length of the match pattern and the path, as a path longer than the match pattern will not match it, and a path shorter than the match pattern will match only if the path is a directory-component wise prefix of the match pattern. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Mar 21, 2007 at 12:34 UTC ccc744abbb27d3766175aee971991bc0f1fd4f00
1 file changed +37 -23
tree-diff.c
+37 -23
@@ -93,7 +93,7 @@ static int tree_entry_interesting(struct tree_desc *desc, const char *base, int
93 for (i = 0; i < opt->nr_paths; i++) {
94 const char *match = opt->paths[i];
95 int matchlen = opt->pathlens[i];
96 - int m;
96 + int m = -1; /* signals that we haven't called strncmp() */
97
98 if (baselen >= matchlen) {
99 /* If it doesn't match, move along... */
@@ -111,29 +111,36 @@ static int tree_entry_interesting(struct tree_desc *desc, const char *base, int
111 match += baselen;
112 matchlen -= baselen;
113
114 - /*
115 - * Does match sort strictly earlier than path with their
116 - * common parts?
117 - */
118 - m = strncmp(match, path,
119 - (matchlen < pathlen) ? matchlen : pathlen);
120 - if (m < 0)
121 - continue;
114 + if (never_interesting) {
115 + /*
116 + * We have not seen any match that sorts later
117 + * than the current path.
118 + */
119 +
120 + /*
121 + * Does match sort strictly earlier than path
122 + * with their common parts?
123 + */
124 + m = strncmp(match, path,
125 + (matchlen < pathlen) ? matchlen : pathlen);
126 + if (m < 0)
127 + continue;
128
123 - /*
124 - * If we come here even once, that means there is at
125 - * least one pathspec that would sort equal to or
126 - * later than the path we are currently looking at.
127 - * In other words, if we have never reached this point
128 - * after iterating all pathspecs, it means all
129 - * pathspecs are either outside of base, or inside the
130 - * base but sorts strictly earlier than the current
131 - * one. In either case, they will never match the
132 - * subsequent entries. In such a case, we initialized
133 - * the variable to -1 and that is what will be
134 - * returned, allowing the caller to terminate early.
135 - */
136 - never_interesting = 0;
129 + /*
130 + * If we come here even once, that means there is at
131 + * least one pathspec that would sort equal to or
132 + * later than the path we are currently looking at.
133 + * In other words, if we have never reached this point
134 + * after iterating all pathspecs, it means all
135 + * pathspecs are either outside of base, or inside the
136 + * base but sorts strictly earlier than the current
137 + * one. In either case, they will never match the
138 + * subsequent entries. In such a case, we initialized
139 + * the variable to -1 and that is what will be
140 + * returned, allowing the caller to terminate early.
141 + */
142 + never_interesting = 0;
143 + }
144
145 if (pathlen > matchlen)
146 continue;
@@ -145,6 +152,13 @@ static int tree_entry_interesting(struct tree_desc *desc, const char *base, int
152 continue;
153 }
154
155 + if (m == -1)
156 + /*
157 + * we cheated and did not do strncmp(), so we do
158 + * that here.
159 + */
160 + m = strncmp(match, path, pathlen);
161 +
162 /*
163 * If common part matched earlier then it is a hit,
164 * because we rejected the case where path is not a