Skip to content

Optimize codegen for generated dependency property setters#792

Merged
Arlodotexe merged 10 commits intomainfrom
user/sergiopedri/optimize-dp-codegen
Apr 15, 2026
Merged

Optimize codegen for generated dependency property setters#792
Arlodotexe merged 10 commits intomainfrom
user/sergiopedri/optimize-dp-codegen

Conversation

@Sergio0694
Copy link
Copy Markdown
Member

This pull request introduces an optimization to the DependencyPropertyGenerator by leveraging specialized XamlBindingHelper.SetPropertyFrom* methods for supported property types. This results in more efficient generated code for property setters, while maintaining compatibility with user-defined callbacks and fallback logic for unsupported types. The changes also refactor and extend the generator’s internal APIs to support this feature.

Optimized setter generation using XamlBindingHelper:

  • The generator now detects if a property type supports a specialized XamlBindingHelper.SetPropertyFrom* method (e.g., SetPropertyFromBoolean, SetPropertyFromInt32). If so, it emits a direct call to this method in the generated setter for improved performance. This optimization is skipped if the property type is object or if the user has implemented a custom On<PropertyName>Set(ref object) callback. [1] [2] [3] [4]

API and model extensions to support the optimization:

  • Added a new XamlBindingHelperSetMethodName property to the DependencyPropertyInfo record, and updated the generator pipeline to compute and pass this value. This enables the code generation logic to conditionally emit the optimized setter code. [1] [2] [3]

Well-known type name utilities:

  • Introduced a utility method XamlBindingHelper(bool useWindowsUIXaml) in WellKnownTypeNames.cs to resolve the fully qualified type name for XamlBindingHelper, supporting both Windows and Microsoft UI XAML namespaces.

Code generation logic updates:

  • Modified the setter and partial method generation logic to use the optimized path when available, and to only emit the boxed On<PropertyName>Set(ref object) partial method when the optimization is not used. [1] [2] [3] [4] [5]

These changes improve the efficiency of generated dependency property code and maintain backward compatibility for advanced scenarios.

@Sergio0694 Sergio0694 force-pushed the user/sergiopedri/optimize-dp-codegen branch from d4c7c8d to 6a789f1 Compare April 14, 2026 21:13
Sergio0694 and others added 2 commits April 14, 2026 16:49
Add support for using XamlBindingHelper.SetPropertyFrom* methods to optimize generated dependency property setters. Introduces WellKnownTypeNames.XamlBindingHelper to resolve the fully-qualified type name, adds Execute.GetXamlBindingHelperSetMethodName and Execute.IsObjectSetCallbackImplemented to detect applicable types and user overrides, and wires the chosen method into the generator pipeline. Generation now emits direct XamlBindingHelper calls when available and falls back to boxed SetValue path otherwise. Also update DependencyPropertyInfo to carry the XamlBindingHelperSetMethodName.
Replace manual boxing and SetValue calls with global::Windows.UI.Xaml.Markup.XamlBindingHelper.SetPropertyFrom* calls across the dependency property generator tests. Update DataRow entries to include the target set method name and refactor the test helper to compute the setter body based on an optional setMethodName parameter. Add a new test (SingleProperty_Int32_WithNoCaching_WithObjectSetCallback) covering an int property with an object-based OnNumberSet callback to validate generated code that mixes object set callbacks and optimized XamlBindingHelper setters.

Co-Authored-By: Copilot <223556219+Copilot@users.noreply.github.com>
@Sergio0694 Sergio0694 force-pushed the user/sergiopedri/optimize-dp-codegen branch from 6a789f1 to 5187a35 Compare April 14, 2026 23:52
Replace manual boxing and SetValue call in the generated Name property setter with a call to global::Windows.UI.Xaml.Markup.XamlBindingHelper.SetPropertyFromString(this, NameProperty, value) to avoid temporary object boxing and better align with XAML binding semantics. Also add a missing [TestMethod] attribute to the SingleProperty_Int32_WithNoCaching_WithDefaultValue_WithCallback test so it will be executed.
@Sergio0694 Sergio0694 force-pushed the user/sergiopedri/optimize-dp-codegen branch from 5187a35 to f6cda27 Compare April 14, 2026 23:53
Arlodotexe and others added 7 commits April 14, 2026 20:21
Adjust the expected value in Test_DependencyPropertyGenerator.cs for the verbatim string representing the Windows root path (C: backslash) so the expected literal uses the correct escaped backslash. This corrects an incorrect expected literal and prevents the test from failing due to improper escaping.
Provide a test AnalyzerConfigOptionsProvider and wire it into the generator tests. Added DependencyPropertyGeneratorAnalyzerConfigOptionsProvider which exposes a singleton AnalyzerConfigOptionsProvider with GlobalOptions setting build_property.DependencyPropertyGeneratorUseWindowsUIXaml=true (plus a small SimpleAnalyzerConfigOptions implementation). Updated CSharpGeneratorTest{TGenerator}.cs to pass this optionsProvider when creating the CSharpGeneratorDriver (and to create generators via AsSourceGenerator where applicable) so the dependency property generator sees the expected MSBuild property during tests.
Reformat the computed setterBody strings in Test_DependencyPropertyGenerator to use multi-line raw string literals and adjust the indentation where they are injected (for Name and IsSelected cases). This is a whitespace/formatting change to improve readability and consistency; no functional behavior changes are intended.
Add a /.globalconfig analyzer config to test helper classes to enable Windows UI XAML for the DependencyPropertyGenerator tests. Sets build_property.DependencyPropertyGeneratorUseWindowsUIXaml = true in CSharpAnalyzerTest, CSharpCodeFixTest and CSharpSuppressorTest so the generator uses Windows.UI.Xaml APIs during tests.
@Arlodotexe Arlodotexe enabled auto-merge April 15, 2026 23:32
@Arlodotexe Arlodotexe merged commit b745f81 into main Apr 15, 2026
23 of 24 checks passed
@Sergio0694 Sergio0694 deleted the user/sergiopedri/optimize-dp-codegen branch April 16, 2026 00:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants