Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization: used StartsWith(char) instead of StartsWith(string) where supported #255

Merged
merged 1 commit into from
Jul 23, 2024

Conversation

epeshk
Copy link
Contributor

@epeshk epeshk commented Jul 17, 2024

This PR optimizes the SerilogLogger.PrepareWrite method by using a StartsWith(char) overload instead of StartsWith(string) on >= net6.0, >= netstandard2.0 platforms.

image

Big win in the benchmark from NLog project (https://github.com/NLog/NLog-Performance-tests/blob/master/MicrosofLoggingPerformance/Program.cs)

Before

Test Name Time (ms) Msgs/sec GC2 GC1 GC0 CPU (ms) Alloc (MB)
NLog 4 895 1 021 447 0 0 13 4 109 2 021,8
Serilog 17 058 293 116 0 0 22 12 468 3 474,5

After

Test Name Time (ms) Msgs/sec GC2 GC1 GC0 CPU (ms) Alloc (MB)
NLog 4 753 1 051 935 0 0 13 4 109 2 021,8
Serilog 3 443 1 452 118 0 0 22 3 109 3 514,8

@epeshk epeshk force-pushed the startswith-char branch 2 times, most recently from 8ba8d1c to 32f5e17 Compare July 18, 2024 08:18
@epeshk epeshk force-pushed the startswith-char branch from 32f5e17 to 3dd50b2 Compare July 18, 2024 08:19
@shvez
Copy link

shvez commented Jul 18, 2024

@snakefoot fyi

@Numpsy
Copy link
Member

Numpsy commented Jul 18, 2024

That looks like a pretty large gain for a simple change :-)

Does it have any effect on the local benchmarkdotnet project? (there were some other perf changes going on for that in #247 )

@epeshk
Copy link
Contributor Author

epeshk commented Jul 18, 2024

In #247 this issue is addressed with StringComparison.Ordinal, which is optimized on modern runtimes

@nblumhardt
Copy link
Member

I like the StringExtensions polyfill; nicer to use the more modern APIs 👍

I'll merge and resolve conflicts with the other PR. We'll need to release a 9.x of this library fairly soon, but an 8.0.1 with this could also be worthwhile, given the throughput boost :-)

@nblumhardt nblumhardt merged commit 7fc50b0 into serilog:dev Jul 23, 2024
1 check passed
@nblumhardt nblumhardt mentioned this pull request Dec 6, 2024
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.

4 participants