Skip to content

Bad hash code calculation in PSMethodInvocationConstraints results in explosion of static cache #24964

Closed

Description

The hash code calculation in PSMethodInvocationConstraints only counts in the array members but not their elements, so the returned hash code would be different for two PSMethodInvocationConstraints instances that hold exactly the same elements in their array members.

The hash code of PSMethodInvocationConstraints is used by the static cache PSInvokeMemberBinder.s_binderCache because PSMethodInvocationConstraints is a member of the key for that cache, so you end up with every request being a cache miss in PSInvokeMemberBinder.s_binderCache.

=> HashCode.Combine(MethodTargetType, ParameterTypes, GenericTypeParameters);

Activity

  1. crazyjncsu commented on Feb 6, 2025

    @crazyjncsu
    ContributorAuthor

    Image

  2. crazyjncsu commented on Feb 6, 2025

    @crazyjncsu
    ContributorAuthor

    this cache is the blue ring over the top:

    Image

  3. crazyjncsu commented on Feb 6, 2025

    @crazyjncsu
    ContributorAuthor

    Image

  4. changed the title [-]Bad hash code calculation in PSMethodInvocationConstraints results in explosion of static cache[/-] [+]Bad hash code calculation in `PSMethodInvocationConstraints` results in explosion of static cache[/+] on Feb 12, 2025
  5. iSazonov commented on Feb 13, 2025

    @iSazonov
    Collaborator

    Jake Morgan (@crazyjncsu) Could you please share how you measure cache misses? Perhaps we could add a test to exclude regressions in future.

  6. microsoft-github-policy-service commented on Feb 13, 2025

    @microsoft-github-policy-service
    Contributor

    📣 Hey Jake Morgan (@crazyjncsu), how did we do? We would love to hear your feedback with the link below! 🗣️

    🔗 https://aka.ms/PSRepoFeedback

  7. crazyjncsu commented on Feb 13, 2025

    @crazyjncsu
    ContributorAuthor

    Ilya (@iSazonov)

    In this case I was looking at a memory profile and deduced it through circumstantial evidence. Then used more runtime inspection to see that the hash codes were different for seemingly empty objects (of type PSMethodInvocationConstraints) where those PSMethodInvocationConstraints were used as a tuple elements which was a cache key.

    There are certainly all kinds of ways to measure at runtime, for example by adding telemetry metrics. Microsoft seems to have at least 10 different ways they measure stuff like this :)

  8. added and removed
    In-PRIndicates that a PR is out for the issue
    on May 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    7.5-regressionThe issue is a regressionIssue-BugIssue has been identified as a bug in the product

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions