comparison src/org/tmatesoft/hg/repo/HgManifest.java @ 267:ec921ef0628e

Revert manifest parser changes - no single string and back to HashMap Pool
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 23 Aug 2011 21:27:56 +0200
parents 0a2f445de774
children c5980f287cc4
comparison
equal deleted inserted replaced
266:0a2f445de774 267:ec921ef0628e
28 import org.tmatesoft.hg.internal.DataAccess; 28 import org.tmatesoft.hg.internal.DataAccess;
29 import org.tmatesoft.hg.internal.DigestHelper; 29 import org.tmatesoft.hg.internal.DigestHelper;
30 import org.tmatesoft.hg.internal.Experimental; 30 import org.tmatesoft.hg.internal.Experimental;
31 import org.tmatesoft.hg.internal.Lifecycle; 31 import org.tmatesoft.hg.internal.Lifecycle;
32 import org.tmatesoft.hg.internal.Pool; 32 import org.tmatesoft.hg.internal.Pool;
33 import org.tmatesoft.hg.internal.Pool2;
34 import org.tmatesoft.hg.internal.RevlogStream; 33 import org.tmatesoft.hg.internal.RevlogStream;
35 import org.tmatesoft.hg.util.Path; 34 import org.tmatesoft.hg.util.Path;
36 35
37 36
38 /** 37 /**
148 public interface Inspector { 147 public interface Inspector {
149 boolean begin(int mainfestRevision, Nodeid nid, int changelogRevision); 148 boolean begin(int mainfestRevision, Nodeid nid, int changelogRevision);
150 boolean next(Nodeid nid, String fname, String flags); 149 boolean next(Nodeid nid, String fname, String flags);
151 boolean end(int manifestRevision); 150 boolean end(int manifestRevision);
152 } 151 }
152
153 public interface ElementProxy<T> {
154 T get();
155 }
156
157 private static class PoolStringProxy {
158 private byte[] data;
159 private int start, length;
160 private int hash;
161 private String result;
162
163 public void init(byte[] data, int start, int length) {
164 this.data = data;
165 this.start = start;
166 this.length = length;
167
168 // copy from String.hashCode()
169 int h = 0;
170 byte[] d = data;
171 for (int i = 0, off = start, len = length; i < len; i++) {
172 h = 31 * h + d[off++];
173 }
174 hash = h;
175 }
176
177 @Override
178 public boolean equals(Object obj) {
179 if (false == obj instanceof PoolStringProxy) {
180 return false;
181 }
182 PoolStringProxy o = (PoolStringProxy) obj;
183 if (o.result != null && result != null) {
184 return result.equals(o.result);
185 }
186 if (o.result == null && result != null || o.result != null && result == null) {
187 String s; PoolStringProxy noString;
188 if (o.result == null) {
189 s = result;
190 noString = o;
191 } else {
192 s = o.result;
193 noString = this;
194 }
195
196 }
197 // both are null
198 if (o.length != length) {
199 return false;
200 }
201 for (int i = 0, x = o.start, y = start; i < length; i++) {
202 if (o.data[x++] != data[y++]) {
203 return false;
204 }
205 }
206 return true;
207 }
208 @Override
209 public int hashCode() {
210 return hash;
211 }
212
213 public String freeze() {
214 if (result == null) {
215 result = new String(data, start, length);
216 data = null;
217 start = length = -1;
218 }
219 return result;
220 }
221 }
153 222
154 private static class ManifestParser implements RevlogStream.Inspector/*, Lifecycle */{ 223 private static class ManifestParser implements RevlogStream.Inspector/*, Lifecycle */{
155 private boolean gtg = true; // good to go 224 private boolean gtg = true; // good to go
156 private final Inspector inspector; 225 private final Inspector inspector;
157 private Pool2<Nodeid> nodeidPool, thisRevPool; 226 private Pool<Nodeid> nodeidPool, thisRevPool;
158 private final Pool2<String> fnamePool; 227 private final Pool<String> fnamePool;
159 private final Pool<String> flagsPool; 228 private final Pool<String> flagsPool;
160 private final byte[] nodeidAsciiConvertBuffer = new byte[40];
161 private byte[] nodeidLookupBuffer = new byte[20]; // get reassigned each time new Nodeid is added to pool 229 private byte[] nodeidLookupBuffer = new byte[20]; // get reassigned each time new Nodeid is added to pool
162 230
163 public ManifestParser(Inspector delegate) { 231 public ManifestParser(Inspector delegate) {
164 assert delegate != null; 232 assert delegate != null;
165 inspector = delegate; 233 inspector = delegate;
166 nodeidPool = new Pool2<Nodeid>(); 234 nodeidPool = new Pool<Nodeid>();
167 fnamePool = new Pool2<String>(); 235 fnamePool = new Pool<String>();
168 flagsPool = new Pool<String>(); 236 flagsPool = new Pool<String>();
169 thisRevPool = new Pool2<Nodeid>(); 237 thisRevPool = new Pool<Nodeid>();
170 } 238 }
171 239
172 public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess da) { 240 public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess da) {
173 if (!gtg) { 241 if (!gtg) {
174 return; 242 return;
175 } 243 }
176 try { 244 try {
177 gtg = gtg && inspector.begin(revisionNumber, new Nodeid(nodeid, true), linkRevision); 245 gtg = gtg && inspector.begin(revisionNumber, new Nodeid(nodeid, true), linkRevision);
178 String fname = null; 246 String fname = null;
179 String flags = null; 247 String flags = null;
180 Nodeid nid = null; 248 Nodeid nid = null;
181 String data = new String(da.byteArray()); 249 int i;
182 final int dataLen = data.length(); // due to byte->char conversion, may be different 250 byte[] data = da.byteArray();
183 for (int x = 0; gtg && x < dataLen; x++) { 251 for (i = 0; gtg && i < actualLen; i++) {
184 int start = x; 252 int x = i;
185 x = data.indexOf('\n', x+1); 253 for( ; data[i] != '\n' && i < actualLen; i++) {
186 assert x != -1; 254 if (fname == null && data[i] == 0) {
187 int z = data.indexOf('\0', start+1); 255 fname = fnamePool.unify(new String(data, x, i - x));
188 assert z!= -1; 256 x = i+1;
189 assert z < x;
190 fname = data.substring(start, z);
191 if (fnamePool.contains(fname)) {
192 fname = fnamePool.unify(fname);
193 } else {
194 fnamePool.record(fname = new String(fname));
195 }
196 z++; // cursor at first char of nodeid
197 int nodeidLen = x-z < 40 ? x-z : 40; // if x-z > 40, there are flags
198 for (int k = 0; k < nodeidLen; k++) {
199 // intentionally didn't clear array as it shall be of length 40 (Nodeid.fromAscii won't stand anything but 40)
200 nodeidAsciiConvertBuffer[k] = (byte) data.charAt(z+k);
201 }
202 DigestHelper.ascii2bin(nodeidAsciiConvertBuffer, 0, nodeidLen, nodeidLookupBuffer);
203 nid = new Nodeid(nodeidLookupBuffer, false); // this Nodeid is for pool lookup only, mock object
204 Nodeid cached = nodeidPool.unify(nid);
205 if (cached == nid) {
206 // buffer now belongs to the cached nodeid
207 nodeidLookupBuffer = new byte[20];
208 } else {
209 nid = cached; // use existing version, discard the lookup object
210 }
211 thisRevPool.record(nid); // memorize revision for the next iteration.
212 if (x-z > 40) {
213 // 'x' and 'l' for executable bits and symlinks?
214 // hg --debug manifest shows 644 for each regular file in my repo
215 // for cpython repo, there are 755 in hg --debug output when 'x' flag is present
216 flags = data.substring(z + nodeidLen, x);
217 if (flagsPool.contains(flags)) {
218 flags = flagsPool.unify(flags);
219 } else {
220 flagsPool.record(flags = new String(flags));
221 } 257 }
222 } 258 }
223 gtg = gtg && inspector.next(nid, fname, flags); 259 if (i < actualLen) {
260 assert data[i] == '\n';
261 int nodeidLen = i - x < 40 ? i-x : 40; // if > 40, there are flags
262 DigestHelper.ascii2bin(data, x, nodeidLen, nodeidLookupBuffer); // ignore return value as it's unlikely to have NULL in manifest
263 nid = new Nodeid(nodeidLookupBuffer, false); // this Nodeid is for pool lookup only, mock object
264 Nodeid cached = nodeidPool.unify(nid);
265 if (cached == nid) {
266 // buffer now belongs to the cached nodeid
267 nodeidLookupBuffer = new byte[20];
268 } else {
269 nid = cached; // use existing version, discard the lookup object
270 } // for cpython 0..10k, cache hits are 15 973 301, vs 18871 misses.
271 thisRevPool.record(nid); // memorize revision for the next iteration.
272 if (nodeidLen + x < i) {
273 // 'x' and 'l' for executable bits and symlinks?
274 // hg --debug manifest shows 644 for each regular file in my repo
275 flags = flagsPool.unify(new String(data, x + nodeidLen, i-x-nodeidLen));
276 }
277 gtg = gtg && inspector.next(nid, fname, flags);
278 }
224 nid = null; 279 nid = null;
225 fname = flags = null; 280 fname = flags = null;
226 } 281 }
227 gtg = gtg && inspector.end(revisionNumber); 282 gtg = gtg && inspector.end(revisionNumber);
228 // 283 //
229 // keep only actual file revisions, found at this version 284 // keep only actual file revisions, found at this version
230 // (next manifest is likely to refer to most of them, although in specific cases 285 // (next manifest is likely to refer to most of them, although in specific cases
231 // like commit in another branch a lot may be useless) 286 // like commit in another branch a lot may be useless)
232 nodeidPool.clear(); 287 nodeidPool.clear();
233 Pool2<Nodeid> t = nodeidPool; 288 Pool<Nodeid> t = nodeidPool;
234 nodeidPool = thisRevPool; 289 nodeidPool = thisRevPool;
235 thisRevPool = t; 290 thisRevPool = t;
236 } catch (IOException ex) { 291 } catch (IOException ex) {
237 throw new HgBadStateException(ex); 292 throw new HgBadStateException(ex);
238 } 293 }