Skip to content

Commit

Permalink
lstat_cache(): small cleanup and optimisation
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Kjetil Barvik authored and gitster committed Feb 10, 2009
1 parent f6b98e4 commit 60b458b
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions symlinks.c
Expand Up @@ -25,27 +25,30 @@ static inline int longest_match_lstat_cache(int len, const char *name,
}
i++;
}
/* Is the cached path string a substring of 'name'? */
if (i == cache.len && cache.len < len && name[cache.len] == '/') {
match_len_prev = match_len;
match_len = cache.len;
/* Is 'name' a substring of the cached path string? */
} else if ((i == len && len < cache.len && cache.path[len] == '/') ||
(i == len && len == cache.len)) {
/*
* Is the cached path string a substring of 'name', is 'name'
* a substring of the cached path string, or is 'name' and the
* cached path string the exact same string?
*/
if (i >= max_len && ((len > cache.len && name[cache.len] == '/') ||
(len < cache.len && cache.path[len] == '/') ||
(len == cache.len))) {
match_len_prev = match_len;
match_len = len;
match_len = i;
}
*previous_slash = match_len_prev;
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.len = 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)
Expand Down Expand Up @@ -77,11 +80,13 @@ static int lstat_cache(int len, const char *name,
if (cache.track_flags != track_flags ||
cache.prefix_len_stat_func != prefix_len_stat_func) {
/*
* As a safeguard we clear the cache if the values of
* track_flags and/or prefix_len_stat_func does not
* match with the last supplied values.
* As a safeguard rule we clear the cache if the
* values of track_flags and/or prefix_len_stat_func
* 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;
} else {
/*
Expand Down Expand Up @@ -153,7 +158,7 @@ static int lstat_cache(int len, const char *name,
cache.path[last_slash] = '\0';
cache.len = last_slash;
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) {
/*
* We have a separate test for the directory case,
Expand All @@ -170,7 +175,7 @@ static int lstat_cache(int len, const char *name,
cache.len = last_slash_dir;
cache.flags = FL_DIR;
} else {
reset_lstat_cache(track_flags, prefix_len_stat_func);
reset_lstat_cache();
}
return ret_flags;
}
Expand All @@ -190,8 +195,7 @@ void invalidate_lstat_cache(int len, const char *name)
cache.len = previous_slash;
cache.flags = FL_DIR;
} else
reset_lstat_cache(cache.track_flags,
cache.prefix_len_stat_func);
reset_lstat_cache();
}
}

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

#define USE_ONLY_LSTAT 0
Expand Down

0 comments on commit 60b458b

Please sign in to comment.