[JGIT PATCH 07/14] Micro-optimize AbstractTreeIterator.pathCompare
From: Shawn O. Pearce <hidden>
Date: 2016-06-15 22:45:11
Subsystem:
the rest · Maintainer:
Linus Torvalds
We were doing far too much work in pathCompare to handle cases that just cannot ever happen, such as if the paths were the same length but had different "last path char" and then somehow had different lengths. We also had the JVM doing a lot of comparsion ops just to return -1/0/1 when really we can get away with the non-zero result returned to the caller. Issuing just the subtraction and one comparsion to 0 is much quicker, JIT or not. Signed-off-by: Shawn O. Pearce <redacted> --- .../jgit/treewalk/AbstractTreeIterator.java | 44 ++----------------- 1 files changed, 5 insertions(+), 39 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/AbstractTreeIterator.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/AbstractTreeIterator.java
index bd75d2d..31257b5 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/treewalk/AbstractTreeIterator.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/AbstractTreeIterator.java@@ -243,45 +243,11 @@ int pathCompare(final AbstractTreeIterator p, final int pMode) { return cmp; } - if (cPos < aLen) { - final int aj = a[cPos] & 0xff; - final int lastb = lastPathChar(pMode); - if (aj < lastb) - return -1; - else if (aj > lastb) - return 1; - else if (cPos == aLen - 1) - return 0; - else - return -1; - } - - if (cPos < bLen) { - final int bk = b[cPos] & 0xff; - final int lasta = lastPathChar(mode); - if (lasta < bk) - return -1; - else if (lasta > bk) - return 1; - else if (cPos == bLen - 1) - return 0; - else - return 1; - } - - final int lasta = lastPathChar(mode); - final int lastb = lastPathChar(pMode); - if (lasta < lastb) - return -1; - else if (lasta > lastb) - return 1; - - if (aLen == bLen) - return 0; - else if (aLen < bLen) - return -1; - else - return 1; + if (cPos < aLen) + return (a[cPos] & 0xff) - lastPathChar(pMode); + if (cPos < bLen) + return lastPathChar(mode) - (b[cPos] & 0xff); + return lastPathChar(mode) - lastPathChar(pMode); } private static int lastPathChar(final int mode) {
--
1.6.0.87.g2858d