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

[EGIT PATCH 05/10] Speed up ObjectId a little.

From
Robin Rosenberg <robin.rosenberg@dewire.com>
Date
May 7, 2007, 21:29 UTC
Message-ID
<20070507212939.21546.80786.stgit@lathund.dewire.com>
In-Reply-To
<20070507212636.21546.29280.stgit@lathund.dewire.com>
ObjectId size if fixed so we can exploit this fact.
Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
 .../src/org/spearce/jgit/lib/ObjectId.java         |   29 ++++++++++++++---------
 1 files changed, 18 insertions(+), 11 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectId.java b/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectId.java
index 45e23e6..fea0d91 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectId.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectId.java
@@ -86,7 +86,7 @@ public class ObjectId implements Comparable {
 	private static int compare(final byte[] a, final byte[] b) {
 		if (a==b)
 			return 0;
-		for (int k = 0; k < a.length && k < b.length; k++) {
+		for (int k = 0; k < Constants.OBJECT_ID_LENGTH; k++) {
 			final int ak = a[k] & 0xff;
 			final int bk = b[k] & 0xff;
 			if (ak < bk)
@@ -94,7 +94,11 @@ public class ObjectId implements Comparable {
 			else if (ak > bk)
 				return 1;
 		}
-		return a.length == b.length ? 0 : a.length < b.length ? -1 : 1;
+		if (a.length != Constants.OBJECT_ID_LENGTH)
+			throw new IllegalArgumentException("Looks like a bad object id");
+		if (b.length != Constants.OBJECT_ID_LENGTH)
+			throw new IllegalArgumentException("Looks like a bad object id");
+		return 0;
 	}
 
 	private final byte[] id;
@@ -106,9 +110,11 @@ public class ObjectId implements Comparable {
 		}
 
 		id = new byte[Constants.OBJECT_ID_LENGTH];
+		char[] bs = new char[Constants.OBJECT_ID_LENGTH*2];
+		i.getChars(0,Constants.OBJECT_ID_LENGTH*2,bs,0);
 		for (int j = 0, k = 0; k < Constants.OBJECT_ID_LENGTH; k++) {
-			final char c1 = i.charAt(j++);
-			final char c2 = i.charAt(j++);
+			final char c1 = bs[j++];
+			final char c2 = bs[j++];
 			int b;
 
 			if ('0' <= c1 && c1 <= '9') {
@@ -181,7 +187,7 @@ public class ObjectId implements Comparable {
 	}
 
 	public void copyTo(final OutputStream w) throws IOException {
-		for (int k = 0; k < id.length; k++) {
+		for (int k = 0; k < Constants.OBJECT_ID_LENGTH; k++) {
 			final int b = id[k];
 			final int b1 = (b >> 4) & 0xf;
 			final int b2 = b & 0xf;
@@ -191,7 +197,7 @@ public class ObjectId implements Comparable {
 	}
 
 	public void copyTo(final Writer w) throws IOException {
-		for (int k = 0; k < id.length; k++) {
+		for (int k = 0; k < Constants.OBJECT_ID_LENGTH; k++) {
 			final int b = id[k];
 			final int b1 = (b >> 4) & 0xf;
 			final int b2 = b & 0xf;
@@ -201,14 +207,15 @@ public class ObjectId implements Comparable {
 	}
 
 	public String toString() {
-		final StringBuffer r = new StringBuffer(2 * id.length);
-		for (int k = 0; k < id.length; k++) {
+		byte s[] = new byte[Constants.OBJECT_ID_LENGTH*2];
+		int i = 0;
+		for (int k = 0; k < Constants.OBJECT_ID_LENGTH; k++) {
 			final int b = id[k];
 			final int b1 = (b >> 4) & 0xf;
 			final int b2 = b & 0xf;
-			r.append(b1 < 10 ? (char) ('0' + b1) : (char) ('a' + b1 - 10));
-			r.append(b2 < 10 ? (char) ('0' + b2) : (char) ('a' + b2 - 10));
+			s[i++] = (b1 < 10 ? (byte) ('0' + b1) : (byte) ('a' + b1 - 10));
+			s[i++] = (b2 < 10 ? (byte) ('0' + b2) : (byte) ('a' + b2 - 10));
 		}
-		return r.toString();
+		return new String(s,0);
 	}
 }
Previous: Robin RosenbergNext: Robin Rosenberg
Message 6 of 14 in “[EGIT 00/10] This weeks Eclipse patches”
  1. Robin RosenbergMay 7, 2007
  2. 01/10 Fixes due to changes in the Eclipse Team APIRobin Rosenberg, May 7, 2007
  3. 02/10 Recursion and update of all elements regardless of need.Robin Rosenberg, May 7, 2007
  4. 03/10 Update compare window immediatelyRobin Rosenberg, May 7, 2007
  5. 04/10 Bug: Do not crash when showing diff for first version of a fileRobin Rosenberg, May 7, 2007
  6. 05/10 Speed up ObjectId a little.Robin Rosenberg, May 7, 2007
  7. 06/10 Create a generic history walkerRobin Rosenberg, May 7, 2007
  8. 07/10 Cache pack index fullyRobin Rosenberg, May 7, 2007
  9. 08/10 Use ObjectId[] instead of List for parentsRobin Rosenberg, May 7, 2007
  10. 09/10 Run history refresh in backgroundRobin Rosenberg, May 7, 2007
  11. 10/10 Enable a tooltip in the history viewRobin Rosenberg, May 7, 2007
  12. Eclipse support status (was: Re: [EGIT 00/10] This weeks Eclipse patches)Grzegorz Kulewski, May 7, 2007
  13. Shawn O. PearceMay 8, 2007
  14. Shawn O. PearceMay 8, 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.