optimize performance : Hessian2Input.readObject() for 'B' or 'b' #120
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
这是一个对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%)