optimize performance : Hessian2Input.readObject() for 'B' or 'b'#120
optimize performance : Hessian2Input.readObject() for 'B' or 'b'#120chjk wants to merge 4 commits intosofastack:3.xfrom
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
这是一个对Hessian读取大byte[]的性能优化。读取耗时较优化前降低80%以上。
【优化原理】
原始读取逻辑:每次从流中读取256字节,然后处理后,放入一个新的ByteArrayOutputStream,如此循环后,最后再用ByteArrayOutputStream生成一个大的byte[]
本次优化:
1、对于只有一个chunk(总字节长度小于等于32768)的字节数组,不需要额外生成ByteArrayOutputStream了,而是直接读取chunk头上的字节长度,一次性生成固定长度的byte[]。然后将buffer中已经读取的一部分先写入到byte[]中,最后计算剩余应该从is流中读取的字节长度,使用_is.read(目标数组, 目标数组偏移量, 读取长度)的方法,一次性读取。
2、对于有多个chunk的字节数组,每个chunk使用上述步骤一次性读取,然后放入到ByteArrayOutputStream,多个chunk读取完成后,通过ByteArrayOutputStream输出拼接的byte[]
数据正确性测试:Hessian2InputTest.testSerialize()
简单的性能测试:Hessian2InputTest.testSerializeTime()
MacBook M1,测试结果如下:
单chunk的情况:反序列化byte[1024], 10000次:
优化前:56ms
优化后:36ms
单chunk的情况:反序列化byte[10*1024], 10000次:
优化前:312ms
优化后:40ms
单chunk的情况:反序列化byte[32000], 10000次:
优化前:824ms (随字节长度线性增长)
优化后:54ms (几乎无增长)
多chunk的情况:反序列化byte[1024*1024], 10000次:
优化前:24609ms
优化后:2225ms (降低90%)