You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow lazy string fields to be interned (custom string decoder) #56
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:
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