]> source.dussan.org Git - jgit.git/commitdiff
blame: Micro optimize blob lookup in tree 43/25243/2
authorShawn Pearce <spearce@spearce.org>
Thu, 17 Apr 2014 22:03:18 +0000 (15:03 -0700)
committerShawn Pearce <spearce@spearce.org>
Thu, 17 Apr 2014 22:51:50 +0000 (15:51 -0700)
Avoid converting the raw mode to FileMode. This is an expensive
if-else-if sort of test to just check if the thing is a blob.
Instead test the bit mask directly, which is at least a few
instructions shorter.

The TreeWalk is already recursive and will auto-dive into any
subtrees found. isSubtree check is unnecessary, as is the loop,
as only one result will ever be returned by next().

Change-Id: I9fb25229ebed857469427bfbdf74aedebfddfac8

org.eclipse.jgit/src/org/eclipse/jgit/blame/BlameGenerator.java

index 63d0fb2cfb7ad7dcb48048505750d1d17fed5687..34e15475c3bbbfabc192198b6100ab2292ff1d5e 100644 (file)
@@ -44,6 +44,7 @@
 package org.eclipse.jgit.blame;
 
 import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
+import static org.eclipse.jgit.lib.FileMode.TYPE_FILE;
 
 import java.io.IOException;
 import java.util.Collection;
@@ -942,20 +943,17 @@ public class BlameGenerator {
        private boolean find(RevCommit commit, PathFilter path) throws IOException {
                treeWalk.setFilter(path);
                treeWalk.reset(commit.getTree());
-               while (treeWalk.next()) {
-                       if (path.isDone(treeWalk)) {
-                               if (treeWalk.getFileMode(0).getObjectType() != OBJ_BLOB)
-                                       return false;
-                               treeWalk.getObjectId(idBuf, 0);
-                               return true;
-                       }
-
-                       if (treeWalk.isSubtree())
-                               treeWalk.enterSubtree();
+               if (treeWalk.next() && isFile(treeWalk.getRawMode(0))) {
+                       treeWalk.getObjectId(idBuf, 0);
+                       return true;
                }
                return false;
        }
 
+       private static final boolean isFile(int rawMode) {
+               return (rawMode & TYPE_FILE) == TYPE_FILE;
+       }
+
        private DiffEntry findRename(RevCommit parent, RevCommit commit,
                        PathFilter path) throws IOException {
                if (renameDetector == null)