guava
guava copied to clipboard
bulk removals from sublists of transformed lists are slow
com.google.common.collect.Lists.transform(List, Function).subList(int, int).clear()
is slow when removing a series of items from middle of decorated ArrayList. java.util.List.clear()
method is intended to alleviate this problem but TransformedList fails to override. Furthermore, when working with javafx.collections.ObservableList
or similar, an excessive number of events are generated. This can lead to more severe performance implications. Issue is addressed via PR #5762.
public class TransformedListTest {
public static void main(String[] pArgs) {
Stream.of(100, 1_000, 10_000, 100_000, 1_000_000).forEach(
pCnt -> Stream.of(false, true).forEach(
pTransform -> {
List<Integer> list = IntStream.range(0, pCnt)
.boxed()
.collect(Collectors.toCollection(ArrayList::new));
if (pTransform) {
list = Lists.transform(list, i -> i);
}
final int idx1 = pCnt / 4;
final int idx2 = pCnt * 3 / 4;
final List<Integer> subList = list.subList(idx1, idx2);
final Instant start = Instant.now();
subList.clear();
final Instant end = Instant.now();
System.out.printf(
"Cleared %,d to %,d from %s list of size %,d in %s%n",
idx1,
idx2,
pTransform ? "transformed" : "standard ",
pCnt,
Duration.between(start, end)
);
}
)
);
}
}
Cleared 25 to 75 from standard list of size 100 in PT0S
Cleared 25 to 75 from transformed list of size 100 in PT0.0019993S
Cleared 250 to 750 from standard list of size 1,000 in PT0S
Cleared 250 to 750 from transformed list of size 1,000 in PT0.0010005S
Cleared 2,500 to 7,500 from standard list of size 10,000 in PT0S
Cleared 2,500 to 7,500 from transformed list of size 10,000 in PT0.0029997S
Cleared 25,000 to 75,000 from standard list of size 100,000 in PT0S
Cleared 25,000 to 75,000 from transformed list of size 100,000 in PT0.2990039S
Cleared 250,000 to 750,000 from standard list of size 1,000,000 in PT0.0029991S
Cleared 250,000 to 750,000 from transformed list of size 1,000,000 in PT1M12.1428634S