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

Source Generator Performance Improvements #19990

Merged
merged 4 commits into from
Jan 26, 2024
Merged

Conversation

mgoertz-msft
Copy link
Contributor

@mgoertz-msft mgoertz-msft commented Jan 19, 2024

Description of Change

Source Generator Performance Improvements:

  • Reversed lookup order (Extension second)
  • Introduced type cache
  • Restored lookup order (Extension first again)
PERF PROGRESS
Updated numbers. Not quite as dramatic but still a good improvement. The original measurements had a TracePoint skewing the timing results quite heavily. The new numbers only use a StopWatch in code with the TracePoint disabled.

SourceGen - Maui.Controls.Sample (262 XAML files)

1) Unchanged:
- 15640 GetTypeByMetadata calls
~ 4s

2) Extensions lookup in XmlTypeXamlExtensions.GetTypeReference() second
- 6232 GetTypeByMetadata calls (~60% reduction)
~ 2.6s                         (~35% reduction)

3) With type cache and Extension lookup second
- 203 GetTypeByMetadata calls  (~99% reduction)
~ 1.5s                         (~62% reduction => ~2.6 times faster)

4) With just a type cache
- 523 GetTypeByMetadata calls  (~97% reduction)
~ 1.5s                         (~62% reduction => ~2.6 times faster)

Issues Fixed

Fixes #19944 GetTypeNameFromCustomNamespace does not cache duplicate results

@Eilon Eilon added the area-xaml XAML, CSS, Triggers, Behaviors label Jan 20, 2024
@StephaneDelcroix
Copy link
Contributor

/rebase

- Reversed lookup order (Extension second)
- Introduced type cache

```
PERF PROGRESS:
SourceGen - Maui.Controls.Sample (262 XAML files)

1) Unchanged:
- 15640 GetTypeByMetadata calls
- 223s

2) Extensions lookup in XmlTypeXamlExtensions.GetTypeReference() second
- 6232 GetTypeByMetadata calls (~60% reduction)
- 90s                          (~60% reduction)

3) With type cache
- 203 GetTypeByMetadata calls (~97% reduction => ~99% total reduction)
- 6s                          (~93% reduction => ~97% total reduction => 37 times faster!)
```
Copy link
Member

@rmarinho rmarinho left a comment

Choose a reason for hiding this comment

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

It's failing the build..

VisualRunner/Pages/TestAssemblyPage.xaml(17,30): XamlC error XFC0009: No property, BindableProperty, or event found for "Type", or mismatching type between value and property. [/Users/builder/azdo/_work/2/s/src/TestUtils/src/DeviceTests.Runners/TestUtils.DeviceTests.Runners.csproj::TargetFramework=net8.0-android]

@mgoertz-msft
Copy link
Contributor Author

Thanks, saw that too. I'm not sure why that would be failing now. Need to look into that...

…d potentially breaking behavior for scenarios besides x:Array. In this case XamlC preferred System.Array over ArrayExtension for x:Array. With the typecache it made no measurable difference in performance.
@mgoertz-msft
Copy link
Contributor Author

@rmarinho Could you please review again? I had to switch the Extension lookup order back because it preferred System.Array over ArrayExtension for x:Array, and it's probably safer to keep this way in case there are other scenarios like this that we may not be aware about. The cache brought the real improvement so this lookup switch wasn't needed anyway.

@@ -0,0 +1,8 @@
{
"profiles": {
Copy link
Member

Choose a reason for hiding this comment

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

Is this something we want to check in ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually think we do. It provides a simple debug target to debug the source generator.

Copy link
Member

@rmarinho rmarinho left a comment

Choose a reason for hiding this comment

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

Seems good just a comment about the launchSettings, and I wonder if we could add a Benchmark around this change? But not blocking to get it merged.

@mgoertz-msft mgoertz-msft merged commit 08f1f61 into main Jan 26, 2024
47 checks passed
@mgoertz-msft mgoertz-msft deleted the dev/mgoertz/typecache branch January 26, 2024 16:07
@mgoertz-msft
Copy link
Contributor Author

I wonder if we could add a Benchmark around this change?

How do you do that? Do you have an example?

@jonathanpeppers
Copy link
Member

There is a BenchmarkDotNet project, but it's not setup to test things like source generators or MSBuild tasks:

https://github.com/dotnet/maui/tree/main/src/Core/tests/Benchmarks/Benchmarks

In theory, we could make a Benchmarks.Build or Benchmarks.Tooling project for things like this.

rmarinho added a commit that referenced this pull request Jan 29, 2024
* Add Obsolete tag for old IndexOf (#20068)

* Split the InputTransparency tests (#19925)

* Bump Xamarin.UITest to 4.3.4 (#20067)

* Bump Xamarin.UITest to 4.3.4

* Update NUnit

* Restore dotnet tool (#20106)

* Do not use underscores in the ApplicationId (#19377)

* Do not use underscores in the ApplicationId

Underscores are not supported on Windows

* Update DotnetInternal.cs

* [WinUI] Add workaround for Connectivity check on Win10  (#19261)

* Implemented a Win10 work-around for connectivity thread issue

Reimplement `ConnectivityImplementation.ConnectionProfiles` to use native .net core APIs

* Remove prop bag ID

* Replace network availability changed event with native .net API

* Remove explicit file include

* Fix file naming

* Implemented a Win10 work-around for connectivity thread issue

Reimplement `ConnectivityImplementation.ConnectionProfiles` to use native .net core APIs

* Remove prop bag ID

* Replace network availability changed event with native .net API

* Remove explicit file include

* Fix file naming

* * Revert change to remove `Windows.Networking.Connectivity.NetworkInformation.NetworkStatusChanged`

* Update Connectivity.uwp.cs

---------

Co-authored-by: Mike Corsaro <[email protected]>
Co-authored-by: Matthew Leibowitz <[email protected]>

* [Windows] Adjust recycle check in ItemContentControl (#20079)

* Adjust recycle check in `ItemContentControl` to use Content property

* Fix confusing line

* Rectify the scopes in the builder (#19932)

* Update System.Drawing.Common (#20122)

* Enable building WASDK Self-Contained packaged apps (#20019)

* Translucent and Transparent NavigationBar on iOS (#19204)

* Changes for enabling translucent navigation bar ios

* Add UITest for NavigationPage Safe Area Translucence

* remove UIKit

* Move UITest from gallery to Issues

* push a new page for UITests, consolidate code, and save shadowimage

* Changes for enabling translucent navigation bar ios

* Add UITest for NavigationPage Safe Area Translucence

* remove UIKit

* Move UITest from gallery to Issues

* make the extension method

* use the background color alpha for translucent

* use same alpha logic for pre ios 15

* accidently added testing comments

* add more UITests for the different NavigationPage and Flyout Page scenarios

* use additionalsafeareainsets for the secondary toolbar

* missing new line

* only run the secondary toolbar offset when we have a secondary toolbar

* use existing childViewControllers

* remove the shadow stuff and simplify the expression with null

* style and fixes from merge conflicts

* standardAppearance and scrolledgeappearance should be kept in

* changes after talking with Shane

* change shell behavior to be more like navrenderer with preiOS13

* move code if we are transparent pre13 shell

* remove new lines

* add screenshot tests

* be able to remove and add secondarybar additionalsafeareas

* reset the xaml on the Sandboc MainPage (#20082)

* [Android] Fix gif animation initial state (#14295)

* Fix gifs initial animation state on Android

* Device tests

* Auto-format source code

* Updated sample

* Updated device tests

* Refactor code

* Update src/Core/src/Handlers/Image/ImageHandler.Android.cs

Co-authored-by: Matthew Leibowitz <[email protected]>

* Remove unnecessary change

* Fix build errors

* Merge branch 'main' into fix-7019

* Fix merge mistake

---------

Co-authored-by: GitHub Actions Autoformatter <[email protected]>
Co-authored-by: Javier Suárez <[email protected]>
Co-authored-by: Matthew Leibowitz <[email protected]>
Co-authored-by: Gerald Versluis <[email protected]>

* Enable Windows Image device tests (#20167)

* [X] allow x:Type extension for BPConverter (#18540)

- fixes #18324

* Source Generator Performance Improvements (#19990)

* Source Generator Performance Improvements:
- Reversed lookup order (Extension second)
- Introduced type cache

```
PERF PROGRESS:
SourceGen - Maui.Controls.Sample (262 XAML files)

1) Unchanged:
- 15640 GetTypeByMetadata calls
- 223s

2) Extensions lookup in XmlTypeXamlExtensions.GetTypeReference() second
- 6232 GetTypeByMetadata calls (~60% reduction)
- 90s                          (~60% reduction)

3) With type cache
- 203 GetTypeByMetadata calls (~97% reduction => ~99% total reduction)
- 6s                          (~93% reduction => ~97% total reduction => 37 times faster!)
```

* - Set uinitial lookupNames capacity to 2 since there won't be more than 2

* Added appium UITest for FlyoutNavigationBetweenItemsWithNavigationStacks (#19444) Fixes #16675

* Add better exception for missing Maps on Windows (#19046)

* Add better exception for missing Maps on Windows

* Update AppHostBuilderExtensions.cs

* [ci] Add missing demands (#20183)

* Add comments to internal methods for XAML Hot Reload usage (#20215)

* Add comments to internal methods for XAML Hot Reload usage

* spacing

* Adding Mobile tag to MAUI project templates (#20191)

Co-authored-by: Luke Westendorf <[email protected]>

---------

Co-authored-by: Tim Miller <[email protected]>
Co-authored-by: Matthew Leibowitz <[email protected]>
Co-authored-by: Gerald Versluis <[email protected]>
Co-authored-by: Mike Corsaro <[email protected]>
Co-authored-by: Mike Corsaro <[email protected]>
Co-authored-by: TJ Lambert <[email protected]>
Co-authored-by: Javier Suárez <[email protected]>
Co-authored-by: GitHub Actions Autoformatter <[email protected]>
Co-authored-by: Javier Suárez <[email protected]>
Co-authored-by: Stephane Delcroix <[email protected]>
Co-authored-by: Marco Goertz <[email protected]>
Co-authored-by: MSLukeWest <[email protected]>
Co-authored-by: Luke Westendorf <[email protected]>
PureWeen pushed a commit that referenced this pull request Feb 8, 2024
* Source Generator Performance Improvements:
- Reversed lookup order (Extension second)
- Introduced type cache

```
PERF PROGRESS:
SourceGen - Maui.Controls.Sample (262 XAML files)

1) Unchanged:
- 15640 GetTypeByMetadata calls
- 223s

2) Extensions lookup in XmlTypeXamlExtensions.GetTypeReference() second
- 6232 GetTypeByMetadata calls (~60% reduction)
- 90s                          (~60% reduction)

3) With type cache
- 203 GetTypeByMetadata calls (~97% reduction => ~99% total reduction)
- 6s                          (~93% reduction => ~97% total reduction => 37 times faster!)
```

* - Set uinitial lookupNames capacity to 2 since there won't be more than 2
@github-actions github-actions bot locked and limited conversation to collaborators Feb 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GetTypeNameFromCustomNamespace does not cache duplicate results
6 participants