Improved The Performance Of Logger#66895
Improved The Performance Of Logger#66895ProgrammingFire wants to merge 3 commits intodotnet:mainfrom ProgrammingFire:patch-1
Conversation
Updated LoggerExtensions.cs To Only Even Try To Log If The Log Level If Enable To Improve Performance Of Logger By Saving Memory Allocation
|
Tagging subscribers to this area: @dotnet/area-extensions-logging Issue DetailsUpdated
|
Added A Space After The `if` Keyword On-Line `28` Inside `LoggerExtensions.cs`
| public static void LogDebug(this ILogger logger, EventId eventId, Exception? exception, string? message, params object?[] args) | ||
| { | ||
| logger.Log(LogLevel.Debug, eventId, exception, message, args); | ||
| if (logger.IsEnabled(LogLevel.Debug)) |
There was a problem hiding this comment.
Which allocations should be saved by this pattern?
The logger.IsEnabled-check should be done before the call to LogDebug in user code.
Thus the allocation of args (the array) and potentially the creation of message can be saved. These objects are already existent when reaching the check you introduced here.
Or even better use LoggerMessage.Define where that behavior can be set / use the (new) logging source generator.
|
@ProgrammingFire please provide the benchmark numbers for the allocation and the code you are using to measure it. |
Updated `LoggerExtensions.cs` To Only Even Try To Log If The Log Level Is Enabled And Make Sure The First Three Arguments For The Formatted Message Is Passed As Generics To Make Sure They Are Not Boxed And Unboxed To Improve Performance Of Logger By Saving Memory Allocation
|
Check this new commit which actually improves the memory allocation by not storing the first three arguments into objects so that they are not boxed and unboxed that actually saves memory allocation: cc87f19 |
|
I think the improvements in the logger depends on the user that's why I am closing this pull request. |
Updated
LoggerExtensions.csTo Only Even Try To Log If The Log Level Is Enable To Improve Performance Of Logger By Saving Memory Allocation