Browse Source

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>
maint
Kjetil Barvik 16 years ago committed by Junio C Hamano
parent
commit
60b458b7d3
  1. 44
      symlinks.c

44
symlinks.c

@ -25,27 +25,30 @@ static inline int longest_match_lstat_cache(int len, const char *name,
} }
i++; i++;
} }
/* Is the cached path string a substring of 'name'? */ /*
if (i == cache.len && cache.len < len && name[cache.len] == '/') { * Is the cached path string a substring of 'name', is 'name'
match_len_prev = match_len; * a substring of the cached path string, or is 'name' and the
match_len = cache.len; * cached path string the exact same string?
/* Is 'name' a substring of the cached path string? */ */
} else if ((i == len && len < cache.len && cache.path[len] == '/') || if (i >= max_len && ((len > cache.len && name[cache.len] == '/') ||
(i == len && len == cache.len)) { (len < cache.len && cache.path[len] == '/') ||
(len == cache.len))) {
match_len_prev = match_len; match_len_prev = match_len;
match_len = len; match_len = i;
} }
*previous_slash = match_len_prev; *previous_slash = match_len_prev;
return match_len; return match_len;
} }


static inline void reset_lstat_cache(int track_flags, int prefix_len_stat_func) static inline void reset_lstat_cache(void)
{ {
cache.path[0] = '\0'; cache.path[0] = '\0';
cache.len = 0; cache.len = 0;
cache.flags = 0; cache.flags = 0;
cache.track_flags = track_flags; /*
cache.prefix_len_stat_func = prefix_len_stat_func; * The track_flags and prefix_len_stat_func members is only
* set by the safeguard rule inside lstat_cache()
*/
} }


#define FL_DIR (1 << 0) #define FL_DIR (1 << 0)
@ -77,11 +80,13 @@ static int lstat_cache(int len, const char *name,
if (cache.track_flags != track_flags || if (cache.track_flags != track_flags ||
cache.prefix_len_stat_func != prefix_len_stat_func) { cache.prefix_len_stat_func != prefix_len_stat_func) {
/* /*
* As a safeguard we clear the cache if the values of * As a safeguard rule we clear the cache if the
* track_flags and/or prefix_len_stat_func does not * values of track_flags and/or prefix_len_stat_func
* match with the last supplied values. * does not match with the last supplied values.
*/ */
reset_lstat_cache(track_flags, prefix_len_stat_func); reset_lstat_cache();
cache.track_flags = track_flags;
cache.prefix_len_stat_func = prefix_len_stat_func;
match_len = last_slash = 0; match_len = last_slash = 0;
} else { } else {
/* /*
@ -153,7 +158,7 @@ static int lstat_cache(int len, const char *name,
cache.path[last_slash] = '\0'; cache.path[last_slash] = '\0';
cache.len = last_slash; cache.len = last_slash;
cache.flags = save_flags; cache.flags = save_flags;
} else if (track_flags & FL_DIR && } else if ((track_flags & FL_DIR) &&
last_slash_dir > 0 && last_slash_dir <= PATH_MAX) { last_slash_dir > 0 && last_slash_dir <= PATH_MAX) {
/* /*
* We have a separate test for the directory case, * We have a separate test for the directory case,
@ -170,7 +175,7 @@ static int lstat_cache(int len, const char *name,
cache.len = last_slash_dir; cache.len = last_slash_dir;
cache.flags = FL_DIR; cache.flags = FL_DIR;
} else { } else {
reset_lstat_cache(track_flags, prefix_len_stat_func); reset_lstat_cache();
} }
return ret_flags; return ret_flags;
} }
@ -190,8 +195,7 @@ void invalidate_lstat_cache(int len, const char *name)
cache.len = previous_slash; cache.len = previous_slash;
cache.flags = FL_DIR; cache.flags = FL_DIR;
} else } else
reset_lstat_cache(cache.track_flags, reset_lstat_cache();
cache.prefix_len_stat_func);
} }
} }


@ -200,7 +204,7 @@ void invalidate_lstat_cache(int len, const char *name)
*/ */
void clear_lstat_cache(void) void clear_lstat_cache(void)
{ {
reset_lstat_cache(0, 0); reset_lstat_cache();
} }


#define USE_ONLY_LSTAT 0 #define USE_ONLY_LSTAT 0

Loading…
Cancel
Save