Skip to content

Add XamlObjectCreationFactory to improve the performance of XAML creation of objects #4026

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

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

Conversation

lindexi
Copy link
Member

@lindexi lindexi commented Jan 20, 2021

See: #4022

This feature can improve the performance of XAML object creation by three times

Benchmark:

BenchmarkDotNet=v0.12.1, OS=Windows 10.0.19041.746 (2004/?/20H1)
Intel Core i7-6700 CPU 3.40GHz (Skylake), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=5.0.101
[Host] : .NET Core 5.0.1 (CoreCLR 5.0.120.57516, CoreFX 5.0.120.57516), X64 RyuJIT
DefaultJob : .NET Core 5.0.1 (CoreCLR 5.0.120.57516, CoreFX 5.0.120.57516), X64 RyuJIT

Method Mean Error StdDev Ratio RatioSD
CreateInstanceInXamlTypeInvokerOld 605.8 ns 9.75 ns 13.67 ns 1.00 0.00
CreateInstanceWhichRegisterInXamlObjectCreationFactory 197.5 ns 3.28 ns 4.26 ns 0.33 0.01
CreateInstanceWhichNotRegisterInXamlObjectCreationFactory 641.1 ns 12.88 ns 18.06 ns 1.06 0.04

CreateInstanceInXamlTypeInvokerOld: The old code.

CreateInstanceWhichRegisterInXamlObjectCreationFactory: The type of the created object is registered to XamlObjectCreationFactory in advance.

CreateInstanceWhichNotRegisterInXamlObjectCreationFactory: The type of the created object is not found in XamlObjectCreationFactory

Benchmark code: https://github.com/lindexi/lindexi_gd/blob/8f95aafa/System.Xaml/System.Xaml.Demo/Program.cs

This will hardly make the original performance worse, if we do not use this feature. Benchmark code: https://github.com/lindexi/lindexi_gd/blob/58bbf616/System.Xaml/System.Xaml.Demo/Program.cs

BenchmarkDotNet=v0.12.1, OS=Windows 10.0.19041.746 (2004/?/20H1)
Intel Core i7-6700 CPU 3.40GHz (Skylake), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=5.0.101
[Host] : .NET Core 5.0.1 (CoreCLR 5.0.120.57516, CoreFX 5.0.120.57516), X64 RyuJIT
DefaultJob : .NET Core 5.0.1 (CoreCLR 5.0.120.57516, CoreFX 5.0.120.57516), X64 RyuJIT

Method Mean Error StdDev Ratio RatioSD
CreateInstanceInXamlTypeInvokerOld 619.6 ns 12.38 ns 11.58 ns 1.00 0.00
CreateInstanceWhichNotRegisterInXamlObjectCreationFactory 582.9 ns 7.26 ns 6.79 ns 0.94 0.02

About Ioc

Register by IServiceCollection :

        public static void BuildWpfXamlIoc(IServiceCollection serviceCollection)
        {
            var serviceProvider = serviceCollection.BuildServiceProvider();
            foreach (var serviceDescriptor in serviceCollection)
            {
                XamlObjectCreationFactory.RegisterCreator(serviceDescriptor.ImplementationType,
                    () => serviceDescriptor.ImplementationFactory(serviceProvider));
            }
        }

See #499

@lindexi lindexi requested a review from a team as a code owner January 20, 2021 01:33
@ghost ghost added the PR metadata: Label to tag PRs, to facilitate with triage label Jan 20, 2021
@ghost ghost requested review from fabiant3, ryalanms and SamBent January 20, 2021 01:33
lindexi added a commit to lindexi/lindexi_gd that referenced this pull request Jan 20, 2021
lindexi added a commit to lindexi/lindexi_gd that referenced this pull request Jan 20, 2021
lindexi added a commit to lindexi/lindexi_gd that referenced this pull request Jan 20, 2021
lindexi added a commit to lindexi/lindexi_gd that referenced this pull request Jan 20, 2021
lindexi added a commit to lindexi/lindexi_gd that referenced this pull request Jan 20, 2021
lindexi added a commit to lindexi/lindexi_gd that referenced this pull request Jan 20, 2021
@ryalanms ryalanms added Performance Performance related issue Untriaged labels Feb 4, 2021
Base automatically changed from master to main March 17, 2021 17:38
@ghost ghost assigned lindexi May 13, 2022
@pchaurasia14 pchaurasia14 added the Community Contribution A label for all community Contributions label Jul 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Review Requested Community Contribution A label for all community Contributions Performance Performance related issue PR metadata: Label to tag PRs, to facilitate with triage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants