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

Reduce generated invocations to FindNameScope #15370

Merged

Conversation

Rekkonnect
Copy link
Contributor

What does the pull request do?

Reduces the invocations of the FindNameScope method on this on generated InitializeComponent. See #15365

What is the current behavior?

For every named control, the generated code invokes FindNameScope within InitializeComponent.

What is the updated/expected behavior with this PR?

The FindNameScope method is invoked only once if there are any named controls that would use this method.

Checklist

  • Adjusted existing unit tests' expected generated code
  • Added XML documentation to any related classes?

Breaking changes

None, assuming FindNameScope does not return different results during the execution of InitializeComponent.

Fixed issues

Fixes #15365

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.2.999-cibuild0047288-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

Copy link
Member

@maxkatz6 maxkatz6 left a comment

Choose a reason for hiding this comment

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

LGTM, thank you!

@maxkatz6 maxkatz6 added this pull request to the merge queue Apr 15, 2024
Merged via the queue into AvaloniaUI:master with commit bca559b Apr 15, 2024
10 checks passed
@@ -22,7 +22,8 @@ namespace Sample.App
AvaloniaXamlLoader.Load(this);
}

UserNameTextBox = this.FindNameScope()?.Find<global::Avalonia.Controls.TextBox>("UserNameTextBox");
var __thisNameScope__ = this.FindNameScope();
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the prefix and suffic double underscores following some convention I'm unaware of? Otherwise, naming conventions are important and this probably shouldn't have been done. thisNameScope would be fine IMO

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My intention was to avoid any potential conflicts, hence the uncommon naming. This is still only a generated local so it won't ever leak outside, and thus barely matters imo

Copy link
Member

Choose a reason for hiding this comment

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

Chance of conflicts is pretty low, but not zero.

With IL code generation we usually generate some obscure names like "!CompiledXaml" that can't be really compiled via C#. It's not possible with source generators though.

Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking this is a local variable so why even worry about it. But there is risk of conflicts with control names. That said, we probably should have a standard for this. I have seen two underscore prefixes used for generated variables but never as suffixes too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Accessing instance members with this would solve the problem, I think. No special naming would be needed. Probably static members would have to be qualified with the current type name as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

@jp2masa I had the same thought after my comment above and was going to check it later to make sure. But if you are thinking the same that's probably a better solution.

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 don't know why we're arguing over a generated local's name that has already been merged. This is too much mental effort for something that literally doesn't impact users.

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, it was merged awful quickly. I try to read through what I can just to be aware. I agree it's low impact and can be changed later as/if required. I wouldn't shut down discussion like this though, long-term it's probably useful especially as we add more generated code.

Copy link
Member

Choose a reason for hiding this comment

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

Since a local/parameter always has priority, imo it's usually best in source generators to make sure member references are correctly qualified, and leave the local/parameters "unescaped".
e.g. this.Property = someLocal; instead of Property = __someLocal__

I agree with Robloo, there's no arguing here, just discussion that could help future PRs.

@maxkatz6 maxkatz6 added the backport-candidate-11.1.x Consider this PR for backporting to 11.1 branch label May 8, 2024
@maxkatz6 maxkatz6 added backported-11.1.x and removed backport-candidate-11.1.x Consider this PR for backporting to 11.1 branch labels May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FindNameScope is being called once per named component assignment
6 participants