From 549034500a55ffc8f401fd73a74d8dc55f096d2f Mon Sep 17 00:00:00 2001 From: Robin Rosenberg Date: Mon, 31 Dec 2012 19:17:19 +0100 Subject: [PATCH] Speed up handling of "only" paths in the CommitCommand Use binary search to reduce the number of lookups for very large number of paths. Change-Id: I76a16594b756bffd95298897414485a9cd637819 --- .../org/eclipse/jgit/api/CommitCommand.java | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java index f51b301cd..056b47d0f 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java @@ -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 { ConcurrentRefUpdateException, WrongRepositoryStateException { checkCallable(); + Collections.sort(only); RepositoryState state = repo.getRepositoryState(); if (!state.canCommit()) @@ -452,18 +454,15 @@ public class CommitCommand extends GitCommand { * @return the item's index in only; -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; }