keva-dev / keva

Low-latency in-memory key-value store, Redis drop-in alternative
https://keva.dev
Apache License 2.0
100 stars 13 forks source link

Implement ByteArray pool allocator && String pool #69

Open tuhuynh27 opened 2 years ago

tuhuynh27 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

the123saurav commented 2 years ago

Doesn't Netty provide something similar like pooled buffer?

tuhuynh27 commented 2 years ago

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