git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v4 2/9] lstat_cache(): generalise longest_match_lstat_cache()

From
KBKjetil Barvik <barvik@broadpark.no>
Date
Feb 9, 2009, 20:54 UTC
Message-ID
<a660c05cd92cd87d7fb08a884b2e5450b34eee45.1234211595.git.barvik@broadpark.no>
In-Reply-To
<cover.1234211594.git.barvik@broadpark.no>

Rename the function to longst_path_match() and generalise it such that it can also be used by other functions.

Signed-off-by: Kjetil Barvik <barvik@broadpark.no>
---
 symlinks.c |   46 ++++++++++++++++++++++++----------------------
 1 files changed, 24 insertions(+), 22 deletions(-)
diff --git a/symlinks.c b/symlinks.c
index ae57e56..4596aee 100644
--- a/symlinks.c
+++ b/symlinks.c
@@ -1,38 +1,30 @@
 #include "cache.h"
 
-static struct cache_def {
-	char path[PATH_MAX + 1];
-	int len;
-	int flags;
-	int track_flags;
-	int prefix_len_stat_func;
-} cache;
-
 /*
  * Returns the length (on a path component basis) of the longest
- * common prefix match of 'name' and the cached path string.
+ * common prefix match of 'name_a' and 'name_b'.
  */
-static inline int longest_match_lstat_cache(int len, const char *name,
-					    int *previous_slash)
+static int longest_path_match(const char *name_a, int len_a,
+			      const char *name_b, int len_b,
+			      int *previous_slash)
 {
 	int max_len, match_len = 0, match_len_prev = 0, i = 0;
 
-	max_len = len < cache.len ? len : cache.len;
-	while (i < max_len && name[i] == cache.path[i]) {
-		if (name[i] == '/') {
+	max_len = len_a < len_b ? len_a : len_b;
+	while (i < max_len && name_a[i] == name_b[i]) {
+		if (name_a[i] == '/') {
 			match_len_prev = match_len;
 			match_len = i;
 		}
 		i++;
 	}
 	/*
-	 * 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?
+	 * Is 'name_b' a substring of 'name_a', the other way around,
+	 * or is 'name_a' and 'name_b' the exact same string?
 	 */
-	if (i >= max_len && ((len > cache.len && name[cache.len] == '/') ||
-			     (len < cache.len && cache.path[len] == '/') ||
-			     (len == cache.len))) {
+	if (i >= max_len && ((len_a > len_b && name_a[len_b] == '/') ||
+			     (len_a < len_b && name_b[len_a] == '/') ||
+			     (len_a == len_b))) {
 		match_len_prev = match_len;
 		match_len = i;
 	}
@@ -40,6 +32,14 @@ static inline int longest_match_lstat_cache(int len, const char *name,
 	return match_len;
 }
 
+static struct cache_def {
+	char path[PATH_MAX + 1];
+	int len;
+	int flags;
+	int track_flags;
+	int prefix_len_stat_func;
+} cache;
+
 static inline void reset_lstat_cache(void)
 {
 	cache.path[0] = '\0';
@@ -94,7 +94,8 @@ static int lstat_cache(int len, const char *name,
 		 * the 2 "excluding" path types.
 		 */
 		match_len = last_slash =
-			longest_match_lstat_cache(len, name, &previous_slash);
+			longest_path_match(name, len, cache.path, cache.len,
+					   &previous_slash);
 		match_flags = cache.flags & track_flags & (FL_NOENT|FL_SYMLINK);
 		if (match_flags && match_len == cache.len)
 			return match_flags;
@@ -188,7 +189,8 @@ void invalidate_lstat_cache(int len, const char *name)
 {
 	int match_len, previous_slash;
 
-	match_len = longest_match_lstat_cache(len, name, &previous_slash);
+	match_len = longest_path_match(name, len, cache.path, cache.len,
+				       &previous_slash);
 	if (len == match_len) {
 		if ((cache.track_flags & FL_DIR) && previous_slash > 0) {
 			cache.path[previous_slash] = '\0';
-- 
1.6.1.349.g99fa5
Previous: Kjetil BarvikNext: Kjetil Barvik
Message 3 of 10 in “git checkout: more cleanups, optimisation, less lstat() calls”
  1. 0/9 git checkout: more cleanups, optimisation, less lstat() callsKjetil Barvik, Feb 9, 2009
  2. 1/9 lstat_cache(): small cleanup and optimisationKjetil Barvik, Feb 9, 2009
  3. 2/9 lstat_cache(): generalise longest_match_lstat_cache()Kjetil Barvik, Feb 9, 2009
  4. 3/9 lstat_cache(): swap func(length, string) into func(string, length)Kjetil Barvik, Feb 9, 2009
  5. 4/9 unlink_entry(): introduce schedule_dir_for_removal()Kjetil Barvik, Feb 9, 2009
  6. 5/9 create_directories(): remove some memcpy() and strchr() callsKjetil Barvik, Feb 9, 2009
  7. 6/9 write_entry(): cleanup of some duplicated codeKjetil Barvik, Feb 9, 2009
  8. 7/9 write_entry(): use fstat() instead of lstat() when file is openKjetil Barvik, Feb 9, 2009
  9. 8/9 show_patch_diff(): remove a call to fstat()Kjetil Barvik, Feb 9, 2009
  10. 9/9 lstat_cache(): print a warning if doing ping-pong between cache typesKjetil Barvik, Feb 9, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.