comparison test/org/tmatesoft/hg/test/MapTagsToFileRevisions.java @ 256:b61ed0f2c4da

Yet another slight perf/mem improvement for the tags per file sample
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 16 Aug 2011 15:45:47 +0200
parents 5a6ab50b4cbf
children ea0c0de86d0e
comparison
equal deleted inserted replaced
255:5a6ab50b4cbf 256:b61ed0f2c4da
1 package org.tmatesoft.hg.test; 1 package org.tmatesoft.hg.test;
2 2
3 import java.io.File; 3 import java.io.File;
4 import java.util.ArrayList; 4 import java.util.ArrayList;
5 import java.util.Arrays;
5 import java.util.HashMap; 6 import java.util.HashMap;
6 import java.util.LinkedList; 7 import java.util.LinkedList;
7 import java.util.List; 8 import java.util.List;
8 import java.util.Map; 9 import java.util.Map;
9 10
60 tagLocalRevs[i] = clogrmap.localRevision(tagRevision); 61 tagLocalRevs[i] = clogrmap.localRevision(tagRevision);
61 } 62 }
62 System.out.printf("Prepared tag revisions to analyze: %d ms\n", System.currentTimeMillis() - start); 63 System.out.printf("Prepared tag revisions to analyze: %d ms\n", System.currentTimeMillis() - start);
63 // 64 //
64 repository.getManifest().walk(new HgManifest.Inspector() { 65 repository.getManifest().walk(new HgManifest.Inspector() {
65 private final ArrayList<Integer> tagIndexAtRev = new ArrayList<Integer>(); 66 private int[] tagIndexAtRev = new int[4]; // it's unlikely there would be a lot of tags associated with a given cset
66 private final Pool<String> filenamePool = new Pool<String>(); 67 private final Pool<String> filenamePool = new Pool<String>();
67 private final Pool<Nodeid> nodeidPool = new Pool<Nodeid>(); 68 private final Pool<Nodeid> nodeidPool = new Pool<Nodeid>();
68 69
69 public boolean begin(int mainfestRevision, Nodeid nid, int changelogRevision) { 70 public boolean begin(int mainfestRevision, Nodeid nid, int changelogRevision) {
70 Nodeid cset = clogrmap.revision(changelogRevision); 71 Nodeid cset = clogrmap.revision(changelogRevision);
71 tagIndexAtRev.clear(); 72 Arrays.fill(tagIndexAtRev, -1);
72 for (int i = 0; i < allTags.length; i++) { 73 for (int i = 0, x = 0; i < allTags.length; i++) {
73 if (cset.equals(allTags[i].revision())) { 74 if (cset.equals(allTags[i].revision())) {
74 tagIndexAtRev.add(i); 75 tagIndexAtRev[x++] = i;
76 if (x == tagIndexAtRev.length) {
77 // expand twice as much
78 int[] expanded = new int[x << 1];
79 System.arraycopy(tagIndexAtRev, 0, expanded, 0, x);
80 expanded[x] = -1; // just in case there'd be no more tags associated with this cset
81 tagIndexAtRev = expanded;
82 }
75 } 83 }
76 } 84 }
77 if (tagIndexAtRev.isEmpty()) { 85 if (tagIndexAtRev[0] == -1) {
78 System.out.println("Can't happen, provided we iterate over revisions with tags only"); 86 System.out.println("Can't happen, provided we iterate over revisions with tags only");
79 } 87 }
80 return true; 88 return true;
81 } 89 }
82 90
86 Nodeid[] m = file2rev2tag.get(fname); 94 Nodeid[] m = file2rev2tag.get(fname);
87 if (m == null) { 95 if (m == null) {
88 file2rev2tag.put(fname, m = new Nodeid[allTags.length]); 96 file2rev2tag.put(fname, m = new Nodeid[allTags.length]);
89 } 97 }
90 for (int tagIndex : tagIndexAtRev) { 98 for (int tagIndex : tagIndexAtRev) {
99 if (tagIndex == -1) {
100 break;
101 }
91 if (m[tagIndex] != null) { 102 if (m[tagIndex] != null) {
92 System.out.printf("There's another revision (%s) associated with tag %s already while we try to associate %s\n", m[tagIndex].shortNotation(), allTags[tagIndex].name(), nid.shortNotation()); 103 System.out.printf("There's another revision (%s) associated with tag %s already while we try to associate %s\n", m[tagIndex].shortNotation(), allTags[tagIndex].name(), nid.shortNotation());
93 } 104 }
94 m[tagIndex] = nid; 105 m[tagIndex] = nid;
95 } 106 }