diff --git a/pom.xml b/pom.xml index 8a4ea56..da6d405 100755 --- a/pom.xml +++ b/pom.xml @@ -259,26 +259,11 @@ Builds the benchmarks with CloudSim Plus 8.5.1, to compare it with the current version (e.g., to measure memory before the changes from cloudsimplus#586 using ListenersMemoryBenchmark): ./mvnw clean package -Pcloudsimplus-8 - Benchmarks that use classes only available in CloudSim Plus 9 are excluded. --> cloudsimplus-8 8.5.1 - - - - org.apache.maven.plugins - maven-compiler-plugin - - - - org/cloudsimplus/listeners/ListenersNotificationBenchmark.java - - - - - diff --git a/src/main/java/org/cloudsimplus/listeners/ListenersNotificationBenchmark.java b/src/main/java/org/cloudsimplus/listeners/ListenersNotificationBenchmark.java index ce662a4..0d47c3c 100644 --- a/src/main/java/org/cloudsimplus/listeners/ListenersNotificationBenchmark.java +++ b/src/main/java/org/cloudsimplus/listeners/ListenersNotificationBenchmark.java @@ -25,14 +25,16 @@ import org.openjdk.jmh.annotations.*; -import java.util.Arrays; import java.util.List; import java.util.concurrent.TimeUnit; /// Measures the time and memory to notify listeners stored in: -/// - an immutable [List] created by [Listeners] (as CloudSim Plus does) and +/// - an immutable [List] created by `List.of(array)` (as CloudSim Plus does) and /// - a plain array, which is the alternative being assessed. /// +/// The lists are created directly by `List.of(array)`, instead of using the CloudSim Plus class that stores listeners, +/// so that the benchmark compiles against different CloudSim Plus versions. +/// /// A for-each loop over a [List] calls [List#iterator()], which creates an object, /// unless the JIT removes it using escape analysis. A for-each loop over an array creates no object. /// Run with the GC profiler (`-prof gc`) and check `gc.alloc.rate.norm` (bytes per operation) @@ -77,8 +79,8 @@ public static class SingleSize { @Setup(Level.Trial) public void setup(final Counter counter) { - list = newList(listenersCount, counter); - array = toArray(list); + array = newListeners(listenersCount, counter); + list = List.of(array); } } @@ -99,8 +101,8 @@ public void setup(final Counter counter) { lists = new List[MAX_LISTENERS + 1]; arrays = new EventListener[MAX_LISTENERS + 1][]; for (int count = 0; count <= MAX_LISTENERS; count++) { - lists[count] = newList(count, counter); - arrays[count] = toArray(lists[count]); + arrays[count] = newListeners(count, counter); + lists[count] = List.of(arrays[count]); } } } @@ -147,20 +149,17 @@ private static void notifyListeners(final EventListener[] listeners) } } - /// Creates a list with the given number of listeners, using [Listeners#add(List, Object)]. + /// Creates an array with the given number of listeners. /// Each listener is a different lambda instance, since it captures its own id. - private static List> newList(final int listenersCount, final Counter counter) { - List> list = List.of(); + /// The callers create a list with `List.of(array)`, which copies the array, so the list and the array don't share storage. + @SuppressWarnings("unchecked") + private static EventListener[] newListeners(final int listenersCount, final Counter counter) { + final EventListener[] listeners = new EventListener[listenersCount]; for (int i = 0; i < listenersCount; i++) { final int id = i + 1; - list = Listeners.add(list, info -> counter.calls += id); + listeners[i] = info -> counter.calls += id; } - return list; - } - - @SuppressWarnings("unchecked") - private static EventListener[] toArray(final List> list) { - return Arrays.copyOf(list.toArray(), list.size(), EventListener[].class); + return listeners; } }