keva icon indicating copy to clipboard operation
keva copied to clipboard

Implement ByteArray pool allocator && String pool

Open tuhuynh27 opened this issue 4 years ago • 4 comments

Issue: There is a lot of byte[] allocated when handling TCP messages:

image

It's better to have a ByteArray pool allocator so that byte[] can be reused and avoid creating massive garbage for the GC and the throughput can be even better.

For example:

ByteArrayPoolAllocator pool = ByteArrayPoolAllocator.DEFAULT
byte[10] obj = byteArrayPool.getByteArray(10);

tuhuynh27 avatar Dec 11 '21 19:12 tuhuynh27

Same for String pool, we are converting byte[] to String like below:

String str = new String(byteArray);

So every conversion, a new String object is allocated.

Reference: Avoid creating 'new' String objects when converting a byte[] to String using a specific charset

tuhuynh27 avatar Dec 11 '21 19:12 tuhuynh27

http://tutorials.jenkov.com/mem-ops-java/index.html

tuhuynh27 avatar Dec 11 '21 21:12 tuhuynh27

Doesn't Netty provide something similar like pooled buffer?

the123saurav avatar Dec 12 '21 04:12 the123saurav

Oh yes @the123saurav Netty does have some utility for pooled object and buffer, we can utilize that

tuhuynh27 avatar Dec 12 '21 07:12 tuhuynh27