Skip to content

Allow lazy string fields to be interned (custom string decoder) #56

Open

Description

@scottsue

Problem

A parsed message decodes a string field on the first call to its getter. LightProtoCodec.readString() creates a new String for each decode. That is the right default, but some applications read the same values in every message: names, codes, keys, identifiers. For them, the string decode becomes most of the remaining allocation after the parse.

An example with one 6 KB message that has 293 string fields, on JDK 21, after warm-up:

Operation B/op
parseFrom(ByteBuf, size) into a reused message 0
The same parse, then a read of every string field ~14,000

materialize() and copyFrom() cost the same, because they decode through the same method.

An application cannot add a cache itself. The buffer index, the length, and _parsedBuffer are private, and no accessor gives the raw bytes of a string field.

Proposal

Add an opt-in generator option, stringDecoder. Its value is the name of a class with this method:

public static String decode(io.netty.buffer.ByteBuf buffer, int index, int length)

When the option is set, each lazy string decode calls this method. When the option is not set, the generated code does not change. The application owns the cache policy (size, eviction, per-thread or shared, which values to cache), because only the application knows which values repeat.

I have an implementation with tests and can open a PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions