Azure / azure-cosmosdb-java

Java Async SDK for SQL API of Azure Cosmos DB
MIT License
54 stars 61 forks source link

memory improvements (cherry pick of #230 to master) #248

Closed moderakh closed 5 years ago

moderakh commented 5 years ago

This is the cherry pick of this PR (https://github.com/Azure/azure-cosmosdb-java/pull/230 for 2.4.6 hotfix release) onto master.

description of the cherry picked PR: https://github.com/Azure/azure-cosmosdb-java/pull/230 This PR Includes the following memory improvements.

(The following numbers are based on running benchmark tool on 16cpu core Linux VM with ReadThroughput workload and 4000 as degree of concurrency.)

1) when reading from network wire (through netty ByteBuf) we used to copy data once to ByteArrayOutputStream and from that we created String. Later ByteArrayOutputStream will get garbage collected, on high load this can lead to GCing 20-30% unnecessary data. The improvement is to pool the buffers.

2) we were deserializing replica physicalAddress in the AddressSelector to URI per request, we can cache the parsed URI form of the replica physical Address, also in the HttpTransportClient we were using URI for constructing paths which consumes more memory comparing to pure String. This somewhat contributed 5-10% memory which gets GCed.

3) we were decoding/encoding UUID to String unnecessarily. 2% memory improvement:

4) in auth token generation we are using StringBuilder which grows (it copies data on each grow) as we append to it. This is improved by preserving memory 1-2% memory improvement:

christopheranderson commented 5 years ago

Also needs manual port to azure-sdk-for-java (v3)