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

Re: inotify to minimize stat() calls

From
Karsten Blees <karsten.blees@gmail.com>
Date
Feb 14, 2013, 00:48 UTC
Message-ID
<511C3454.6080204@gmail.com>
In-Reply-To
<20130213225529.GA25353@sigill.intra.peff.net>
Am 13.02.2013 23:55, schrieb Jeff King:
Show 18 quoted lines
> On Wed, Feb 13, 2013 at 09:25:59PM +0100, Karsten Blees wrote:
> 
>> Alternatively, we could simply create normal cache_entries for the
>> directories that are linked via ce->next, but have a trailing '/' in
>> their name?
>>
>> Reference counting sounds good to me, at least better than allocating
>> directory entries per cache entry * parent dirs.
> 
> I think that is more or less what my patch does, but it splits the
> ref-counted fake cache_entries out into a separate hash of "struct
> dir_hash_entry" rather than storing it in the regular hash. Which IMHO
> is a bit cleaner for two reasons:
> 
>   1. You do not have to pay the memory price of storing fake
>      cache_entries (the name+refcount struct for each directory is much
>      smaller than a real cache_entry).
> 
Yes, but considering the small number of directories compared to files, I think this is a relatively small price to pay.
>   2. It makes the code a bit simpler, as you do not have to do any
>      "check for trailing /" magic on the result of index_name_exists to
>      determine if it is a "real" name or just a fake dir entry.
> 
True for dir.c. On the other hand, you need a lot of new find / find_or_create logic in name-hash.c.
Just to illustrate what I mean, here's a quick sketch (there's still a segfault somewhere, but I don't have time to debug right now...).
Note that hash_index_entry_directories works from right to left - if the immediate parent directory is there, there's no need to check the parent's parent.
cache_entry.dir points to the parent directory so that we don't need to lookup all path components for reference counting when adding / removing entries.
As directory entries are 'real' cache_entries, we can reuse the existing index_name_exists and hash_index_entry code.
I feel slightly guilty for abusing ce_size as reference counter...well :-)
---
 cache.h     |  4 +++-
 name-hash.c | 80 ++++++++++++++++++++++++++++---------------------------------
 2 files changed, 39 insertions(+), 45 deletions(-)
diff --git a/cache.h b/cache.h
index 665b512..2bc1372 100644
--- a/cache.h
+++ b/cache.h
@@ -131,7 +131,7 @@ struct cache_entry {
 	unsigned int ce_namelen;
 	unsigned char sha1[20];
 	struct cache_entry *next;
-	struct cache_entry *dir_next;
+	struct cache_entry *dir;
 	char name[FLEX_ARRAY]; /* more */
 };
 
@@ -285,6 +285,8 @@ extern void add_name_hash(struct index_state *istate, struct cache_entry *ce);
 static inline void remove_name_hash(struct cache_entry *ce)
 {
 	ce->ce_flags |= CE_UNHASHED;
+	if (ce->dir && !(--ce->dir->ce_size))
+		remove_name_hash(ce->dir);
 }
 
 
diff --git a/name-hash.c b/name-hash.c
index d8d25c2..01e8320 100644
--- a/name-hash.c
+++ b/name-hash.c
@@ -32,6 +32,9 @@ static unsigned int hash_name(const char *name, int namelen)
 	return hash;
 }
 
+static struct cache_entry *lookup_index_entry(struct index_state *istate, const char *name, int namelen, int icase);
+static void hash_index_entry(struct index_state *istate, struct cache_entry *ce);
+
 static void hash_index_entry_directories(struct index_state *istate, struct cache_entry *ce)
 {
 	/*
@@ -40,30 +43,25 @@ static void hash_index_entry_directories(struct index_state *istate, struct cach
 	 * closing slash.  Despite submodules being a directory, they never
 	 * reach this point, because they are stored without a closing slash
 	 * in the cache.
-	 *
-	 * Note that the cache_entry stored with the directory does not
-	 * represent the directory itself.  It is a pointer to an existing
-	 * filename, and its only purpose is to represent existence of the
-	 * directory in the cache.  It is very possible multiple directory
-	 * hash entries may point to the same cache_entry.
 	 */
-	unsigned int hash;
-	void **pos;
+	int len = ce_namelen(ce);
+	if (len && ce->name[len - 1] == '/')
+		len--;
+	while (len && ce->name[len - 1] != '/')
+		len--;
+	if (!len)
+		return;
 
-	const char *ptr = ce->name;
-	while (*ptr) {
-		while (*ptr && *ptr != '/')
-			++ptr;
-		if (*ptr == '/') {
-			++ptr;
-			hash = hash_name(ce->name, ptr - ce->name);
-			pos = insert_hash(hash, ce, &istate->name_hash);
-			if (pos) {
-				ce->dir_next = *pos;
-				*pos = ce;
-			}
-		}
+	ce->dir = lookup_index_entry(istate, ce->name, len, ignore_case);
+	if (!ce->dir) {
+		ce->dir = xcalloc(1, cache_entry_size(len));
+		memcpy(ce->dir->name, ce->name, len);
+		ce->dir->ce_namelen = len;
+		ce->dir->name[len] = 0;
+		hash_index_entry(istate, ce->dir);
 	}
+	ce->dir->ce_flags &= ~CE_UNHASHED;
+	ce->dir->ce_size++;
 }
 
 static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
@@ -74,7 +72,7 @@ static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
 	if (ce->ce_flags & CE_HASHED)
 		return;
 	ce->ce_flags |= CE_HASHED;
-	ce->next = ce->dir_next = NULL;
+	ce->next = ce->dir = NULL;
 	hash = hash_name(ce->name, ce_namelen(ce));
 	pos = insert_hash(hash, ce, &istate->name_hash);
 	if (pos) {
@@ -137,38 +135,32 @@ static int same_name(const struct cache_entry *ce, const char *name, int namelen
 	if (!icase)
 		return 0;
 
-	/*
-	 * If the entry we're comparing is a filename (no trailing slash), then compare
-	 * the lengths exactly.
-	 */
-	if (name[namelen - 1] != '/')
-		return slow_same_name(name, namelen, ce->name, len);
-
-	/*
-	 * For a directory, we point to an arbitrary cache_entry filename.  Just
-	 * make sure the directory portion matches.
-	 */
-	return slow_same_name(name, namelen, ce->name, namelen < len ? namelen : len);
+	return slow_same_name(name, namelen, ce->name, len);
 }
 
-struct cache_entry *index_name_exists(struct index_state *istate, const char *name, int namelen, int icase)
+static struct cache_entry *lookup_index_entry(struct index_state *istate, const char *name, int namelen, int icase)
 {
 	unsigned int hash = hash_name(name, namelen);
-	struct cache_entry *ce;
-
-	lazy_init_name_hash(istate);
-	ce = lookup_hash(hash, &istate->name_hash);
+	struct cache_entry *ce = lookup_hash(hash, &istate->name_hash);
 
 	while (ce) {
 		if (!(ce->ce_flags & CE_UNHASHED)) {
 			if (same_name(ce, name, namelen, icase))
 				return ce;
 		}
-		if (icase && name[namelen - 1] == '/')
-			ce = ce->dir_next;
-		else
-			ce = ce->next;
+		ce = ce->next;
 	}
+	return NULL;
+}
+
+struct cache_entry *index_name_exists(struct index_state *istate, const char *name, int namelen, int icase)
+{
+	struct cache_entry *ce;
+
+	lazy_init_name_hash(istate);
+	ce = lookup_index_entry(istate, name, namelen, icase);
+	if (ce)
+		return ce;
 
 	/*
 	 * Might be a submodule.  Despite submodules being directories,
@@ -182,7 +174,7 @@ struct cache_entry *index_name_exists(struct index_state *istate, const char *na
 	 * true.
 	 */
 	if (icase && name[namelen - 1] == '/') {
-		ce = index_name_exists(istate, name, namelen - 1, icase);
+		ce = lookup_index_entry(istate, name, namelen - 1, icase);
 		if (ce && S_ISGITLINK(ce->ce_mode))
 			return ce;
 	}
Previous: Jeff KingNext: Karsten Blees
Message 57 of 88 in “inotify to minimize stat() calls”
  1. Ramkumar RamachandraFeb 8, 2013
  2. Junio C HamanoFeb 8, 2013
  3. Junio C HamanoFeb 8, 2013
  4. Duy NguyenFeb 9, 2013
  5. Junio C HamanoFeb 9, 2013
  6. Junio C HamanoFeb 9, 2013
  7. Robert ZehFeb 9, 2013
  8. Ramkumar RamachandraFeb 9, 2013
  9. Ramkumar RamachandraFeb 9, 2013
  10. Ramkumar RamachandraFeb 9, 2013
  11. Duy NguyenFeb 9, 2013
  12. Ramkumar RamachandraFeb 9, 2013
  13. Ramkumar RamachandraFeb 9, 2013
  14. Duy NguyenFeb 10, 2013
  15. Duy NguyenFeb 10, 2013
  16. Duy NguyenFeb 10, 2013
  17. Junio C HamanoFeb 10, 2013
  18. Duy NguyenFeb 11, 2013
  19. Duy NguyenFeb 11, 2013
  20. Torsten BögershausenMar 7, 2013
  21. Junio C HamanoMar 8, 2013
  22. Torsten BögershausenMar 8, 2013
  23. Junio C HamanoMar 8, 2013
  24. Torsten BögershausenMar 8, 2013
  25. Duy NguyenMar 8, 2013
  26. Ramkumar RamachandraMar 10, 2013
  27. status: hint the user about -uno if read_directory takes too longNguyễn Thái Ngọc Duy, Mar 13, 2013
  28. Torsten BögershausenMar 13, 2013
  29. Junio C HamanoMar 13, 2013
  30. Duy NguyenMar 14, 2013
  31. Junio C HamanoMar 14, 2013
  32. Duy NguyenMar 15, 2013
  33. Torsten BögershausenMar 15, 2013
  34. Ramkumar RamachandraMar 15, 2013
  35. Junio C HamanoMar 15, 2013
  36. Torsten BögershausenMar 15, 2013
  37. Junio C HamanoMar 15, 2013
  38. Torsten BögershausenMar 15, 2013
  39. Junio C HamanoMar 15, 2013
  40. Torsten BögershausenMar 16, 2013
  41. Junio C HamanoMar 17, 2013
  42. Duy NguyenMar 16, 2013
  43. demerphqFeb 10, 2013
  44. Duy NguyenFeb 10, 2013
  45. Magnus BäckFeb 14, 2013
  46. Ramkumar RamachandraFeb 10, 2013
  47. Duy NguyenFeb 11, 2013
  48. Erik Faye-LundFeb 10, 2013
  49. Duy NguyenFeb 11, 2013
  50. Karsten BleesFeb 12, 2013
  51. Duy NguyenFeb 13, 2013
  52. Duy NguyenFeb 13, 2013
  53. Jeff KingFeb 13, 2013
  54. Jeff KingFeb 13, 2013
  55. Karsten BleesFeb 13, 2013
  56. Jeff KingFeb 13, 2013
  57. Karsten BleesFeb 14, 2013
  58. name-hash.c: fix endless loop with core.ignorecase=trueKarsten Blees, Feb 27, 2013
  59. Junio C HamanoFeb 27, 2013
  60. Karsten BleesFeb 27, 2013
  61. name-hash.c: fix endless loop with core.ignorecase=trueKarsten Blees, Feb 27, 2013
  62. Junio C HamanoFeb 28, 2013
  63. Ramkumar RamachandraFeb 19, 2013
  64. Karsten BleesFeb 19, 2013
  65. Drew NorthupFeb 19, 2013
  66. Duy NguyenFeb 19, 2013
  67. Junio C HamanoFeb 9, 2013
  68. Robert ZehFeb 10, 2013
  69. Martin FickFeb 10, 2013
  70. Robert ZehFeb 10, 2013
  71. Duy NguyenFeb 11, 2013
  72. Robert ZehFeb 11, 2013
  73. Ramkumar RamachandraFeb 19, 2013
  74. Robert ZehApr 24, 2013
  75. Duy NguyenApr 24, 2013
  76. Robert ZehApr 25, 2013
  77. Duy NguyenApr 25, 2013
  78. Robert ZehApr 26, 2013
  79. Thomas RastApr 25, 2013
  80. Robert ZehApr 25, 2013
  81. Thomas RastApr 25, 2013
  82. Thomas RastApr 27, 2013
  83. Duy NguyenApr 27, 2013
  84. Ramkumar RamachandraFeb 9, 2013
  85. Ævar Arnfjörð BjarmasonFeb 14, 2013
  86. Junio C HamanoFeb 14, 2013
  87. Ramkumar RamachandraFeb 19, 2013
  88. Duy NguyenApr 30, 2013

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.