Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JacksonCodec should better guess the allocated buffer #4977

Closed
franz1981 opened this issue Nov 26, 2023 · 1 comment
Closed

JacksonCodec should better guess the allocated buffer #4977

franz1981 opened this issue Nov 26, 2023 · 1 comment

Comments

@franz1981
Copy link
Contributor

ByteBuf buf = Unpooled.buffer();
relies on default allocation, which is very small (256 bytes, but can be very big based on the actual need!). Subsequent reallocations, if any, will happen based on Netty default strategy, which is less then optimal, given that can quickly converge to very big values(see https://github.com/franz1981/netty/blob/4.1/buffer/src/main/java/io/netty/buffer/AbstractByteBufAllocator.java#L277 for what is doing now).

There are 4 different approaches here:

  • use a thread local tmp buffer (or something similar to the striped pool implemented for Loom - Jackson buffer pool)
  • use a chunked approach which have smaller chunks and perform a final copy into an exact sized Netty one based on the list
  • like the previous, but creating a Composite ByteBuf out of the list of chunks (which is dangerous in case we have too many abstract implementations of Netty buffers flying around in core and hot path methods)
  • perform a better guess for interesting types (eg JsonArray/JsonObject) - but can be easily O(N)

I don't know how much important is the codec I have mentioned but is the default choice from what I can tell.
I'll go deeper in the code in the next days and maybe there is a better way to improve this.

@franz1981
Copy link
Contributor Author

Some other data points:
image

The Jackson generator is already making uses of a thread local BufferRecycler, already and:

  • Unpooled::buffer is not what we want because end up calling LongAdder: it would be better to switch to unpooled vertx heap buffers instead (which are not ref cnt and accounting for LongAdder::incrementAndGet
  • we can lazily let the jackson recycler to perform a single write over it, and allocate it once -> I'll send a PR soon for both

franz1981 added a commit to franz1981/vert.x that referenced this issue Nov 28, 2023
@vietj vietj closed this as completed in ea066cb Nov 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant