diff --git a/src/main/java/io/usethesource/vallang/impl/persistent/PersistentHashIndexedBinaryRelation.java b/src/main/java/io/usethesource/vallang/impl/persistent/PersistentHashIndexedBinaryRelation.java index 047a6892..f9c93622 100644 --- a/src/main/java/io/usethesource/vallang/impl/persistent/PersistentHashIndexedBinaryRelation.java +++ b/src/main/java/io/usethesource/vallang/impl/persistent/PersistentHashIndexedBinaryRelation.java @@ -634,7 +634,7 @@ private static SetMultimap.Transient computeClosureDepthFirst(fi final IValue lhs = focus.getKey(); final Object values =focus.getValue(); - todo.clear(); + assert todo.isEmpty(); if (values instanceof IValue) { todo.push((IValue)values); } @@ -644,11 +644,18 @@ else if (values instanceof Set) { else { throw new IllegalArgumentException("Unexpected map entry"); } - // we mark ourselves as done before we did it, - // so we don't do the that causes an extra round + // to avoid recalculating `lhs` next time we see it, we mark it as done. + // so that the next time we come across if on the rhs, we know the range is + // already the transitive closure. + // We add it before we've done it, just to avoid scheduling + // when it occurs during the depth scan for lhs. done.add(lhs); IValue rhs; while ((rhs = todo.poll()) != null) { + if (lhs == rhs) { + // no need to handle + continue; + } boolean rhsDone = done.contains(rhs); for (IValue composed : result.get(rhs)) { if (result.__insert(lhs, composed) && !rhsDone) {