Skip to content

Optimize memory allocations #1769

@igor84

Description

@igor84

Is your feature request related to a problem? Please describe.
I am considering replacing our in house logging solution for Unity3D with Serilog but it allocates 3x more memory for logging a simple string.

Describe the solution you'd like
Using a few quick experiments I found that we could bring the allocations to the same minimal level with these 3 changes:

  1. Use foreach over _outputTemplate.TokenArray instead of Tokens IEnumerator in MessageTemplateTextFormatter.Format method so no IEnumerator is allocated.
  2. Pool and reuse internally created StringWriters using writer.GetStringBuilder.Clear().
  3. Pool and reuse LogEvent instances.

Additional context
If you agree this would be useful I could make a pull request. It would be nice if I could use ConcurrentBag for the pools but we would have to sack .netstandard1.0 since it is not supported there.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions