-
-
Notifications
You must be signed in to change notification settings - Fork 37
[6.2] New aproach to optimizing cancelable events #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LexManos
wants to merge
6
commits into
MinecraftForge:6.2.x
Choose a base branch
from
LexManos:cancelables
base: 6.2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5534c01
Fix off by one in ParallelEventTest
LexManos 0ea103b
New approach to optimizing the cancelability of events
LexManos 8c5b7cd
Slight optimization to prevent temporary instance from being made.
LexManos d42aefc
Add same optimization design to lambda based listeners.
LexManos 70d1279
Small style cleanup
LexManos eca493b
Address some of Paints feedback
LexManos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
80 changes: 80 additions & 0 deletions
80
.../src/test/java/net/minecraftforge/eventbus/test/general/ParentInheritsCancelableTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (c) Forge Development LLC | ||
* SPDX-License-Identifier: LGPL-2.1-only | ||
*/ | ||
|
||
package net.minecraftforge.eventbus.test.general; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.function.Consumer; | ||
import java.util.function.Supplier; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import net.minecraftforge.eventbus.api.BusBuilder; | ||
import net.minecraftforge.eventbus.api.Cancelable; | ||
import net.minecraftforge.eventbus.api.Event; | ||
import net.minecraftforge.eventbus.api.EventPriority; | ||
import net.minecraftforge.eventbus.api.IEventBus; | ||
import net.minecraftforge.eventbus.api.SubscribeEvent; | ||
import net.minecraftforge.eventbus.test.ITestHandler; | ||
|
||
public class ParentInheritsCancelableTest implements ITestHandler { | ||
@Override | ||
public void test(Consumer<Class<?>> validator, Supplier<BusBuilder> builder) { | ||
validator.accept(SuperEvent.class); | ||
|
||
IEventBus bus = builder.get().build(); | ||
|
||
// Register a Non-Cancelable ASMEventHandler to the SuperEvent. Which should result in an Unchecked handler being registered | ||
Listener listener = new Listener(); | ||
bus.register(listener); | ||
|
||
// Test that it gets invoked | ||
bus.post(new SuperEvent()); | ||
assertTrue(listener.invoked, "Handler was not invoked for SuperEvent"); | ||
listener.invoked = false; | ||
|
||
// Now we classload the Cancelable event | ||
validator.accept(SubEvent.class); | ||
|
||
// Lambda handler should be invoked, and so should the normal listener | ||
AtomicBoolean handled = new AtomicBoolean(false); | ||
bus.addListener(EventPriority.NORMAL, false, SubEvent.class, (SubEvent event) -> { | ||
handled.set(true); | ||
}); | ||
|
||
bus.post(new SubEvent()); | ||
assertTrue(listener.invoked, "Handler was not invoked for SubEvent"); | ||
listener.invoked = false; | ||
assertTrue(handled.getAndSet(false), "Lambda Handler was not invoked for SubEvent"); | ||
|
||
// And finally lets add a listener that cancels the event, its registered after the first lambda, so that should be called, but it cancels so the ASMEventHandler in Super shouldn't be | ||
AtomicBoolean canceled = new AtomicBoolean(false); | ||
bus.addListener(EventPriority.NORMAL, false, SubEvent.class, (SubEvent event) -> { | ||
canceled.set(true); | ||
event.setCanceled(true); | ||
}); | ||
|
||
|
||
bus.post(new SubEvent()); | ||
assertTrue(handled.get(), "Lambda Handler was not invoked for SubEvent"); | ||
assertTrue(canceled.get(), "Canceling Handler was not invoked for SubEvent"); | ||
assertFalse(listener.invoked, "Handler was invoked for canceled SubEvent"); | ||
} | ||
|
||
public static class SuperEvent extends Event {} | ||
|
||
@Cancelable | ||
public static class SubEvent extends SuperEvent {} | ||
|
||
public static class Listener { | ||
private boolean invoked = false; | ||
|
||
@SubscribeEvent | ||
public void listener(SuperEvent event) { | ||
invoked = true; | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This causes wrapping still as the
IEventListener
is first created as an anon class on line 241 and then wrapped inReactiveEventListener.Unchecked
.Been a while since I last checked, but I think anon classes that capture a local var like this have their backing field trusted by MS OpenJDK, as it's smart enough to figure out that you can't reflect on the field easily.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ya, lambdas can be implemented in multiple ways by the compiler, These anon classes being one of them.
The reason i wanted to have this as an anon class is because the toString method in current implementation is completely useless. So when we have errors in events, it literally just shows that its all EventBus lambda listeners. This way it points to the lambda passed in by the modder which should have their class name in it and thus we have a far easier time debugging it.
Even if it is slightly slower, which I doubt, its worth it. Unless its like a 50x difference.