mybatis / mybatis-3

MyBatis SQL mapper framework for Java
http://mybatis.github.io/mybatis-3/
Apache License 2.0
19.55k stars 12.75k forks source link

(fix): Only execute certain operations once while handling results #3116

Closed epochcoder closed 3 months ago

epochcoder commented 3 months ago

While creating benchmarks and profiling for the constructor injection ticket, I stumbled upon two cases which could be optimised, and both reduce memory allocation and runtime for very large result sets.

I did not want to include this in the previous PR as it is already huge, and these two improvements can be standalone. Here are the benchmarking results using the same benchmark I created for the previously mentioned ticket.

Benchmark                                                                        Mode  Cnt  Score   Error  Units
PropertyVsConstructorInjectionBenchmark.retrieveAllUsingConstructorInjection     avgt    5  0.817 ± 0.036  ms/op
PropertyVsConstructorInjectionBenchmark.retrieveAllUsingPropertyInjection        avgt    5  1.067 ± 0.013  ms/op
PropertyVsConstructorInjectionBenchmark.retrieveSingleUsingConstructorInjection  avgt    5  0.046 ± 0.003  ms/op
PropertyVsConstructorInjectionBenchmark.retrieveSingleUsingPropertyInjection     avgt    5  0.037 ± 0.001  ms/op

For easy reference, here are the same benchmarks without this change:

Benchmark                                                                        Mode  Cnt  Score   Error  Units
PropertyVsConstructorInjectionBenchmark.retrieveAllUsingConstructorInjection     avgt    5  0.834 ± 0.015  ms/op
PropertyVsConstructorInjectionBenchmark.retrieveAllUsingPropertyInjection        avgt    5  1.164 ± 0.055  ms/op
PropertyVsConstructorInjectionBenchmark.retrieveSingleUsingConstructorInjection  avgt    5  0.045 ± 0.001  ms/op
PropertyVsConstructorInjectionBenchmark.retrieveSingleUsingPropertyInjection     avgt    5  0.038 ± 0.002  ms/op

As you can see, it does not really affect retrieving a single row, but improvements start to accumulate as our result set gets bigger

coveralls commented 3 months ago

Coverage Status

coverage: 87.118% (+0.001%) from 87.117% when pulling 7e032612b7e1055497535303a42b1ed75239cef9 on epochcoder:fix/minor-performance-improvement-resultsethandler into 0ec8fcbf0c3d0ef11241f8880ee10adaa2f6fbba on mybatis:master.