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

Optimize FontSourceCollection creation from a filesystem directory, reduce allocs #9844

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

h3xds1nz
Copy link
Contributor

@h3xds1nz h3xds1nz commented Sep 26, 2024

Description

Optimizes building of FontSourceCollection and also some work in FontResourceCache.

  • Path.GetExtension has a span variant, this prevents string creation just to check IsSupportedFontExtension.
  • SupportedExtensions static array has been removed as it is not needed for anything, anymore.
  • I've included the code-path removal for isWindowsFonts from the previous PR, hence why this is a draft.
  • Instead of using Directory.GetFiles, we will use the underlying implementation on .NET Core; the FileSystemEnumerable.
    • I've copied the enumeration options to mimic what Directory.GetFiles(string) does (Compatible).
    • We do not need the MatchType to be specified at all (which would be Win32), so it is not.
    • This will greatly reduce allocations when querying folders with unsupported files, etc.,
  • In case of a single file://, we will no longer allocate a string[] of 1.

416 files in directory, no matches

Method Mean [ns] Error [ns] StdDev [ns] Gen0 Code Size [B] Gen1 Allocated [B]
Original 147,452.0 ns 1,774.1 ns 1,659.5 ns 5.6152 4,449 B 1.2207 96320 B
PR__EDIT2 112,287.9 ns 534.4 ns 473.7 ns - 5,824 B - 368 B

351 files in directory, 156 matches

Method Mean [ns] Error [ns] StdDev [ns] Gen0 Code Size [B] Gen1 Allocated [B]
Original 170,543.1 ns 973.2 ns 812.6 ns 3.6621 4,989 B 0.2441 62760 B
PR__EDIT2 153,370.2 ns 999.2 ns 885.7 ns 1.4648 4,539 B - 25888 B

5 files, 1 match

Method Mean [ns] Error [ns] StdDev [ns] Gen0 Code Size [B] Allocated [B]
Original 20,788.0 ns 143.1 ns 126.9 ns 0.0916 5,025 B 1768 B
PR__EDIT2 19,837.8 ns 161.6 ns 151.2 ns 0.0305 6,521 B 600 B

1 file, 1 match

Method Mean [ns] Error [ns] StdDev [ns] Gen0 Code Size [B] Allocated [B]
Original 19,390.3 ns 177.1 ns 165.7 ns 0.0305 5,031 B 752 B
PR__EDIT2 19,281.6 ns 205.9 ns 189.4 ns 0.0305 6,525 B 600 B

Customer Impact

Decreased allocations, increased performance when initializing from directory.
I've observed about 3-4ms improvement when building from Fonts directory (351 files; 156 matches).

Regression

No.

Testing

Local build, initializing the collection with folders to look in, testing the functinality via FontFamily change.

Risk

Low, this is rather simple 1:1 swap. Options for FileSystemEnumerable were copied for GetFiles codepath.

Microsoft Reviewers: Open in CodeFlow

@h3xds1nz h3xds1nz requested review from a team as code owners September 26, 2024 10:58
@h3xds1nz h3xds1nz marked this pull request as draft September 26, 2024 11:17
@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 Sep 26, 2024
@h3xds1nz h3xds1nz marked this pull request as ready for review October 7, 2024 16:41
@h3xds1nz
Copy link
Contributor Author

h3xds1nz commented Oct 7, 2024

Resolved merge conflicts 11

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 Queue for test pass Status:Proposed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants