Skip to content

Improving DynamicResource performance (second try) #10532

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 7 commits into from
Apr 22, 2025

Conversation

batzen
Copy link
Contributor

@batzen batzen commented Mar 4, 2025

Second try to improve performance of DynamicResource.

This approach should not cause issues like #10491.

Original issue is still #4468.
Original PR was #5610 with follow up PR #9393 with opt out switch introduced in #9501 and final opt-in switch added in #10516.

Feedback on the approach is very appreciated.

Microsoft Reviewers: Open in CodeFlow

@batzen batzen requested review from a team as code owners March 4, 2025 17:17
@batzen batzen requested review from harshit7962 and removed request for singhashish-wpf, dipeshmsft and Kuldeep-MS March 4, 2025 17:18
@dotnet-policy-service dotnet-policy-service bot added PR metadata: Label to tag PRs, to facilitate with triage Community Contribution A label for all community Contributions labels Mar 4, 2025
@pchaurasia14 pchaurasia14 requested a review from Copilot March 4, 2025 17:29
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.

PR Overview

This PR aims to improve the performance of dynamic resource resolution by refactoring the deferred resource reference handling and optimizing related lookup methods. Key changes include:

  • Replacing the DeferredResourceReferenceList with a Dictionary-based weak reference map (_weakDeferredResourceReferencesMap) for deferred resource references.
  • Introducing a new private Contains method that returns additional flags to better control deferred resource lookup.
  • Refactoring resource reference classes in SystemResources.cs and Baml2006KeyRecord.cs for clarity and performance.

Reviewed Changes

File Description
ResourceDictionary.cs Updated deferred reference management with a weak reference map; added new Contains overload and revised methods to use the new infrastructure.
Baml2006KeyRecord.cs Minor syntax change to standardize type usage.
StyleHelper.cs Altered the logic order in the deferred resource check to prioritize Freezable types.
SystemResources.cs Refactored DeferredResourceReference and related classes to use separate _key and _value fields and updated removal logic accordingly.

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs:713

  • [nitpick] The private Contains method shares its name with the public Contains method, which may lead to confusion. Consider renaming it to better reflect its specialized behavior.
private void Contains(object key, bool mustReturnDeferredResourceReference, out bool contains, out bool containsBamlObjectFactory)

Copy link

codecov bot commented Mar 4, 2025

Codecov Report

Attention: Patch coverage is 29.05983% with 83 lines in your changes missing coverage. Please review.

Project coverage is 13.05569%. Comparing base (cac41ae) to head (b266a06).
Report is 21 commits behind head on main.

Additional details and impacted files
@@                 Coverage Diff                 @@
##                main      #10532         +/-   ##
===================================================
+ Coverage   12.66562%   13.05569%   +0.39006%     
===================================================
  Files           3316        3316                 
  Lines         665321      665434        +113     
  Branches       74667       74674          +7     
===================================================
+ Hits           84267       86877       +2610     
+ Misses        578844      576217       -2627     
- Partials        2210        2340        +130     
Flag Coverage Δ
Debug 13.05569% <29.05983%> (+0.39006%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@harshit7962 harshit7962 self-assigned this Mar 19, 2025
Copy link
Member

@harshit7962 harshit7962 left a comment

Choose a reason for hiding this comment

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

@batzen apologies for the delay in getting started with this. At this state the changes LGTM and we will include this in the next
test run.

@harshit7962
Copy link
Member

@batzen The test pass of this is done with, can you please resolve the conflicts? We can take this PR in post that.

batzen and others added 3 commits April 11, 2025 10:06
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@batzen batzen force-pushed the issues/dynamic_resource_perf branch from e675765 to c08649b Compare April 11, 2025 08:08
@batzen
Copy link
Contributor Author

batzen commented Apr 11, 2025

Rebased and resolved conflicts.

@batzen
Copy link
Contributor Author

batzen commented Apr 11, 2025

Not sure why the build is failing.
I consider this a false positive for IDE0100 as the code says "is false".

@harshit7962 harshit7962 merged commit 5edc518 into dotnet:main Apr 22, 2025
8 checks passed
@harshit7962
Copy link
Member

Thank you @batzen for this contribution and thank you @h3xds1nz and @lindexi for your time and suggestions on this.

harshit7962 added a commit that referenced this pull request Apr 22, 2025
* Fixes the layout and resource usage for PasswordBox (#10768)

* Fixed PasswordBox style

* Fixes minor issues in the new PasswordBox style

* [Fluent] Fixes mulitple issues is TextBox and TextBoxBase styles (#10756)

* Fixes issues is TextBox and TextBoxBase styles

* Minimizing extra non functional changes

* Matching parity with Aero2 for TextBox style

* [Fluent] Fixes multiple issues in DatePicker style (#10688)

* Fixes multiple issues in DatePicker style

* Reintroduced commented .NET 9 DatePicker resources

* Improving DynamicResource performance (second try) (#10532)

* Using alternative storage for dynamic resources

* Fixing removal

* Removing not required check

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Improving Contains

* Addressing PR feedback

* Adjusting code to false positive IDE warning...

* Addressing PR feedback

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Ignore DR Optimization preferences

---------

Co-authored-by: Dipesh Kumar <85861525+dipeshmsft@users.noreply.github.com>
Co-authored-by: Bastian Schmidt <batzen@gmx.org>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community Contribution A label for all community Contributions PR metadata: Label to tag PRs, to facilitate with triage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants