lettuce icon indicating copy to clipboard operation
lettuce copied to clipboard

Avoid calling URI.toString when populating tracing span tags

Open GnawNom opened this issue 7 months ago • 1 comments

Feature Request

Looking at flame graphs for our service we found 3% of our cpu time was spent on calling toString for a string that should already exist.

https://github.com/redis/lettuce/blob/main/src/main/java/io/lettuce/core/protocol/CommandHandler.java#L495

Is your feature request related to a problem? Please describe

Current implementation wastes CPU cycles

Describe the solution you'd like

redisUriStr is passed to create a URI class and then toString is called on the URI class to populate the span tag for server.address. We can avoid this toString call and pass redisUriStr directly for server.address. Unless the toString produces a different string from the one passed in to create the URI class, I think this is a reasonable change.

Describe alternatives you've considered

An alternative is to cache the URI object and resulting strings, but that seems like overkill. If this channel attribute is immutable (channel.attr(ConnectionBuilder.REDIS_URI).get();) we could probably extend the change to cache all the span tags we generate if the URI is not going to change for the channel. In our flame graph analysis another 3% of wasted CPU time was in instantiating this URI class every time span tags need to be populated.

Teachability, Documentation, Adoption, Migration Strategy

Not sure, not really a user facing feature. More of an optimization. Let me know if this belongs under a different category from Feature Request

GnawNom avatar May 23 '25 19:05 GnawNom

Image Image Image

Tracing takes up about 10% of our cpu, with 6% of that solely dedicated to instantiating a URI and calling toString on the created URI object.

GnawNom avatar May 23 '25 19:05 GnawNom

Hi @tishun could you please take a look at the linked PR and provide feedback on this performance improvement? Thank you!

RohanNagar avatar Jun 27 '25 20:06 RohanNagar