Skip to content

[dotnet] Improve logging performance when it is disabled #13464

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

Merged
merged 4 commits into from
Jan 19, 2024

Conversation

nvborisenko
Copy link
Member

@nvborisenko nvborisenko commented Jan 19, 2024

Description

It adds IsEnabled method to check there is handler and event can be emitted. Now internally we should use the following pattern to emit a message:

if (logger.IsEnabled(LogEventLevel.Trace))
{
  logger.Trace("my message");
}

Motivation and Context

Fixes #13411

Benchmark

Code:

[Benchmark]
public void EmitLogOld()
{
    var message = new string('a', 1000);

    logger.Trace(message);
}

[Benchmark]
public void EmitLogNew()
{
    if (logger.IsEnabled(LogEventLevel.Trace))
    {
        var message = new string('a', 1000);

        logger.Trace(message);
    }
}

Results:

| Method     | Mean       | Error     | StdDev    | Gen0   | Allocated |
|----------- |-----------:|----------:|----------:|-------:|----------:|
| EmitLogOld | 213.412 ns | 4.0365 ns | 3.7757 ns | 0.3226 |    2024 B |
| EmitLogNew |   9.917 ns | 0.2251 ns | 0.3369 ns |      - |         - |

I also tried with MethodImpl attribute with aggressive inlining, got mean = 9.1 ns. Decided to not use it, keeping code simple.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@nvborisenko nvborisenko added the C-dotnet .NET Bindings label Jan 19, 2024
@nvborisenko nvborisenko merged commit e8f02e9 into SeleniumHQ:trunk Jan 19, 2024
@nvborisenko nvborisenko deleted the dotnet-log-perf branch February 17, 2024 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-dotnet .NET Bindings
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[🚀 Feature]: [dotnet] Improve performance of internal logging
2 participants