Skip to content

Commit

Permalink
ObjectIndex: only resolve relevant pending objects
Browse files Browse the repository at this point in the history
If a pending object will not be of a type compatible with the
requested type, then skip resolution of that pending object.

This improves performance of the get(Class) and getAll() methods in
cases where there are a large number of pending objects in the index.

It also helps to eliminate difficulties like those in issue #90.
  • Loading branch information
ctrueden committed Jul 7, 2014
1 parent 88ed27b commit 0c36a52
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/main/java/org/scijava/object/ObjectIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public List<E> getAll() {
*/
public List<E> get(final Class<?> type) {
// lazily register any pending objects
if (!pending.isEmpty()) resolvePending();
if (!pending.isEmpty()) resolvePending(type);

List<E> list = retrieveList(type);
// NB: Return a copy of the data, to facilitate thread safety.
Expand Down Expand Up @@ -380,15 +380,32 @@ protected List<E> retrieveList(final Class<?> type) {
return list;
}

private void resolvePending() {
private void resolvePending(final Class<?> type) {
synchronized (pending) {
while (!pending.isEmpty()) {
final LazyObjects<? extends E> c = pending.remove(0);
for (int p = pending.size() - 1; p >= 0; p--) {
final LazyObjects<? extends E> c = pending.get(p);

// NB: If this pending callback returns objects of a different
// type than the one we are interested in, it can be skipped.
if (!isCompatibleType(c, type)) continue;

// trigger the callback and add the results
pending.remove(p);
addAll(c.get());
}
}
}

// -- Helper methods --

private boolean isCompatibleType(final LazyObjects<? extends E> c,
final Class<?> type)
{
if (type == All.class) return true;
final Class<?> cType = c.getType();
return cType.isAssignableFrom(type) || type.isAssignableFrom(cType);
}

// -- Helper classes --

private static class All {
Expand Down

0 comments on commit 0c36a52

Please sign in to comment.