项目已弃用
长话短说,优化思路仍然适用,但 Forge/NeoForge 不允许模组修改 EventBus。
与 Minecraft 或 Forge 自身的类不同,EventBus 并非作为游戏的一部分加载,而是作为“库”加载,这使得通过 Mixin 或 Transformer 修改 EventBus 成为不可能。
在 1.2.0 版本中,为了进一步提升性能,我使用了一些不太规范的手段(或者你更愿意称之为“非标准方法”)来修改 ASMEventHandler 类,但不幸的是这在 1.18.2+ 版本中失效了。这些版本现已归档,并且可能不会再收到更新。
FastEvent
是一个用于 Forge/Neoforge 的优化模组,旨在优化 Forge 中最基础的系统之一:事件系统。
有多快
为每个支持的 Minecraft 版本制作测试用例并不容易,尤其是因为这些版本的事件系统各不相同。因此,我将提供我在 Cleanroom 项目中提交的一个 PR 里的 JMH 基准测试报告,其中使用了与 FastEvent 相同的优化方法:
注册 10,000 个事件监听器,发布事件 0 次:
Benchmark Mode Cnt Score Error Units
BusPerformanceTest.register10000Legacy avgt 5 1126.498 ± 284.633 ms/op
BusPerformanceTest.register10000Modern avgt 5 1058.961 ± 173.586 ms/op
快了约 6.4%。
注册 1,000 个事件监听器,发布事件 10,000 次:
Benchmark Mode Cnt Score Error Units
BusPerformanceTest.register1000test10000Legacy avgt 5 4407.963 ± 4250.643 ms/op
BusPerformanceTest.register1000test10000Modern avgt 5 3550.578 ± 1991.352 ms/op
快了约 24%。
原始 PR
它是如何工作的
(极客技术细节警告)
当开发者使用 @EventBusSubscriber 和/或 @SubscribeEvent 订阅事件处理器时,EventBus 并不能神奇地获取到事件处理器,而只能获得一个 Method 对象。因此,最直接的方法是直接使用这个对象:method.invoke(...)。这个调用最终会通过 JVM 重定向回原始方法,从而让事件处理器在订阅后接收到事件。
但是这种方式(method.invoke(...))非常慢。为了加快速度,EventBus 会在运行时为其找到的每个方法生成事件处理器类,从而消除基于反射调用的昂贵开销。
然而,生成类会引入另一种减速。为了进一步加速,FastEvent 用 Lambda 表达式构造取代了类生成,从而加快了事件处理器的构建速度。另一个好处是 Lambda 是“隐藏”的,允许 JVM 执行更多的优化。
如果你恰好懂一点 Java,下面的代码示例可能会让你更直观地理解:
class Listen {
public void onEvent(Event event) {
}
}
Listen lis = new Listen();
// EventBus 会为每个事件处理器生成一个新类
class IEventListener$Listen$onEvent implements IEventListener {
private Listen instance;
public IEventListener$Listen$onEvent(Listen instance) {
this.instance = instance;
}
@Override
public void invoke(Event event) {
instance.onEvent(event);
}
}
IEventListener handler = new IEventListener$Listen$onEvent(lis);
// FastEvent 使用 Lambda 来生成事件处理器
IEventListener handler = lis::onEvent;
Project Abandoned
Long story short, the optimization approach still applies, but Forge/NeoForge does not allow mod to modify EventBus.
Unlike classes from Minecraft or Forge itself, EventBus is loaded not as part of the game, but as "library", which makes it impossible to modify EventBus via Mixin or Transformer.
In 1.2.0, in an effort of further improving performance, I used some dirty hack (or "non-standard approach" if you prefer) to modify the ASMEventHandler class, which is unfortunately broken in 1.18.2+. These releases are archived now, and are likely not receiving further update.
FastEvent
is a Forge/Neoforge optimization mod that optimizes one of the most fundamental systems in Forge: the Event system.
How Fast
It's not easy to make a test case for every supported Minecraft version, especially when the Event system for these versions are different from each other. So instead, I will provide a JMH benchmark report from a PR I made for Cleanroom project, in which I used the same optimization approach as FastEvent:
Register 10,000 event listeners, post event 0 times:
Benchmark Mode Cnt Score Error Units
BusPerformanceTest.register10000Legacy avgt 5 1126.498 ± 284.633 ms/op
BusPerformanceTest.register10000Modern avgt 5 1058.961 ± 173.586 ms/op
About 6.4% faster.
Register 1,000 event listeners, post event 10,000 times:
Benchmark Mode Cnt Score Error Units
BusPerformanceTest.register1000test10000Legacy avgt 5 4407.963 ± 4250.643 ms/op
BusPerformanceTest.register1000test10000Modern avgt 5 3550.578 ± 1991.352 ms/op
About 24% faster.
Original PR
How does it work
(Nerdy technical details alert)
When devs are using @EventBusSubscriber and/or @SubscribeEvent to subscribe event handlers, the EventBus cannot get the event handler magically, instead only a Method object is available. So the most straight-forward approach is use this object directly: method.invoke(...). This invocation will eventually be redirected by JVM back to the original method, allowing an event handler to receive an event after subscribing to it.
But this (method.invoke(...)) is really slow. To make it faster, the EventBus will, at runtime, generate event handler classes for every method it found, eliminating expensive reflection based invocation.
But generating classes introduces another slow-down. To make it even faster, FastEvent replaced class generation with lambda construction, speeding up event handler construction. Another benefit is that lambda is "hidden", allowing JVM to perform more optimization.
If you happen to know a bit Java, code examples below might be more intuitive for you:
class Listen {
public void onEvent(Event event) {
}
}
Listen lis = new Listen();
// EventBus will generate a new class for every event handler
class IEventListener$Listen$onEvent implements IEventListener {
private Listen instance;
public IEventListener$Listen$onEvent(Listen instance) {
this.instance = instance;
}
@Override
public void invoke(Event event) {
instance.onEvent(event);
}
}
IEventListener handler = new IEventListener$Listen$onEvent(lis);
// FastEvent uses lambda to generate event handler
IEventListener handler = lis::onEvent;
下载 FastEvent
下载 FastEvent
下载 FastEvent
下载 FastEvent
下载 FastEvent
下载 FastEvent
下载 FastEvent
下载 FastEvent
下载 FastEvent
下载 FastEvent
移除对非通用事件(即几乎所有事件)的通用过滤器检查
关于这个资源的讨论、问题反馈与使用心得
还没有讨论,来发第一帖吧
你是这个资源的作者?登录后可认领此资源。
相似资源
模组
缓冲的原始输入和线程化的事件轮询
模组
原版世界生成优化模组
模组
一堆修复
模组
专为独立/集成服务器设计的优化模组。
模组
使用异步路径追踪来隐藏不可见的方块/实体
模组
一款旨在提升 Minecraft 区块性能的 Fabric 模组。
评论 0
登录后参与评分
还没有评论,来抢沙发吧