Use a ConcurrentHashMap instead of a TreeMap but still sort via priority.#2514
Merged
Yesssssman merged 5 commits intoAntikythera-Studios:1.21.1from Apr 4, 2026
Merged
Conversation
Collaborator
|
There are compile errors should be fixed according to this change in |
Collaborator
Thread safety requirementThe subscriber container should be thread-safe since NeoForge loads mods asynchronously, if event registration is called in the mod's constructor it will cause Tested by custom multi threading code: public EpicFightMod(IEventBus modEventBus, ModContainer modContainer) {
...
Thread t1 = new Thread(() -> {
MutableInt mi = new MutableInt(0);
for (int i = 0; i < 50; i++) {
EpicFightEventHooks.Registry.ENTITY_PATCH.registerEvent(event -> {
System.out.println(mi.getAndIncrement() + "'th entity patch event");
}, i);
}
});
Thread t2 = new Thread(() -> {
MutableInt mi = new MutableInt(1000);
for (int i = 1000; i < 1050; i++) {
EpicFightEventHooks.Registry.ENTITY_PATCH.registerEvent(event -> {
System.out.println(mi.getAndIncrement() + "'th entity patch event");
}, i);
}
});
Thread t3 = new Thread(() -> {
MutableInt mi = new MutableInt(10000);
for (int i = 10000; i < 10050; i++) {
EpicFightEventHooks.Registry.ENTITY_PATCH.registerEvent(event -> {
System.out.println(mi.getAndIncrement() + "'th entity patch event");
}, i);
}
});
t3.start();
t1.start();
t2.start();
}Output: Due to the registration failing while running the thread, the event logs end in a random sequence. Performance OptimizationInstead of sorting event priorities each time an event hook is called, we should pre-order the events so that the event hook just calls subscribers in order. Tested code on PR (sort on trigger) vs local test code (sort on register) public T post(T event) {
long begin = System.currentTimeMillis();
for (var subscriberList : this.subscribers.values()) {
for (var subscriber : subscriberList) {
if (subscriber.subscriptionType() instanceof DefaultEventSubscription<T> passiveSubscription) {
passiveSubscription.fire(event);
}
}
}
long took = System.currentTimeMillis() - begin;
System.out.println("ConcurrentSkipListMap + CopyOnWriteArrayList event call: " + (took));
long begin2 = System.currentTimeMillis();
List<EventListener<T>> buffer = Lists.newArrayList();
this.subscribers.values().forEach(buffer::addAll);
Comparator<EventListener<T>> eventListeners = Comparator.comparing(EventListener::priority);
buffer.stream().sorted(eventListeners.reversed()).forEach(listener -> {
if (listener.subscriptionType() instanceof DefaultEventSubscription<T> passiveSubscription) {
passiveSubscription.fire(event);
}
});
long took2 = System.currentTimeMillis() - begin2;
System.out.println("concurrent map + arraylist event call: " + (took2));
return event;
}Output ConcurrentSkipListMap + CopyOnWriteArrayList event call: 187
concurrent map + arraylist event call: 690p.s. The sort order of event subscribers is descending |
Yesssssman
requested changes
Apr 2, 2026
neoforge/src/main/java/yesman/epicfight/api/event/EventHook.java
Outdated
Show resolved
Hide resolved
neoforge/src/main/java/yesman/epicfight/api/event/EventHook.java
Outdated
Show resolved
Hide resolved
Taking in sugestions Co-authored-by: Yesman <79469058+Yesssssman@users.noreply.github.com>
Yesssssman
approved these changes
Apr 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is still a draft but feedback is still needed.