Skip to content

Fixing performance regression in EventSource #117549

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 3 commits into from
Jul 14, 2025

Conversation

rcj1
Copy link
Contributor

@rcj1 rcj1 commented Jul 11, 2025

This PR is intended to close #117203 by fetching EventMetadata by reference instead of by value. Accessing the uninitialized TraceLoggingEventTypes field of EventMetadata structs will run a constructor; if the struct is accessed by value, this constructor runs repeatedly on WriteEventVarargs, in addition to the typical overhead of accessing by value vs by reference.

Below are performance benchmark test results on my local machine (Windows x64)

Microsoft.Extensions.Logging.EventSourceLogger

Method HasSubscribers Json Mean Error StdDev Median Min Max Gen0 Allocated
NestedScopes_TwoMessages False False 66.65 ns 12.78 ns 14.72 ns 62.90 ns 50.07 ns 103.5 ns 0.0019 24 B
NestedScopes_TwoMessages False True 88.13 ns 34.40 ns 39.61 ns 75.93 ns 49.05 ns 158.7 ns 0.0018 24 B
NestedScopes_TwoMessages True False 1,865.06 ns 131.82 ns 151.80 ns 1,852.22 ns 1,673.65 ns 2,245.5 ns 0.4971 6256 B
NestedScopes_TwoMessages True True 2,101.99 ns 469.80 ns 541.02 ns 1,980.80 ns 1,648.20 ns 4,039.1 ns 0.4920 6256 B

System.Diagnostics.Perf_EventSource

Method Mean Error StdDev Median Min Max Gen0 Allocated
Log 117.2 ns 3.90 ns 4.49 ns 116.7 ns 111.8 ns 127.0 ns 0.0232 296 B

System.Diagnostics.Tracing.Perf_EventListener

Method Mean Error StdDev Median Min Max Gen0 Allocated
WriteEvent_NoParams 49.70 ns 2.437 ns 2.709 ns 49.86 ns 45.84 ns 55.84 ns 0.0057 72 B
WriteEvent_IntParams 69.31 ns 2.776 ns 3.197 ns 68.21 ns 64.94 ns 76.14 ns 0.0170 216 B
WriteEvent_StringParams 75.52 ns 2.703 ns 3.113 ns 75.14 ns 71.29 ns 82.14 ns 0.0197 248 B
WriteEvent_MixedParams 77.85 ns 2.863 ns 3.182 ns 76.61 ns 73.60 ns 84.66 ns 0.0176 224 B

Also closes dotnet/perf-autofiling-issues#58568

@Copilot Copilot AI review requested due to automatic review settings July 11, 2025 17:55
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jul 11, 2025
@rcj1 rcj1 requested review from steveisok and noahfalk July 11, 2025 17:55
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves performance by fetching EventMetadata by reference instead of by value to avoid repeated struct copies and constructor invocations.

  • Replaced direct m_eventData[eventId] indexing with CollectionsMarshal.GetValueRefOrNullRef in exception handling and event serialization paths.
  • Updated WriteEventWithRelatedActivityIdCore, WriteEventVarargs, SerializeEventArgs, LogEventArgsMismatches, and IsEnabledByDefault to use reference-based metadata access.

@rcj1 rcj1 added area-System.Diagnostics.Tracing and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Jul 11, 2025
Copy link
Contributor

Tagging subscribers to this area: @tarekgh, @tommcdon, @steveisok, @pjanotti
See info in area-owners.md if you want to be subscribed.

Copy link
Member

@noahfalk noahfalk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@steveisok steveisok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@rcj1 rcj1 merged commit f1f1167 into dotnet:main Jul 14, 2025
141 of 144 checks passed
@rcj1 rcj1 deleted the WriteEventRegressionFix branch July 14, 2025 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Perf] Windows/x64: 6 Regressions on 6/27/2025 11:05:34 AM +00:00 [Perf] Linux/x64: 7 Regressions on 6/27/2025 11:05:34 AM +00:00
3 participants