aws / aws-sdk-java-v2

The official AWS SDK for Java - Version 2
Apache License 2.0
2.2k stars 853 forks source link

Avoid growing copies of collections of known size #5551

Closed sugmanue closed 2 months ago

sugmanue commented 2 months ago

Motivation and Context

Improves the performance of building collections by allocating the expected size beforehand instead of letting the collection grow as we add elements. This trivial change has small, but noticeable, performance improvements in the benchmarks for RPCv2, but will also benefit other protocols

Before

Benchmark                              (protocol)  (size)  Mode  Cnt      Score     Error  Units
JsonMarshallerBenchmark.unmarshall  smithy-rpc-v2   small  avgt    5   1944.436 ±  21.069  ns/op
JsonMarshallerBenchmark.unmarshall  smithy-rpc-v2  medium  avgt    5   3209.297 ± 259.975  ns/op
JsonMarshallerBenchmark.unmarshall  smithy-rpc-v2     big  avgt    5  11061.383 ± 187.669  ns/op

And flamegraph snippet below

before

After

Benchmark                              (protocol)  (size)  Mode  Cnt      Score     Error  Units
JsonMarshallerBenchmark.unmarshall  smithy-rpc-v2   small  avgt    5   1821.490 ±  18.858  ns/op
JsonMarshallerBenchmark.unmarshall  smithy-rpc-v2  medium  avgt    5   3045.257 ± 239.893  ns/op
JsonMarshallerBenchmark.unmarshall  smithy-rpc-v2     big  avgt    5  10593.384 ± 113.698  ns/op

And flamegraph snippet below

after

Modifications

Testing

Screenshots (if appropriate)

Types of changes

Checklist

License

sonarcloud[bot] commented 2 months ago

Quality Gate Failed Quality Gate failed

Failed conditions
41.4% Coverage on New Code (required ≥ 80%)
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarCloud

Catch issues before they fail your Quality Gate with our IDE extension SonarLint