-
Notifications
You must be signed in to change notification settings - Fork 809
Demonstration of pooling of LogEvent instances #1788
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
base: dev
Are you sure you want to change the base?
Conversation
# Conflicts: # Serilog.sln
@@ -19,7 +19,7 @@ namespace Serilog.Events; | |||
/// </summary> | |||
/// <remarks>This type is currently internal, while we consider future directions for the logging pipeline, but should end up public | |||
/// in future.</remarks> | |||
readonly struct EventProperty | |||
public readonly struct EventProperty |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comments above.
@nblumhardt this is demo only, see API changes diff. No backward breaking changes, only new APIs. No new dependencies. In some cases -- one-threaded/console apps -- these approach can archive 100% reuse of created |
src/Serilog/Core/Logger.cs
Outdated
{ | ||
var logEvent = CreateFunc(logTimestamp, level, exception, parsedTemplate, boundProperties); | ||
Dispatch(logEvent); | ||
ReturnFunc?.Invoke(logEvent); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing bad if ReturnFunc
would not be called due to exception from Dispatch
.
|
||
namespace PoolRunner; | ||
|
||
internal static class LoggerExtensions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks and feels like some integration package.
I see now. Very nice solution. You even implemented a way for users to provide custom pooling solutions. You are counting that users only opt into log event pooling if they know they are using sinks and enrichers that will not keep the logEvent reference around so the only downside would be that nothing actually stops users from making such mistake and getting weird results. |
Yes, you got the idea. |
It turned out by itself, because I had no other choice. |
Can it somehow identify sinks / enrichers without support for pooling while its running and write error to selflog / throw? Reading source code / debugging through for every used sink would be tedious at best. |
I think that it is impossible. One can just store the reference to |
I would say that all syncronous sinks support pooling and async sinks may support pooling. |
Keen to see if you have you ran any benchmarks yet? EDIT: nevermind, I see them here now 😄 |
#1780 is about different memory optimization. Give me some time to add results here - today or tomorrow. |
Slightly modified
Results: BenchmarkDotNet=v0.13.2, OS=Windows 11 (10.0.22621.1105)
AMD Ryzen 7 6800H with Radeon Graphics, 1 CPU, 16 logical and 8 physical cores
.NET SDK=7.0.102
[Host] : .NET 7.0.2 (7.0.222.60605), X64 RyuJIT AVX2
DefaultJob : .NET 7.0.2 (7.0.222.60605), X64 RyuJIT AVX2
😮 |
|
Of course with |
Disclaimer: results may vary due to |
@nblumhardt What do you think? Memory consumption decreased by 4 times and it seems that pooled path works event a bit faster. Of course experience may vary. |
@@ -120,6 +120,8 @@ namespace Serilog.Core | |||
} | |||
public sealed class Logger : Serilog.Core.ILogEventSink, Serilog.ILogger, System.IAsyncDisposable, System.IDisposable | |||
{ | |||
public System.Action<Serilog.Events.LogEvent>? AfterDispatch { get; set; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible that these changes are acceptable in the light of the v3 release? Moreover, backward compatibility is not violated here.
# Conflicts: # Serilog.sln
@nblumhardt Will it be included in v3 release? No breaking changes. |
I think this one's a cool demo but probably not something I'd want to jump into at this point. Happy to keep it on the backburner and discuss post-3.0, though. I think it'd need a lot of thought around how the feature might be misused/potential bugs/drawbacks/footguns. |
Related: #1769 |
# Conflicts: # src/Serilog/Core/Logger.cs # src/Serilog/Events/LogEvent.cs # test/Serilog.ApprovalTests/Serilog.approved.txt
As a continuation of discussions from #1772. See benchmarks here #1788 (comment)