lstat_cache(): small cleanup and optimisation
Simplify the if-else test in longest_match_lstat_cache() such that we only have one simple if test. Instead of testing for 'i == cache.len' or 'i == len', we transform this to a common test for 'i == max_len'. And to further optimise we use 'i >= max_len' instead of 'i == max_len', the reason is that it is now the exact opposite of one part inside the while-loop termination expression 'i < max_len && name[i] == cache.path[i]', and then the compiler can probably reuse a test instruction from it. We also throw away the arguments to reset_lstat_cache(), such that all the safeguard logic inside lstat_cache() is handled at one place. Signed-off-by: Kjetil Barvik <barvik@broadpark.no> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Kjetil Barvik committed
Feb 9, 2009 at 21:54 UTC
60b458b7d31ff2497ed90cbe9f65444d84882cec
1 file changed
+24
-20
symlinks.c
+24
-20
@@ -25,27 +25,30 @@ static inline int longest_match_lstat_cache(int len, const char *name,
25
}
26
i++;
27
}
28
- /* Is the cached path string a substring of 'name'? */
29
- if (i == cache.len && cache.len < len && name[cache.len] == '/') {
30
- match_len_prev = match_len;
31
- match_len = cache.len;
32
- /* Is 'name' a substring of the cached path string? */
33
- } else if ((i == len && len < cache.len && cache.path[len] == '/') ||
34
- (i == len && len == cache.len)) {
28
+ /*
29
+ * Is the cached path string a substring of 'name', is 'name'
30
+ * a substring of the cached path string, or is 'name' and the
31
+ * cached path string the exact same string?
32
+ */
33
+ if (i >= max_len && ((len > cache.len && name[cache.len] == '/') ||
34
+ (len < cache.len && cache.path[len] == '/') ||
35
+ (len == cache.len))) {
36
match_len_prev = match_len;
36
- match_len = len;
37
+ match_len = i;
38
}
39
*previous_slash = match_len_prev;
40
return match_len;
41
}
42
42
-static inline void reset_lstat_cache(int track_flags, int prefix_len_stat_func)
43
+static inline void reset_lstat_cache(void)
44
{
45
cache.path[0] = '\0';
46
cache.len = 0;
47
cache.flags = 0;
47
- cache.track_flags = track_flags;
48
- cache.prefix_len_stat_func = prefix_len_stat_func;
48
+ /*
49
+ * The track_flags and prefix_len_stat_func members is only
50
+ * set by the safeguard rule inside lstat_cache()
51
+ */
52
}
53
54
#define FL_DIR (1 << 0)
@@ -77,11 +80,13 @@ static int lstat_cache(int len, const char *name,
80
if (cache.track_flags != track_flags ||
81
cache.prefix_len_stat_func != prefix_len_stat_func) {
82
/*
80
- * As a safeguard we clear the cache if the values of
81
- * track_flags and/or prefix_len_stat_func does not
82
- * match with the last supplied values.
83
+ * As a safeguard rule we clear the cache if the
84
+ * values of track_flags and/or prefix_len_stat_func
85
+ * does not match with the last supplied values.
86
*/
84
- reset_lstat_cache(track_flags, prefix_len_stat_func);
87
+ reset_lstat_cache();
88
+ cache.track_flags = track_flags;
89
+ cache.prefix_len_stat_func = prefix_len_stat_func;
90
match_len = last_slash = 0;
91
} else {
92
/*
@@ -153,7 +158,7 @@ static int lstat_cache(int len, const char *name,
158
cache.path[last_slash] = '\0';
159
cache.len = last_slash;
160
cache.flags = save_flags;
156
- } else if (track_flags & FL_DIR &&
161
+ } else if ((track_flags & FL_DIR) &&
162
last_slash_dir > 0 && last_slash_dir <= PATH_MAX) {
163
/*
164
* We have a separate test for the directory case,
@@ -170,7 +175,7 @@ static int lstat_cache(int len, const char *name,
175
cache.len = last_slash_dir;
176
cache.flags = FL_DIR;
177
} else {
173
- reset_lstat_cache(track_flags, prefix_len_stat_func);
178
+ reset_lstat_cache();
179
}
180
return ret_flags;
181
}
@@ -190,8 +195,7 @@ void invalidate_lstat_cache(int len, const char *name)
195
cache.len = previous_slash;
196
cache.flags = FL_DIR;
197
} else
193
- reset_lstat_cache(cache.track_flags,
194
- cache.prefix_len_stat_func);
198
+ reset_lstat_cache();
199
}
200
}
201
@@ -200,7 +204,7 @@ void invalidate_lstat_cache(int len, const char *name)
204
*/
205
void clear_lstat_cache(void)
206
{
203
- reset_lstat_cache(0, 0);
207
+ reset_lstat_cache();
208
}
209
210
#define USE_ONLY_LSTAT 0