Skip to content

Commit dc81a74

Browse files
committed
GH-1146 - Improve lookup of event publications in progress.
Instead of iterating over all publications currently in progress we now use a map to look them up by key. Baseline -------- Benchmark Mode Cnt Score Error Units ….inProgressPublicationsAccess thrpt 50 312,155 ± 7,428 ops/s Fixed ----- Benchmark Mode Cnt Score Error Units ….inProgressPublicationsAccess thrpt 50 2328732,504 ± 7809,092 ops/s
1 parent dc67a11 commit dc81a74

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

spring-modulith-events/spring-modulith-events-core/src/main/java/org/springframework/modulith/events/core/DefaultEventPublicationRegistry.java

+48-11
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@
1919
import java.time.Duration;
2020
import java.time.Instant;
2121
import java.util.Collection;
22-
import java.util.HashSet;
2322
import java.util.Iterator;
23+
import java.util.Map;
2424
import java.util.Optional;
25-
import java.util.Set;
2625
import java.util.concurrent.ConcurrentHashMap;
2726
import java.util.function.Consumer;
2827
import java.util.function.Predicate;
@@ -296,7 +295,7 @@ private static String getConfirmationMessage(Collection<?> publications) {
296295
*/
297296
static class PublicationsInProgress implements Iterable<TargetEventPublication> {
298297

299-
private final Set<TargetEventPublication> publications = ConcurrentHashMap.newKeySet();
298+
private final Map<Key, TargetEventPublication> publications = new ConcurrentHashMap<>();
300299

301300
/**
302301
* Registers the given {@link TargetEventPublication} as currently processed.
@@ -308,7 +307,7 @@ TargetEventPublication register(TargetEventPublication publication) {
308307

309308
Assert.notNull(publication, "TargetEventPublication must not be null!");
310309

311-
publications.add(publication);
310+
publications.put(new Key(publication), publication);
312311

313312
return publication;
314313
}
@@ -325,8 +324,7 @@ void unregister(Object event, PublicationTargetIdentifier identifier) {
325324
Assert.notNull(event, "Event must not be null!");
326325
Assert.notNull(identifier, "PublicationTargetIdentifier must not be null!");
327326

328-
getPublication(event, identifier)
329-
.ifPresent(publications::remove);
327+
publications.remove(new Key(event, identifier));
330328
}
331329

332330
/**
@@ -338,7 +336,7 @@ void unregister(TargetEventPublication publication) {
338336

339337
Assert.notNull(publication, "TargetEventPublication must not be null!");
340338

341-
publications.remove(publication);
339+
publications.remove(new Key(publication));
342340
}
343341

344342
/**
@@ -354,9 +352,7 @@ Optional<TargetEventPublication> getPublication(Object event, PublicationTargetI
354352
Assert.notNull(event, "Event must not be null!");
355353
Assert.notNull(identifier, "PublicationTargetIdentifier must not be null!");
356354

357-
return publications.stream()
358-
.filter(it -> it.isAssociatedWith(event, identifier))
359-
.findFirst();
355+
return Optional.ofNullable(publications.get(new Key(event, identifier)));
360356
}
361357

362358
/*
@@ -365,7 +361,48 @@ Optional<TargetEventPublication> getPublication(Object event, PublicationTargetI
365361
*/
366362
@Override
367363
public Iterator<TargetEventPublication> iterator() {
368-
return new HashSet<>(publications).iterator();
364+
return publications.values().iterator();
365+
}
366+
367+
private record Key(Object event, PublicationTargetIdentifier identifier) {
368+
369+
public Key(TargetEventPublication publication) {
370+
this(publication.getEvent(), publication.getTargetIdentifier());
371+
}
372+
373+
/*
374+
* (non-Javadoc)
375+
* @see org.springframework.modulith.events.core.DefaultEventPublicationRegistry.PublicationsInProgress.Key#equals(java.lang.Object)
376+
*/
377+
@Override
378+
public final boolean equals(Object obj) {
379+
380+
if (obj == this) {
381+
return true;
382+
}
383+
384+
if (!(obj instanceof Key that)) {
385+
return false;
386+
}
387+
388+
return this.event == that.event
389+
&& this.identifier.equals(identifier);
390+
}
391+
392+
/*
393+
* (non-Javadoc)
394+
* @see org.springframework.modulith.events.core.DefaultEventPublicationRegistry.PublicationsInProgress.Key#hashCode()
395+
*/
396+
@Override
397+
public final int hashCode() {
398+
399+
int result = 7;
400+
401+
result += 31 * System.identityHashCode(event);
402+
result += 31 * identifier.hashCode();
403+
404+
return result;
405+
}
369406
}
370407
}
371408
}

spring-modulith-events/spring-modulith-events-core/src/test/java/org/springframework/modulith/events/core/DefaultEventPublicationRegistryUnitTests.java

-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ void obtainsCorrectInProgressPublicationForIdenticalEvents() {
9797

9898
assertThat(inProgress.getPublication(firstEvent, identifier)).containsSame(first);
9999
assertThat(inProgress.getPublication(secondEvent, identifier)).containsSame(second);
100-
101100
}
102101

103102
private DefaultEventPublicationRegistry createRegistry(Instant instant) {

0 commit comments

Comments
 (0)