Skip to content

Remove char[] allocation#2198

Merged
nblumhardt merged 2 commits intoserilog:devfrom
karpinsn:dev
Sep 5, 2025
Merged

Remove char[] allocation#2198
nblumhardt merged 2 commits intoserilog:devfrom
karpinsn:dev

Conversation

@karpinsn
Copy link
Copy Markdown
Contributor

@karpinsn karpinsn commented Sep 4, 2025

This PR contributes a performance fix to remove some char[] allocations that are occurring in MessageTemplateParser. This trims ~10% of Gen0 memory usage in the MessageTemplateParsingBenchmark.DefaultConsoleOutputTemplate benchmark.

This fix was found by our new Copilot Profiler Agent as it was profiling and looking for different optimizations in the available benchmarks.

Before - 1776 Bytes

BeforeViaTests BeforeAllocations Note: the allocation trace was done having the Parse run 1000 times in the benchmark and only running the benchmark workload 5 times to keep the allocations consistent between runs.

After - 1616 Bytes

AfterViaTests AfterAllocation

- This PR removes a char[] allocation that is occuring in
  MessageTemplateParser. The fix is to instead create a static char[]
  that we then reference which removes the allocation altogether. This
  doesn't have a huge impact on the benchmark runtime though it does
  help eliminate a small allocation that was contributing to more GCs in
  the benchmark.
Copy link
Copy Markdown
Member

@nblumhardt nblumhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find, Nik! Thanks for the PR, just had the one query.

@nblumhardt nblumhardt merged commit 2fd949e into serilog:dev Sep 5, 2025
1 check passed
@nblumhardt nblumhardt mentioned this pull request Feb 10, 2026
This was referenced Feb 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants