the union of many small lists is expensive
The reason seems to be the number of memory allocations. In order to create the union of 100 lists we have 99 memory allocations. The first needs the space for the first two lists, the second the space for the first three lists, and so on. We can reduce the number of allocations drastically (in many cases to one) by leveraging the fact that many of the lists were already sorted, non-overlapping and increasing, so that we can simply concatenate them.
This commit is contained in:
@@ -30,6 +30,8 @@ public class Preconditions {
|
||||
* @param b
|
||||
* @param message formatted with {@link MessageFormat}
|
||||
* @param args
|
||||
* @throws IllegalStateException if {@code a} is not greater or equal to
|
||||
* {@code b}
|
||||
*/
|
||||
public static void checkGreaterOrEqual(final long a, final long b, final String message, final Object... args) {
|
||||
if (a < b) {
|
||||
@@ -49,6 +51,8 @@ public class Preconditions {
|
||||
* @param expected the expected value
|
||||
* @param message formatted with {@link MessageFormat}
|
||||
* @param args arguments for the message
|
||||
* @throws IllegalStateException if {@code actual} is not equal to
|
||||
* {@code expected}
|
||||
*/
|
||||
public static void checkEqual(final Object actual, final Object expected, final String message,
|
||||
final Object... args) {
|
||||
@@ -58,6 +62,18 @@ public class Preconditions {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the given value is true.
|
||||
*
|
||||
* @param actual must be true
|
||||
* @param message formatted with {@link MessageFormat}
|
||||
* @param args arguments for the message
|
||||
* @throws IllegalStateException if {@code actual} is not true
|
||||
*/
|
||||
public static void checkTrue(final boolean actual, final String message, final Object... args) {
|
||||
checkEqual(actual, true, message, args);
|
||||
}
|
||||
|
||||
public static void checkNull(final Object actual, final String message, final Object... args) {
|
||||
if (actual != null) {
|
||||
throw new IllegalStateException(MessageFormat.format(message, args));
|
||||
|
||||
Reference in New Issue
Block a user