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

[PATCH 1/4] Cache tree ObjectId's too

From
Robin Rosenberg <robin.rosenberg@dewire.com>
Date
Mar 18, 2007, 22:15 UTC
Message-ID
<20070318221555.24742.93993.stgit@lathund.dewire.com>
In-Reply-To
<20070318220711.24742.90943.stgit@lathund.dewire.com>

The caching part is experimental. We explicitly leave it up to the garbage collector when to recycle git objects. After trying this practically we will se what is best. The upside is that rescanning the history for a certain resource is very quick when data is still in the cache (tens of milliseconds), which is very good for interactivity. When memory is tight we go to disk and fetch the data, which could take a couple of seconds on a large repo.

Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
 .../src/org/spearce/jgit/lib/Repository.java       |   18 +++++++++++++-----
 1 files changed, 13 insertions(+), 5 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
index 8b1f42c..d3b2178 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
@@ -47,7 +47,8 @@ public class Repository {
 
 	private WindowCache windows;
 
-	private Map cache = new WeakHashMap(30000); 
+	private Map treeCache = new WeakHashMap(30000); 
+	private Map commitCache = new WeakHashMap(30000); 
 
 	public Repository(final File d) throws IOException {
 		gitDir = d.getAbsoluteFile();
@@ -189,7 +190,7 @@ public class Repository {
 	}
 
 	public Commit mapCommit(final ObjectId id) throws IOException {
-		Commit ret = (Commit)cache.get(id);
+		Commit ret = (Commit)commitCache.get(id);
 		if (ret != null)
 			return ret;
 
@@ -201,7 +202,7 @@ public class Repository {
 			ret = new Commit(this, id, raw);
 			// The key must not be the referenced strongly
 			// by the value in WeakHashMaps
-			cache.put(new ObjectId(id.getBytes()), ret);
+			commitCache.put(new ObjectId(id.getBytes()), ret);
 			return ret;
 		}
 		throw new IncorrectObjectTypeException(id, Constants.TYPE_COMMIT);
@@ -213,12 +214,19 @@ public class Repository {
 	}
 
 	public Tree mapTree(final ObjectId id) throws IOException {
+		Tree ret = (Tree)treeCache.get(id);
+		if (ret != null)
+			return ret;
+
 		final ObjectLoader or = openObject(id);
 		if (or == null)
 			return null;
 		final byte[] raw = or.getBytes();
-		if (Constants.TYPE_TREE.equals(or.getType()))
-			return new Tree(this, id, raw);
+		if (Constants.TYPE_TREE.equals(or.getType())) {
+			ret = new Tree(this, id, raw);
+			treeCache.put(new ObjectId(id.getBytes()), ret);
+			return ret;
+		}
 		if (Constants.TYPE_COMMIT.equals(or.getType()))
 			return mapTree(ObjectId.fromString(raw, 5));
 		throw new IncorrectObjectTypeException(id, Constants.TYPE_TREE);
Previous: Robin RosenbergNext: Robin Rosenberg
Message 2 of 6 in “Eclipse (EGIT) Structured compare”
  1. 0/4 Eclipse (EGIT) Structured compareRobin Rosenberg, Mar 18, 2007
  2. 1/4 Cache tree ObjectId's tooRobin Rosenberg, Mar 18, 2007
  3. 2/4 Rework tree/commit cacheRobin Rosenberg, Mar 18, 2007
  4. 3/4 Decode message when encoding line is presentRobin Rosenberg, Mar 18, 2007
  5. 4/4 Add support for structured comparison.Robin Rosenberg, Mar 18, 2007
  6. Shawn O. PearceMar 19, 2007

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.