Skip to content

Commit c8cdb02

Browse files
committed
GH-1160 - 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 cb8e82a commit c8cdb02

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

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

Lines changed: 14 additions & 11 deletions
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,14 @@ 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+
}
369372
}
370373
}
371374
}

0 commit comments

Comments
 (0)