Re: I'm a total push-over..

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Junio C Hamano
Date: Wednesday, January 23, 2008 - 12:23 am

Linus Torvalds <torvalds@linux-foundation.org> writes:


Yes, I agree with that in principle. Storing computable values
makes sense only when it is expensive to recompute.  We did not
have cache-tree for quite a long time until you noticed that it
was rather expensive and wasteful to recompute tree objects from
unchanged parts of the index every time.

It's the same argument; when the hashing performance starts to
become noticeable, we can think about storing and reusing it,
not before.


Yes, ls-files is cheap.  So is lstat(2) on Linux.  It only
matters when you do it many many times.

In any case, the change does not look too bad.  The best time
(real) of running git-ls-files in the kernel repository on my
box is 0.010s vs 0.011s (10% improvement, heh!, which is the
same as the master version) and empty commit is both 0.082s (no
change).

-- >8 --
[PATCH] lazy index hashing

This delays the hashing of index names until it becomes necessary for
the first time.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 cache.h      |    1 +
 read-cache.c |   26 +++++++++++++++++++++++---
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/cache.h b/cache.h
index 409738c..e4aeff0 100644
--- a/cache.h
+++ b/cache.h
@@ -191,6 +191,7 @@ struct index_state {
 	struct cache_tree *cache_tree;
 	time_t timestamp;
 	void *alloc;
+	unsigned name_hash_initialized : 1;
 	struct hash_table name_hash;
 };
 
diff --git a/read-cache.c b/read-cache.c
index 9477c0b..e45f4b3 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -34,12 +34,11 @@ static unsigned int hash_name(const char *name, int namelen)
 	return hash;
 }
 
-static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
+static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
 {
 	void **pos;
 	unsigned int hash = hash_name(ce->name, ce_namelen(ce));
 
-	istate->cache[nr] = ce;
 	pos = insert_hash(hash, ce, &istate->name_hash);
 	if (pos) {
 		ce->next = *pos;
@@ -47,6 +46,24 @@ static void set_index_entry(struct index_state *istate, int nr, struct cache_ent
 	}
 }
 
+static void lazy_init_name_hash(struct index_state *istate)
+{
+	int nr;
+
+	if (istate->name_hash_initialized)
+		return;
+	for (nr = 0; nr < istate->cache_nr; nr++)
+		hash_index_entry(istate, istate->cache[nr]);
+	istate->name_hash_initialized = 1;
+}
+
+static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
+{
+	istate->cache[nr] = ce;
+	if (istate->name_hash_initialized)
+		hash_index_entry(istate, ce);
+}
+
 /*
  * We don't actually *remove* it, we can just mark it invalid so that
  * we won't find it in lookups.
@@ -75,7 +92,10 @@ static void replace_index_entry(struct index_state *istate, int nr, struct cache
 int index_name_exists(struct index_state *istate, const char *name, int namelen)
 {
 	unsigned int hash = hash_name(name, namelen);
-	struct cache_entry *ce = lookup_hash(hash, &istate->name_hash);
+	struct cache_entry *ce;
+
+	lazy_init_name_hash(istate);
+	ce = lookup_hash(hash, &istate->name_hash);
 
 	while (ce) {
 		if (!(ce->ce_flags & CE_UNHASHED)) {
-- 
1.5.4.rc4.14.g6fc74

-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
I'm a total push-over.., Linus Torvalds, (Tue Jan 22, 4:37 pm)
Re: I'm a total push-over.., Kevin Ballard, (Tue Jan 22, 6:35 pm)
Re: I'm a total push-over.., Junio C Hamano, (Tue Jan 22, 7:23 pm)
Re: I'm a total push-over.., Junio C Hamano, (Tue Jan 22, 7:36 pm)
Re: I'm a total push-over.., Linus Torvalds, (Tue Jan 22, 7:58 pm)
Re: I'm a total push-over.., Linus Torvalds, (Tue Jan 22, 8:19 pm)
Re: I'm a total push-over.., Junio C Hamano, (Wed Jan 23, 12:23 am)
Re: I'm a total push-over.., Andreas Ericsson, (Wed Jan 23, 1:32 am)
Re: I'm a total push-over.., Dmitry Potapov, (Wed Jan 23, 2:15 am)
Re: I'm a total push-over.., Andreas Ericsson, (Wed Jan 23, 2:31 am)
Re: I'm a total push-over.., Johannes Schindelin, (Wed Jan 23, 5:24 am)
Re: I'm a total push-over.., Johannes Schindelin, (Wed Jan 23, 5:25 am)
Re: I'm a total push-over.., David Kastrup, (Wed Jan 23, 5:28 am)
Re: I'm a total push-over.., Theodore Tso, (Wed Jan 23, 5:56 am)
Re: I'm a total push-over.., Marko Kreen, (Wed Jan 23, 7:01 am)
Re: I'm a total push-over.., Andreas Ericsson, (Wed Jan 23, 7:39 am)
Re: I'm a total push-over.., Linus Torvalds, (Wed Jan 23, 9:06 am)
Re: I'm a total push-over.., Linus Torvalds, (Wed Jan 23, 9:25 am)
Re: I'm a total push-over.., Johannes Schindelin, (Wed Jan 23, 9:34 am)
Re: I'm a total push-over.., Linus Torvalds, (Wed Jan 23, 10:09 am)
Re: I'm a total push-over.., Dmitry Potapov, (Wed Jan 23, 10:10 am)
Re: I'm a total push-over.., Linus Torvalds, (Wed Jan 23, 10:29 am)
Re: I'm a total push-over.., Luke Lu, (Wed Jan 23, 11:51 pm)
Re: I'm a total push-over.., Andreas Ericsson, (Thu Jan 24, 3:24 am)
Re: I'm a total push-over.., Andreas Ericsson, (Thu Jan 24, 3:39 am)
Re: I'm a total push-over.., Marko Kreen, (Thu Jan 24, 6:19 am)
Re: I'm a total push-over.., Andreas Ericsson, (Thu Jan 24, 9:00 am)
Re: I'm a total push-over.., Marko Kreen, (Thu Jan 24, 9:13 am)
Re: I'm a total push-over.., Dmitry Potapov, (Thu Jan 24, 9:28 am)
Re: I'm a total push-over.., Linus Torvalds, (Thu Jan 24, 10:15 am)
Re: I'm a total push-over.., Dmitry Potapov, (Thu Jan 24, 11:45 am)
Re: I'm a total push-over.., Linus Torvalds, (Thu Jan 24, 12:08 pm)
Re: I'm a total push-over.., Jeremy Maitin-Shepard, (Thu Jan 24, 10:21 pm)
Re: I'm a total push-over.., Junio C Hamano, (Thu Jan 24, 11:50 pm)
Re: I'm a total push-over.., Johannes Schindelin, (Fri Jan 25, 5:51 am)
Re: I'm a total push-over.., Linus Torvalds, (Fri Jan 25, 9:24 am)
Re: I'm a total push-over.., Jeremy Maitin-Shepard, (Fri Jan 25, 11:19 am)
Re: I'm a total push-over.., Johannes Schindelin, (Fri Jan 25, 11:24 am)
Re: I'm a total push-over.., Junio C Hamano, (Fri Jan 25, 12:07 pm)
Re: I'm a total push-over.., Marko Kreen, (Fri Jan 25, 1:08 pm)
Re: I'm a total push-over.., Marko Kreen, (Fri Jan 25, 1:52 pm)
Re: I'm a total push-over.., Linus Torvalds, (Fri Jan 25, 3:16 pm)
Re: I'm a total push-over.., Linus Torvalds, (Fri Jan 25, 3:35 pm)
Re: I'm a total push-over.., Marko Kreen, (Sat Jan 26, 5:16 am)
Re: I'm a total push-over.., Marko Kreen, (Sat Jan 26, 5:37 am)
Re: I'm a total push-over.., Linus Torvalds, (Sat Jan 26, 11:51 pm)
Re: I'm a total push-over.., Dmitry Potapov, (Sun Jan 27, 1:21 am)
Re: I'm a total push-over.., Marko Kreen, (Sun Jan 27, 2:45 am)
Re: I'm a total push-over.., Johannes Schindelin, (Sun Jan 27, 7:07 am)
Re: I'm a total push-over.., Dmitry Potapov, (Sun Jan 27, 7:48 am)
Re: I'm a total push-over.., Dmitry Potapov, (Sun Jan 27, 8:06 am)