Skip to content

Conversation

vcsjones
Copy link
Member

@vcsjones vcsjones commented Oct 14, 2025

s_compatOids is a dictionary that is never updated at runtime and has contained 11 items for more than 5 years.

Rather than use a dictionary, let's use a switch here. The compiler will generate an efficient lookup for the handful of switch cases. This means we are doing less work in the .cctor of OidLookup to create the dictionary. This dictionary is not frequently read from, either, so the eagerly created dictionary was wasted work, most of the time.

@vcsjones vcsjones self-assigned this Oct 14, 2025
@Copilot Copilot AI review requested due to automatic review settings October 14, 2025 15:18
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 optimizes the OID lookup mechanism by replacing a static dictionary s_compatOids with a switch expression. The change eliminates the overhead of creating an 11-item dictionary during static construction, which was rarely accessed, in favor of a more efficient compile-time optimized switch statement.

  • Replaces dictionary lookup with switch expression for better performance
  • Removes static dictionary initialization overhead
  • Maintains identical functionality with cleaner code structure

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-security, @bartonjs, @vcsjones
See info in area-owners.md if you want to be subscribed.

@EgorBo
Copy link
Member

EgorBo commented Oct 14, 2025

Unrelated to this PR: we have quite a few static readonly Dictionary fields initialized with some constant values - I wonder if we can propose an analyzer suggesting to convert them to plain switches? It should be more efficient even for string literals given they have special support from the Roslyn side via Trie-like lookup, while for Dictionary we have to compute the hashcode (O(N) over string's content).

Or just replace them all manually at least in the BCL

cc @jkotas

Copy link
Member

@EgorBo EgorBo left a comment

Choose a reason for hiding this comment

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

This not only removes the pretty slow static init, but also makes lookup faster: sharplab (trie)

@vcsjones
Copy link
Member Author

There is definitely more I think we can do in this OidLookup, I will keep chipping away at it.

@vcsjones vcsjones merged commit 8d67e35 into dotnet:main Oct 15, 2025
86 checks passed
@vcsjones vcsjones deleted the no-compat-oid-dictionary branch October 15, 2025 00:37
@stephentoub
Copy link
Member

special support from the Roslyn side via Trie-like lookup, while for Dictionary we have to compute the hashcode (O(N) over string's content).

Only in some cases. Others will fall back to a binary search still based on computing a hash code, e.g SharpLab

switch also only works for the equivalent of StringComparer.Ordinal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants