Skip to content
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

MECM MemoryCache performance; split string keys into separate collection #111502

Merged
merged 1 commit into from
Jan 24, 2025

Conversation

mgravell
Copy link
Member

@mgravell mgravell commented Jan 16, 2025

Historically, MECM MemoryCache uses a single lookup of object types. The runtime includes advanced key re-hashing in the relevant dictionary types, but this is restricted to TKeytypeof(string), which won't apply if TKey is object. We resolve this by splitting the collection into two - one for string keys, one for everything else. This allows the string keys (which is most of them, in many cases) to be re-hashed automatically. This improves performance in catastrophic key collision scenarios.

MECM is an OOB NuGet package with net462 and netstandard2.0 targets (in addition to up-level). On down-level TFMs, where this re-hashing isn't available, we can still mitigate against the impact of catastrophic key collisions by using Marvin (i.e. using a random per-process seed for string hashing); this means that collision scenarios are different per execution, reducing risk by repeatable scenarios. Specifically: a precomputed list of known colliding strings cannot be forced into the dictionary via user-input, API access, etc.

Changes:

  • split string and non-string keys into separate collections, to allow re-hashing of string keys
  • use Marvin explicitly on down-level TFMs (it is implicit on up-level)

Note:

- split string and non-string keys into separate collections, to
  allow re-hashing of string keys
- use Marvin explicitly on down-level TFMs (it is implicit on up-level)
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-extensions-caching
See info in area-owners.md if you want to be subscribed.

@mgravell
Copy link
Member Author

mgravell commented Jan 17, 2025

/cc @adamsitnik as a reviewer of the original net9 PR (42427)

Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

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

Approving as this is the same changes that went into release/9.0:

@mgravell mgravell merged commit d90c2e5 into main Jan 24, 2025
139 checks passed
@mgravell mgravell deleted the marc/MECM-Marvin branch January 24, 2025 16:13
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.

2 participants