]> source.dussan.org Git - jgit.git/commitdiff
Speed up handling of "only" paths in the CommitCommand 08/9408/3
authorRobin Rosenberg <robin.rosenberg@dewire.com>
Mon, 31 Dec 2012 18:17:19 +0000 (19:17 +0100)
committerRobin Rosenberg <robin.rosenberg@dewire.com>
Mon, 7 Jan 2013 00:08:01 +0000 (01:08 +0100)
Use binary search to reduce the number of lookups for very large number
of paths.

Change-Id: I76a16594b756bffd95298897414485a9cd637819

org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java

index f51b301cd5b05552c69d703d6be85f61c84816f2..056b47d0fd2a328408fc9a1ec8771ce698e4a6c8 100644 (file)
@@ -46,6 +46,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.text.MessageFormat;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
 
@@ -147,6 +148,7 @@ public class CommitCommand extends GitCommand<RevCommit> {
                        ConcurrentRefUpdateException,
                        WrongRepositoryStateException {
                checkCallable();
+               Collections.sort(only);
 
                RepositoryState state = repo.getRepositoryState();
                if (!state.canCommit())
@@ -452,18 +454,15 @@ public class CommitCommand extends GitCommand<RevCommit> {
         * @return the item's index in <code>only</code>; -1 if no item matches
         */
        private int lookupOnly(String pathString) {
-               int i = 0;
-               for (String o : only) {
-                       String p = pathString;
-                       while (true) {
-                               if (p.equals(o))
-                                       return i;
-                               int l = p.lastIndexOf("/"); //$NON-NLS-1$
-                               if (l < 1)
-                                       break;
-                               p = p.substring(0, l);
-                       }
-                       i++;
+               String p = pathString;
+               while (true) {
+                       int position = Collections.binarySearch(only, p);
+                       if (position >= 0)
+                               return position;
+                       int l = p.lastIndexOf("/"); //$NON-NLS-1$
+                       if (l < 1)
+                               break;
+                       p = p.substring(0, l);
                }
                return -1;
        }