diff src/org/tmatesoft/hg/repo/HgManifest.java @ 196:e2115da4cf6a

Pool objects to avoid memory polution with duplicates
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Mon, 18 Apr 2011 18:04:24 +0200
parents d5268ca7715b
children 047b1dec7a04
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/repo/HgManifest.java	Fri Apr 15 05:17:44 2011 +0200
+++ b/src/org/tmatesoft/hg/repo/HgManifest.java	Mon Apr 18 18:04:24 2011 +0200
@@ -21,6 +21,7 @@
 import org.tmatesoft.hg.core.HgBadStateException;
 import org.tmatesoft.hg.core.Nodeid;
 import org.tmatesoft.hg.internal.DataAccess;
+import org.tmatesoft.hg.internal.Pool;
 import org.tmatesoft.hg.internal.RevlogStream;
 
 
@@ -36,50 +37,10 @@
 	}
 
 	public void walk(int start, int end, final Inspector inspector) {
-		RevlogStream.Inspector insp = new RevlogStream.Inspector() {
-
-			private boolean gtg = true; // good to go
-
-			public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess da) {
-				if (!gtg) {
-					return;
-				}
-				try {
-					gtg = gtg && inspector.begin(revisionNumber, new Nodeid(nodeid, true));
-					int i;
-					String fname = null;
-					String flags = null;
-					Nodeid nid = null;
-					byte[] data = da.byteArray();
-					for (i = 0; gtg && i < actualLen; i++) {
-						int x = i;
-						for( ; data[i] != '\n' && i < actualLen; i++) {
-							if (fname == null && data[i] == 0) {
-								fname = new String(data, x, i - x);
-								x = i+1;
-							}
-						}
-						if (i < actualLen) {
-							assert data[i] == '\n'; 
-							int nodeidLen = i - x < 40 ? i-x : 40;
-							nid = Nodeid.fromAscii(data, x, nodeidLen);
-							if (nodeidLen + x < i) {
-								// 'x' and 'l' for executable bits and symlinks?
-								// hg --debug manifest shows 644 for each regular file in my repo
-								flags = new String(data, x + nodeidLen, i-x-nodeidLen);
-							}
-							gtg = gtg && inspector.next(nid, fname, flags);
-						}
-						nid = null;
-						fname = flags = null;
-					}
-					gtg = gtg && inspector.end(revisionNumber);
-				} catch (IOException ex) {
-					throw new HgBadStateException(ex);
-				}
-			}
-		};
-		content.iterate(start, end, true, insp);
+		if (inspector == null) {
+			throw new IllegalArgumentException();
+		}
+		content.iterate(start, end, true, new ManifestParser(inspector));
 	}
 
 	public interface Inspector {
@@ -87,4 +48,59 @@
 		boolean next(Nodeid nid, String fname, String flags);
 		boolean end(int revision);
 	}
+
+	private static class ManifestParser implements RevlogStream.Inspector {
+		private boolean gtg = true; // good to go
+		private final Inspector inspector;
+		private final Pool<Nodeid> nodeidPool;
+		private final Pool<String> fnamePool;
+		private final Pool<String> flagsPool;
+
+		public ManifestParser(Inspector delegate) {
+			assert delegate != null;
+			inspector = delegate;
+			nodeidPool = new Pool<Nodeid>();
+			fnamePool = new Pool<String>();
+			flagsPool = new Pool<String>();
+		}
+
+		public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess da) {
+			if (!gtg) {
+				return;
+			}
+			try {
+				gtg = gtg && inspector.begin(revisionNumber, new Nodeid(nodeid, true));
+				int i;
+				String fname = null;
+				String flags = null;
+				Nodeid nid = null;
+				byte[] data = da.byteArray();
+				for (i = 0; gtg && i < actualLen; i++) {
+					int x = i;
+					for( ; data[i] != '\n' && i < actualLen; i++) {
+						if (fname == null && data[i] == 0) {
+							fname = fnamePool.unify(new String(data, x, i - x));
+							x = i+1;
+						}
+					}
+					if (i < actualLen) {
+						assert data[i] == '\n'; 
+						int nodeidLen = i - x < 40 ? i-x : 40;
+						nid = nodeidPool.unify(Nodeid.fromAscii(data, x, nodeidLen));
+						if (nodeidLen + x < i) {
+							// 'x' and 'l' for executable bits and symlinks?
+							// hg --debug manifest shows 644 for each regular file in my repo
+							flags = flagsPool.unify(new String(data, x + nodeidLen, i-x-nodeidLen));
+						}
+						gtg = gtg && inspector.next(nid, fname, flags);
+					}
+					nid = null;
+					fname = flags = null;
+				}
+				gtg = gtg && inspector.end(revisionNumber);
+			} catch (IOException ex) {
+				throw new HgBadStateException(ex);
+			}
+		}
+	}
 }