Skip to content

Conversation

apple502j
Copy link
Contributor

Should be self-descriptive.

Although this is cool, there is no reason to
keep this as part of shipped code.
@apple502j apple502j added the enhancement New feature or request label Jun 26, 2024
Copy link
Member

@modmuss50 modmuss50 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some tests.

@apple502j apple502j requested a review from modmuss50 July 6, 2024 02:40
@modmuss50 modmuss50 added the status: last call If you care, make yourself heard right away! label Jul 6, 2024
*
* @return whether the event has any listeners
*/
public boolean hasListener() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this is a bad pattern. If you are concerned about allocation, reuse the context object. In typical modpacks, most events will be used and these checks will be pointless anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats a fair point, @apple502j did you have a specific use case in mind?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A usecase I had for this was for a deprecated event, where actually properly servicing it required setting up some expensive stuff

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, not all context objects can be reused.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you link some specific examples?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/BasiqueEvangelist/ne-v-seti/blob/sanity-1.17/src/main/java/me/basiqueevangelist/nevseti/OfflineDataCache.java#L30
I guess I misremembered specifically how much it would take to service this event, but I think it will still be useful for events that are deprecated and require running extra logic to run their invokers

@apple502j apple502j requested a review from modmuss50 July 20, 2024 18:27
*
* @return whether the event has any listeners
*/
public boolean hasListener() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not resolved, please don't mark it as resolved.

@modmuss50
Copy link
Member

Still wating on a good usecase for this, in places where it matter we already re-use a context object, and firing an event with no listeners should have little overhead.

@apple502j
Copy link
Contributor Author

I think I had some events in my mind when I filed this PR, I need to figure that out again.

@apple502j
Copy link
Contributor Author

Events with non-zero invocation cost

  • RegSync: Remap event constructs RemapStateImpl. Very negligible as this is called only once per connection.
  • ItemGroupEvents: Involves creation of several objects. Once per startup, so doesn't really matter, unless Mojang decuples the number of items or something.
  • LootTableEvents: This might benefit from a check. An event (invoked per loot table) requires LootTable to be converted back to builder.

Will do a more thorough research later.

@melontini
Copy link

LootTableEvents: This might benefit from a check. An event (invoked per loot table) requires LootTable to be converted back to builder.

IMO this would only benefit if there was a separation between singular and wildcard events (like MODIFY_ALL, modify(RegistryKey) or modify(Predicate<LootTable>)).

With the current implementation, if a single mod wants to modify a single loot table, all the benefits of such a check disappear.

@Juuxel
Copy link
Member

Juuxel commented Aug 4, 2024

modify(Identifier) or modify(RegistryKey<LootTable>) would actually make sense for the API since most modifications are probably targeted to just a single loot table.

@modmuss50 modmuss50 removed the status: last call If you care, make yourself heard right away! label Oct 15, 2024
@ishland
Copy link

ishland commented Apr 25, 2025

When a synchronous event that's only meant to be invoked from the server thread needs to be invoked from a worker thread, it needs to make a round-trip to the server thread. Such round-trip can take 50ms or more depending on the MSPT of the server.
Wasting that amount of time invoking an event that nobody listens to is just a waste of time. Having such API can avoid doing such costly round-trips.

I encountered this when implementing #4541 where I have to make round-trips to the server thread to invoke the event:
RelativityMC/C2ME-fabric@0a04fdc
There's one such case in the ServerBlockTicking class and another two in ServerEntityTicking.

@Technici4n
Copy link
Member

Wouldn't this indicate a flaw with the event design? The moment any mod registers a listener, all the benefits of C2ME will be gone, no? @ishland

@ishland
Copy link

ishland commented Apr 25, 2025

Wouldn't this indicate a flaw with the event design?

I can't think of any better designs other than splitting that event into 6 events. Relaxing where the event can be invoked just adds more trouble for modders that have no idea about threading.

The moment any mod registers a listener, all the benefits of C2ME will be gone, no?

No. Multiple pending synchronizations with the server thread can be in flight at the same time. But adding extra server thread synchronization points (for just invoking the event) adds more server thread overhead and increases latency.

@modmuss50
Copy link
Member

I dont have any major concers with this, the use case seems good enough to me. The PR will need updating before moving forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants