Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 10, 2025

This change optimizes the PersistentStateValueProvider class by making the _serializerCache field static instead of instance-level.

Problem

Previously, every instance of PersistentStateValueProvider would create its own ConcurrentDictionary for caching serializers, even though the serializers are typically registered as singletons in the DI container and don't vary between instances.

Solution

Changed _serializerCache from private readonly to private static readonly. This allows all instances to share the same cache, reducing memory allocation and improving performance when multiple PersistentStateValueProvider instances are created.

Impact

  • Performance: Reduces memory allocation by sharing the serializer cache across all instances
  • Compatibility: No breaking changes - the cache behavior remains functionally identical
  • Thread Safety: ConcurrentDictionary is already thread-safe, making the static sharing safe

Testing

  • ✅ All Components tests pass (1,124 tests)
  • ✅ All PersistentStateValueProvider tests pass (35 tests)
  • ✅ All IPersistentComponentStateSerializer tests pass (2 tests)
  • ✅ No build regressions in the core Components project

Fixes #62659.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] [Blazor] Do not create a dictionary every time [Blazor] Make _serializerCache static readonly to avoid creating dictionaries per instance Jul 10, 2025
Copilot finished work on behalf of javiercn July 10, 2025 15:09
@Copilot Copilot AI requested a review from javiercn July 10, 2025 15:09
@javiercn javiercn marked this pull request as ready for review July 10, 2025 15:26
@javiercn javiercn requested a review from a team as a code owner July 10, 2025 15:26
Copy link
Member

@ilonatommy ilonatommy left a comment

Choose a reason for hiding this comment

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

If the CI is green.

@javiercn javiercn enabled auto-merge (squash) July 10, 2025 16:09
@javiercn javiercn merged commit 745b8ca into main Jul 10, 2025
28 checks passed
@javiercn javiercn deleted the copilot/fix-62659 branch July 10, 2025 16:31
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-preview7 milestone Jul 10, 2025
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.

[Blazor] Avoid creating a cache dictionary per instance on PersistentStateValueProvider
4 participants