Skip to content

Optimize SpecialBracketCharacters parsing and initialization of master caches#9921

Open
h3xds1nz wants to merge 6 commits intodotnet:mainfrom
h3xds1nz:special-bracket-optimization
Open

Optimize SpecialBracketCharacters parsing and initialization of master caches#9921
h3xds1nz wants to merge 6 commits intodotnet:mainfrom
h3xds1nz:special-bracket-optimization

Conversation

@h3xds1nz
Copy link
Member

@h3xds1nz h3xds1nz commented Oct 9, 2024

Description

Optimizes the very much unused SpecialBracketCharacters (which is used with MarkupExtensionBracketCharactersAttribute), bringing less static memory and more modern code.

  • Yes, there's now NetFX and NET version, there's just no way around bringing markup/baml improvements without special-casing most of the stuff. We will remove the NetFX, once.
  • Uses SearchValues<char> over SortedSet<char>, decreasing static memory 4 times and increasing perf 4 times.
  • Fixes pretty much what you might classify as a blatant memory leak by keeping the StringBuilder instances around.
  • This also leads to me doing, what in my opinion, is more proper implementation of ISupportInitialize (there are no new exceptions thrown, the code is written properly in all places I've checked, so it's just a safety work)
  • Mark BuildBracketCharacterCacheForType method as static in both PBT and System.Xaml.
  • Removes up to TRIPLE dictionary lookup in InitBracketCharacterCacheForType for PBT/PresentationFramework.
    • ContainsKey -> Add -> indexer lookup
  • Since I consider half of System.Xaml a spaghetti code I've adjusted BuildBracketCharacterCacheForType to be more readable (that's my personal opinion).

SortedSet<char> vs SearchValues<char> (2x contains)

Method char1 char2 Mean [ns] Error [ns] StdDev [ns] Code Size [B] Allocated [B]
Original \u3039 \u005c 4.5088 ns 0.0866 ns 0.0810 ns 101 B -
PR_EDIT \u3039 \u005c 0.3253 ns 0.0075 ns 0.0070 ns 70 B -

Construction of those two (SortedSet has a heap array)

Method Mean [ns] Error [ns] StdDev [ns] Code Size [B] Gen0 Allocated [B]
SearchValues 48.855 ns 0.9252 ns 0.8654 ns 1,377 B 0.0048 80 B
SortedSet 71.338 ns 1.5273 ns 3.6001 ns 1,079 B 0.0243 408 B

Customer Impact

Improved performance, decreased allocations.

Regression

No.

Testing

Local build; I've tested the changes in SpecialBracketCharacters and it works as expected.

Risk

Low, there are no functional changes, just optimizations.

Microsoft Reviewers: Open in CodeFlow

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 Included in test pass PR metadata: Label to tag PRs, to facilitate with triage Status:Completed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants