jsonparser icon indicating copy to clipboard operation
jsonparser copied to clipboard

pre-allocate for Set() new component for lower memory usage

Open JoshuaC215 opened this issue 7 years ago • 2 comments

Extra allocations are done when using an empty bytes.Buffer here in createInsertComponents(). bytes.Buffer uses a normal grow strategy of successively doubling in size. This could be avoided by counting the length of the needed keys and the final set value and starting with a Buffer that is about the right size.

JoshuaC215 avatar Jun 20 '17 19:06 JoshuaC215

Yes, it is a good idea!

I also think in terms of implementing multiple key set which will lead us to full marshaling implementation. When we set multiple keys, it make the first iteration, and find all keys first, and estimate how big new buffer should be, and on second iteration fills it with data.

buger avatar Jun 21 '17 05:06 buger

Actually I dug into source and realized the bytes.Buffer provides a 64 byte initial slice for writing into. Given that, I think it's a tossup whether the complexity is worth it to pre-allocate.

JoshuaC215 avatar Jun 21 '17 15:06 JoshuaC215