serilog / serilog-extensions-logging

Serilog provider for Microsoft.Extensions.Logging
Apache License 2.0
307 stars 97 forks source link

Use method BindMessageTemplate to improve performance and memory usage #162

Closed Maxim-Kornilov closed 2 years ago

Maxim-Kornilov commented 4 years ago

Performs 40-45% faster and uses 40-50% less memory for repeated messages.

Benchmarks

Link to benchmarks source code

BenchmarkDotNet=v0.12.0, OS=Windows 10.0.18362
Intel Core i5-8400 CPU 2.80GHz (Coffee Lake), 1 CPU, 6 logical and 6 physical cores
.NET Core SDK=2.2.105
  [Host]     : .NET Core 2.2.3 (CoreCLR 4.6.27414.05, CoreFX 4.6.27414.05), X64 RyuJIT
  DefaultJob : .NET Core 2.2.3 (CoreCLR 4.6.27414.05, CoreFX 4.6.27414.05), X64 RyuJIT

Before

Method Mean Error StdDev Ratio Gen 0 Allocated
Serilog 2.900 us 0.0052 us 0.0049 us 1.00 0.3395 1.58 KB
Extension 6.579 us 0.0281 us 0.0263 us 2.27 0.7935 3.66 KB
ExtensionWithEventId 6.891 us 0.0079 us 0.0070 us 2.38 0.8316 3.84 KB

After

Method Mean Error StdDev Ratio Gen 0 Allocated
Serilog 2.923 us 0.0073 us 0.0068 us 1.00 0.3395 1.58 KB
Extension 3.590 us 0.0182 us 0.0171 us 1.23 0.4120 1.91 KB
ExtensionWithEventId 3.805 us 0.0061 us 0.0057 us 1.30 0.4654 2.15 KB
nblumhardt commented 4 years ago

Thanks! Looks very promising. I'll try to find some quality review time ASAP, though time is a little tight at the moment, so apologies in advance if the turnaround is not snappy.

nblumhardt commented 3 years ago

apologies in advance if the turnaround is not snappy

That may have been the understatement of 2020! :-D ... Thanks again for this, Maxim.

I finally did have time to think it through; my main concern with using BindMessageTemplate() here is that it makes the enumeration order of the event properties important. It's not clear that the order of properties in the enumerated structure will always match the order of property names in the template holes. Some ILogger<T> optimization techniques hand-code the enumeration of properties in the state object, so this could be broken even if we don't have a failing case in front of us.

I don't think we should proceed in this direction, but thanks again, and please let me know if it sounds like I've missed something.

Best regards, Nick