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
This commit is contained in:
Robin Rosenberg 2012-12-31 19:17:19 +01:00
parent c2d1183e39
commit 549034500a
1 changed files with 11 additions and 12 deletions

View File

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