You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ByteBuddy动态生成类性能不及手动实现问题排查与解决

问题与解决:ByteBuddy生成Entity子类的性能优化

问题背景

我使用ByteBuddy为Entity接口的子类实现了hasComponent(KClass): Boolean和<T> findComponentOrNull(KClass): T?方法,功能可正常运行,但性能仅为手动实现版本的一半。

基准测试结果

注:代码生成基准测试未包含字节码生成时间

Benchmark                                                     Mode  Cnt          Score        Error  Units
CodegenBranchEntityBenchmark.findComponentOrNullBenchmark    thrpt    5   93353925.859 ± 626597.466  ops/s
ManualBranchEntityBenchmark.findComponentOrNullBenchmark     thrpt    5  213006529.660 ± 638025.482  ops/s
MapLookupEntityBenchmark.findComponentOrNullBenchmark        thrpt    5  168355957.091 ± 633810.959  ops/s

排查过程

对比生成实现与手动实现的字节码,两者几乎完全一致,我曾猜测生成的类未被JIT处理。唯一显著差异是Kotlin编译器生成了PropertyReference1Impl,但这不应造成如此大的性能差距。

解决方法

将findComponentOrNull的参数改为标准Java Class(可跳过调用JvmClassMappingKt#getJavaClass)后,性能与手动实现一致。推测JIT未对动态加载类型的KClass进行优化,具体原因未知,但功能现已正常。


内容的提问来源于stack exchange,提问作者user13868280

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 13:50:34