We found out that ArrayList.addAll(...) method doesn't check if the given collection is not empty. It means we will re-allocate memory (System.arrayCopy(...) will be called) even if we don't actually need it.
Shoud we add an IF check for an optimization?
For example: elapsed time for the first code is more than 8 times faster than the second one. It seems like our optimization works properly, so why it wasn't implemented already?
List<Integer> existed = new ArrayList<>();
List<Integer> empty = new ArrayList<>();
for (int i = 0; i < 100_000_000; i++) {
if(!empty.isEmpty())
existed.addAll(empty);
}
VS
List<Integer> existed = new ArrayList<>();
List<Integer> empty = new ArrayList<>();
for (int i = 0; i < 100_000_000; i++) {
existed.addAll(empty);
}