Skip to content

Avoid per-member work when JSON rename is empty - #52577

Closed
dgomesbr wants to merge 2 commits into
spring-projects:mainfrom
dgomesbr:perf/json-rename-fast-path
Closed

dgomesbr wants to merge 2 commits into
spring-projects:mainfrom
dgomesbr:perf/json-rename-fast-path

Conversation

@dgomesbr

@dgomesbr dgomesbr commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

As soon as any logging.structured.json.* property is bound - include, exclude, rename, add, context, stack trace, a customizer, any of them - Boot registers StructuredLoggingJsonPropertiesJsonMembersCustomizer, and its name processor runs on every member of every event, whether or not that member's name actually changes. Two things happen on every single call. renameJsonMembers builds path.toUnescapedString() before it even checks whether a rename is configured, and that string gets thrown away unless the user renamed something. JsonValueWriter.processName builds its assertion message eagerly - "NameProcessor " + nameProcessor + " returned an empty result" - even though the message is only used when the result actually is empty. For Spring Boot's own processor, a method reference, building that message means rendering a hidden class name and an identity hash into roughly 160 characters, for every name, every time.

renameJsonMembers now returns the existing name before building the path when no rename is configured, and processName passes its assertion message as a Supplier, matching how the other assertions in the same file already work. The processor stays registered exactly as before; only the per-name cost on the success path disappears.

Names, filtering, ordering, and failures don't change - an empty name still fails with the same IllegalStateException and the same message text. The one observable difference: a NameProcessor's toString() is now called only when it actually returns an empty result, so a custom processor whose toString() throws will fail only on that path instead of on every write. Dropping the toUnescapedString() call also drops its recursion, so this can only reduce stack depth at the margin instead of inflating/growing on top of it. A differential run over 10,000 generated events across 11 property combinations found no output difference on JDK 17.0.20.1 or 25.0.5, with or without #52362 applied on top - that PR is open against the same customizer but a different method, so the two don't conflict.

Most of what this removes is that assertion-message string, built and thrown away on every successful write.

Benchmark JDK ops/s before -> after B/op before -> after
Format one event with json.add set (micro) 25 300.7k -> 714.0k (+137%, noisy run) 20.1k -> 5128 (-75%)
Format one event with json.add set (micro) 17 noisy this run, no reliable number 21.2k -> 5744 (-73%)
One ECS event end to end, json.add set 25 176.5k -> 291.3k (+65%, noisy) 31.7k -> 15.2k (-52%)
One ECS event end to end, json.add set 17 161.1k -> 278.2k (+73%, noisy) 32.6k -> 15.3k (-53%)

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue that has not yet been triaged. Contributions will not be accepted. label Nov 3, 2026
Add tests that pin the JSON written by the common structured logging
formats for the rename, add, include, exclude, context and customizer
properties, including empty names and charsets, and the message of the
empty name assertion in JsonWriter, before changing how names are
processed.

Signed-off-by: Diego Magalhães <dgomesbr@gmail.com>
Check whether any rename is configured before building the member path
in StructuredLoggingJsonPropertiesJsonMembersCustomizer, and only build
the message of the empty-name assertion in JsonValueWriter when the
assertion fails. Both ran for every member and pair of every log event
whenever a logging.structured.json property was set. Names, filtering
and failures are unchanged.

Signed-off-by: Diego Magalhães <dgomesbr@gmail.com>
@philwebb

philwebb commented Nov 4, 2026

Copy link
Copy Markdown
Member

See #52574 (comment)

@philwebb philwebb closed this Nov 4, 2026
@philwebb philwebb added status: superseded An issue that has been superseded by another and removed status: waiting-for-triage An issue that has not yet been triaged. Contributions will not be accepted. labels Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: superseded An issue that has been superseded by another

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants