Skip to content

Let the vertx-pg-client encoder estimate the capacity of buffer it allocates #1514

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vietj
Copy link
Member

@vietj vietj commented Apr 24, 2025

Motivation:

The pg encoder allocates Netty buffer without an initial capacity, when pipelining is used this buffer can be reallocated multiple times and in agressive scenario can lead to out of memory errors with the adapative allocator.

Changes:

Cumulate and encode all the outbound messages at once when flush happens instead of cumulating them in the outbound buffer.

Capacity is estimated when a message is enqueued.

At flush time, a single buffer of the estimated capacity is created and the outbound messages are written to this buffer.

This requires to pre-render some messages to estimate their length, e.g. a json object is pre-rendered to a string that can be then estimated. The client has been modified to handle message preparation which takes care of this and lazy duplicate the tuple / list of tuples when this happens.

Todo:

  • check if we can save writer index read/writes in the loop
  • use fixed floating point to improve UTF estimation which is currently rounded (currently it is 1-1 because the float is rounded)
  • add a configurable UTF bytes per char option to improve efficiency for non latin languages
  • see if we can improve the pending message array usage without further allocation
  • replace long if/else statements in main loop with a switch
…locates.

Motivation:

The pg encoder allocates Netty buffer without an initial capacity, when pipelining is used this buffer can be reallocated multiple times and in agressive scenario can lead to out of memory errors with the adapative allocator.

Changes:

Cumulate and encode all the outbound messages at once when flush happens instead of cumulating them in the outbound buffer.

Capacity is estimated when a message is enqueued.

At flush time, a single buffer of the estimated capacity is created and the outbound messages are written to this buffer.

This requires to pre-render some messages to estimate their length, e.g. a json object is pre-rendered to a string that can be then estimated. The client has been modified to handle message preparation which takes care of this and lazy duplicate the tuple / list of tuples when this happens.
@vietj vietj added this to the 5.0.0 milestone Apr 24, 2025
@vietj vietj self-assigned this Apr 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
1 participant