Skip to content

Commit 29ff80b

Browse files
committed
ObjectIndex: only resolve relevant pending objects
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.
1 parent 8bbb47a commit 29ff80b

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/main/java/org/scijava/object/ObjectIndex.java

+21-4
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public List<E> getAll() {
129129
*/
130130
public List<E> get(final Class<?> type) {
131131
// lazily register any pending objects
132-
if (!pending.isEmpty()) resolvePending();
132+
if (!pending.isEmpty()) resolvePending(type);
133133

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

383-
private void resolvePending() {
383+
private void resolvePending(final Class<?> type) {
384384
synchronized (pending) {
385-
while (!pending.isEmpty()) {
386-
final LazyObjects<? extends E> c = pending.remove(0);
385+
for (int p = pending.size() - 1; p >= 0; p--) {
386+
final LazyObjects<? extends E> c = pending.get(p);
387+
388+
// NB: If this pending callback returns objects of a different
389+
// type than the one we are interested in, it can be skipped.
390+
if (!isCompatibleType(c, type)) continue;
391+
392+
// trigger the callback and add the results
393+
pending.remove(p);
387394
addAll(c.get());
388395
}
389396
}
390397
}
391398

399+
// -- Helper methods --
400+
401+
private boolean isCompatibleType(final LazyObjects<? extends E> c,
402+
final Class<?> type)
403+
{
404+
if (type == All.class) return true;
405+
final Class<?> cType = c.getType();
406+
return cType.isAssignableFrom(type) || type.isAssignableFrom(cType);
407+
}
408+
392409
// -- Helper classes --
393410

394411
private static class All {

0 commit comments

Comments
 (0)