Skip to content

converting eventsource metadata list to a dict #117031

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 11 commits into from
Jun 27, 2025
Merged

converting eventsource metadata list to a dict #117031

merged 11 commits into from
Jun 27, 2025

Conversation

rcj1
Copy link
Contributor

@rcj1 rcj1 commented Jun 25, 2025

Closes #99816

This pull request implements the request described in #99816 of reducing the memory usage of EventSource in the case of high EventIDs by using a dictionary of EventMetadata instead of an array. This change resulted in no visible slowdown of WriteEvent, and memory usage was brought down to consistent levels across high and low EventIDs that matches the preexisting memory usage levels in the case of low EventIDs.

@Copilot Copilot AI review requested due to automatic review settings June 25, 2025 21:20
@rcj1 rcj1 requested a review from hoyosjs June 25, 2025 21:20
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
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 replaces the fixed-size EventMetadata[] storage with a Dictionary<int, EventMetadata> to allow sparse and dynamic event ID mappings.

  • Swaps all m_eventData array uses to Dictionary<int, EventMetadata> and updates access patterns to use CollectionsMarshal.
  • Converts loops over array indices into dictionary key/value iterations and adapts initializers.
  • Adds using System.Runtime.InteropServices for CollectionsMarshal APIs.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/NativeRuntimeEventSource.cs Switched m_eventData to a dictionary, updated bounds check and payload decoding to use CollectionsMarshal.
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs Replaced array-based loops and initializers with dictionary equivalents; updated all indexing to use GetValueRefOrNullRef / GetValueRefOrAddDefault.

@rcj1 rcj1 enabled auto-merge (squash) June 25, 2025 23:29
rcj1 and others added 5 commits June 26, 2025 13:13
…acing/EventSource.cs

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
…acing/EventSource.cs

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
…acing/EventSource.cs

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
@rcj1
Copy link
Contributor Author

rcj1 commented Jun 27, 2025

Blocked by #117039

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 aside from a little bit of formatting stuff and a couple unneeded null checks.

Perf-wise I'd be a bit surprised if this change didn't incur at least a few % extra CPU cost in WriteEvent for a scenario with an active EventListener but I think it would be an OK tradeoff for the sparse event id memory benefits.

@rcj1 rcj1 merged commit 64e178b into main Jun 27, 2025
136 of 143 checks passed
rcj1 added a commit that referenced this pull request Jun 27, 2025
@jkotas jkotas deleted the gh99816 branch June 27, 2025 14:31
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.

Higher Memory Usage for the EventSource Class
3 participants