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

FindNameScope is being called once per named component assignment #15365

Closed
Rekkonnect opened this issue Apr 14, 2024 · 2 comments · Fixed by #15370
Closed

FindNameScope is being called once per named component assignment #15365

Rekkonnect opened this issue Apr 14, 2024 · 2 comments · Fixed by #15370

Comments

@Rekkonnect
Copy link
Contributor

Describe the bug

Avalonia.Generators.NameGenerator.AvaloniaNameSourceGenerator generates InitializeComponent and invokes FindNameScope as many times as there are named components within the control.

To Reproduce

  1. Create any control (user or templated)
  2. Add some components to the control like panels, text blocks, etc.
  3. Set the Name attribute to name some of the contained controls
  4. Go to the definition of InitializeComponent for the control
  5. Observe the generated source

Example of generated code:

    partial class ContentEditor
    {
        internal global::Avalonia.Controls.Panel backgroundPanel;
        internal global::Avalonia.Controls.Panel linePanel;
        internal global::Avalonia.Controls.Panel scrollBarPanel;
        internal global::Avalonia.Controls.StackPanel contentStackPanel;

        /// <summary>
        /// Wires up the controls and optionally loads XAML markup and attaches dev tools (if Avalonia.Diagnostics package is referenced).
        /// </summary>
        /// <param name="loadXaml">Should the XAML be loaded into the component.</param>

        public void InitializeComponent(bool loadXaml = true)
        {
            if (loadXaml)
            {
                AvaloniaXamlLoader.Load(this);
            }

            backgroundPanel = this.FindNameScope()?.Find<global::Avalonia.Controls.Panel>("backgroundPanel");
            linePanel = this.FindNameScope()?.Find<global::Avalonia.Controls.Panel>("linePanel");
            scrollBarPanel = this.FindNameScope()?.Find<global::Avalonia.Controls.Panel>("scrollBarPanel");
            contentStackPanel= this.FindNameScope()?.Find<global::Avalonia.Controls.StackPanel>("contentStackPanel");
        }
    }

Expected behavior

The FindNameScope method's result can be stored in a variable before each component assignment. If there are no named components, the method should not be invoked at all, and no variable will have to be used for storing its result.

Avalonia version

11.0.10

OS

No response

Additional context

The FindNameScope method, to my understanding, will always return the same result within InitializeComponent when invoked on this. This results in redundant invocations of the method, which costs performance upon initializing an instance of the control.

@Rekkonnect Rekkonnect added the bug label Apr 14, 2024
@timunie
Copy link
Contributor

timunie commented Apr 14, 2024

Not sure it's a bug. But still a good idea. Would be worth a PR with a benchmark to test performance gain, if you are interested in.

@Rekkonnect
Copy link
Contributor Author

I used the bug report template by mistake. I'll consider a PR for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants