Jasione
概述
Jasione 是一款优化模组,旨在减少内存分配(降低 GC 压力)。
Java 编译器会为每个枚举类生成一个 values() 方法,可用于将所有枚举值作为数组获取。该方法在返回前会对数组进行克隆,以确保调用者可以安全地修改该数组而不影响其他调用者。然而,这会产生性能开销。当在高频路径中调用时,这些微小的内存分配会累积并增加 GC 压力。
Jasione 通过将不修改返回数组的 values() 调用重定向到使用缓存来解决此问题,从而消除克隆开销和内存分配。有关如何安全实现此功能的详细信息,请参阅下方的“技术细节”部分。
配置
Jasione 目前提供几个调试选项,可用于检查优化结果。配置文件位于 config/jasione.toml。
#指定是否启用模组
enabled = true
[debug]
#启用后,在应用优化时记录统计信息
printOptimization = false
#启用后,将转换后的类转储到 ".jasione.out" 文件夹
dumpClasses = false
常见问题
JIT 不是在 Enum#values() 调用安全时会对其进行优化吗?
不会,JIT 编译器目前甚至无法识别通过迭代值数组等最 trivial 的使用模式。克隆和内存分配的开销依然存在,参见 JMH 测试。
NeoForge 显示关于此模组的警告。为什么?
在最新 NeoForge (1.21.11+) 上运行此模组时,您可能会遇到以下警告:
[Render thread/ERROR] [ne.ne.fm.cl.tr.ClassTransformStatistics/]: Class processor jasione:main transformed 100.00% of loaded class which is suspiciously high; it may be attempting mass-ASM. Please report this to the mod author.
根据 NeoForge 团队成员的说法,此消息本质上是对模组作者的一种提示,让他们确认这种用例是否必要。对于 Jasione,这种转换是必要的。(参见上一个问题)
参考:“The NeoForged Project” Discord 服务器上的讨论(请注意,讨论涉及不同的实现方案,由于各种原因未能成为最终版本。更多详情请见“技术细节”部分。)
技术细节
Enum#values(其中 Enum 指代任何枚举类)的契约规定,返回的数组是全新的且可以自由修改。因此,我们必须通过分析调用者是否修改了数组或数组是否逃逸,来识别哪些调用者可以安全地进行优化。借助 asm-analysis 库可以实现这一点。如果通过字节码分析检测到数组修改或逃逸,则调用保持不变以保持正确性。
我们需要找到一个地方来存储缓存的值数组,因为枚举类的合成 $VALUES 字段是私有的。当前实现执行运行时类生成。对于每个枚举类,都会生成一个对应的缓存持有者类,用于以 static final 字段的形式存储值数组。例如,当转换器首次检测到对 net.minecraft.core.Direction#values 的使用时,它会生成类 me.decce.jasione.cached.net.minecraft.core.Direction,该类包含一个缓存的值数组以及各种确保正确性的元数据。借此,它可以将原始 values() 方法的所有调用者转换为使用缓存类。
Jasione
Overview
Jasione is an optimization mod that reduces memory allocation (GC pressure).
The Java compiler generates, for each enum class, a values() method that can be used to retrieve all enum values as an array. This method clones the array before returning it, ensuring that callers of this method can safely mutate the array without affecting other callers. This, however, incurs a performance cost. When invoked in hot paths, these small memory allocations accumulate and increase GC pressure.
Jasione addresses this issue by redirecting the values() calls that do not mutate the returned array to use a cache, eliminating the cloning overhead and the memory allocation. For details on how this is implemented safely, see to the "Technical Details" section below.
Configuration
Jasione currently offers several debug options, which can be used to inspect the optimization result. The configuration resides in config/jasione.toml.
#Specifies whether to enable the mod
enabled = true
[debug]
#When enabled, logs statistics when optimizations are applied
printOptimization = false
#When enabled, dumps transformed classes to the ".jasione.out" folder
dumpClasses = false
FAQs
Doesn't the JIT optimize the Enum#values() calls when they're safe?
No, the JIT compiler doesn't currently recognize even the most trivial use patterns like iterating through the values array. The cloning and memory allocation overhead persists, per JMH tests.
NeoForge displays a warning about this mod. Why?
When running this mod on the latest NeoForge (1.21.11+), you may encounter this warning:
[Render thread/ERROR] [ne.ne.fm.cl.tr.ClassTransformStatistics/]: Class processor jasione:main transformed 100.00% of loaded class which is suspiciously high; it may be attempting mass-ASM. Please report this to the mod author.
According to members of the NeoForge team, this message essentially acts as a nudge for mod authors to identify whether the use case is necessary. For Jasione, this transformation is necessary. (See the previous question)
For reference, see discussion on "The NeoForged Project" discord server (note that the discussion involved different implementations which didn't become the final version due to various issues. See the "Technical Details" section for more detail.)
Technical Details
The contract of Enum#values (where Enum refers to any enum class) is that the returned array is fresh and can be freely mutated. Therefore, we must identify which callers are safe to optimize by analyzing whether they mutate the array or the array escapes. This is made possible by utilizing the asm-analysis library. If array mutation or escapes are detected via bytecode analysis, the call remains unchanged to preserve correctness.
We need to find a place to store the cached values array, because the synthetic $VALUES field for enum classes is private. The current implementation performs runtime class generation. For each enum class, a corresponding cache holder class is generated to store the values array as a static final field. For example, when the transformer first detects use of net.minecraft.core.Direction#values, it generates the class me.decce.jasione.cached.net.minecraft.core.Direction, which holds a cached values array and various other metadata to ensure correctness. With this, it can transform all callers of the original values() method to use the cached class.
下载 Jasione
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 Jasione
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 Jasione
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 Jasione
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 Jasione
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 Jasione
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 Jasione
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 Jasione
下载 Jasione
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 Jasione
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
- 分析器:修复了导致部分分析失败的 maxLocals 计算问题。
关于这个资源的讨论、问题反馈与使用心得
还没有讨论,来发第一帖吧
你是这个资源的作者?登录后可认领此资源。
评论 0
登录后参与评分
还没有评论,来抢沙发吧