jsoniter-scala icon indicating copy to clipboard operation
jsoniter-scala copied to clipboard

Set Buf size before encoding value

Open samuelorji opened this issue 1 year ago • 4 comments

Hi,

Thanks for your contribution.

I have a question about how this buffer which is part of the thread local variable writer pool connects to something like the writer config here.

Unless I'm mistaken, I can see that the buffer only gets resized after its been encoded, as opposed to when writing to a stream where the buffer is resized based on the supplied config before its encoded

As a practical example, say I have to writeToArray a Map[String,String] and say the size of each entry in the map is < 2KB.

Now I defined my write config in terms of the size of the Map, so I make my buffer the right size for each response or Map like this:

val map : Map[String, String] = _
writeToArray(map, WriterConfig.withPreferredBufSize(AVERAGE_RESPONSE_SIZE * (map.size + 1))) // +1 in case of empty map

where AVERAGE_RESPONSE_SIZE = 2KB.

Now, if the size of the first map is 2, my buffer size should be 4Kb, if the next map has size of 200, then my buffer size should be 400Kb,

Going by the logic in the code, it means that for the first invocation, it uses the previous value of the buffer which is the default 32KB , after which it encodes my value, then shrinks it to 4KB ? because this will mean that for the second Map where I want 400KB buffer, it is going to start with a 4KB buffer because it only reallocateBufToPreferredSize after the encoding has happened?

Is this the way it's expected to behave?

Is a better way to resize the buf before we encode here.

I don't mind submitting a PR if you think that's the preferred solution

Thanks

samuelorji avatar Sep 05 '24 13:09 samuelorji