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

Remove dictionary conversion from SetParametersAsync source generator (#9682) #10205

Merged
merged 2 commits into from
Mar 8, 2025

Conversation

ShahryarSaljoughi
Copy link
Contributor

@ShahryarSaljoughi ShahryarSaljoughi commented Mar 7, 2025

closes #9682

Behaviour after this change:

This PR implements the changes we previously discussed with maintainers. Here is a brief of the implemented solution:

  • The generated SetParametersAsync method, initializes parameters defined within the enclosing class (does not initialize properties inherited from base type, or properties of the runtime type, that are not available in current type) and then calls the base implementation of SetParametersAsync method.
  • In case the base class is of type Microsoft.AspNetCore.Components.ComponentBase, the generated SetParametersAsync passes an empty ParameterView to base implementation, This is correct because parameters defined in derived types are already initialized during the call chains and the ComponentBase itself has nothing to initialize using the ParameterView
  • In case the base class is not Microsoft.AspNetCore.Components.ComponentBase, the generated SetParametersAsyn passes the same ParameterView it receives to its parent class if the parent class is not ComponentBase. (Does not delete values already assigned to parameters from the ParameterView anymore)

We prevented slow code, with one exception:

Our intention was to prevent the execution of reflectoin-based initializations which may happen if Microsoft.AspNetCore.Components.ComponentBase receives a not empty ParameterView in its SetParametersAsync.
There is only one scenario in which that slow code gets executed. If our component is neither directly inheriting from Microsoft.AspNetCore.Components.ComponentBase nor is covered by our source generator (maybe because it is not in our assembly), then that slow reflection based initialization is going to execute. The following shows an example of such scenario:

classDiagram
    ComponentBase <|-- ExternalComponent
    ExternalComponent <|-- MyComponent
Loading

MyComponent is covered by source generator.
ExternalComponent is in a third-party assembly and not covered by source generator.
ComponentBase is Microsoft.AspNetCore.Components.ComponentBase.

Summary by CodeRabbit

  • Refactor
    • Enhanced component parameter processing to improve stability and responsiveness during updates by streamlining how parameter information is handled.

…ensures that when passing ParameterView to base classes like InputBase<TValue> cascaded parameters are not lost.
Copy link

coderabbitai bot commented Mar 7, 2025

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

This update simplifies how parameters are handled in the SetParametersAsync method of the Blazor source generator. The change removes the line that deleted parameters from the dictionary and replaces the conversion of the dictionary into a ParameterView with a direct pass of the original parameters argument to the base method.

Changes

File Path Summary
src/SourceGenerators/.../BlazorSetParametersSourceGenerator.cs Removed the line that removed a parameter from the dictionary and modified the base method call to pass parameters directly, avoiding the dictionary-to-ParameterView conversion.

Sequence Diagram(s)

sequenceDiagram
    participant Client as Caller
    participant Generator as BlazorSetParametersSourceGenerator
    participant Base as BaseComponent

    Client->>Generator: Call SetParametersAsync(parameters)
    Generator->>Generator: Process parameters (no dictionary removal)
    Generator->>Base: Call base.SetParametersAsync(parameters)
    Base-->>Generator: Return result
    Generator-->>Client: Return result
Loading

Poem

I'm a rabbit with code so light,
Hopping through changes in plain sight.
No more tweaks that make you frown,
Clean logic now is the crown.
Parameters stay and flow just right,
Bound to bring our code delight!
🐰 Keep on hopping to new heights!


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/SourceGenerators/Bit.SourceGenerators/Blazor/BlazorSetParametersSourceGenerator.cs (1)

86-86: Improvement to parameter inheritance handling.

This change correctly implements the PR objective by passing the original parameters to the base class's SetParametersAsync method rather than creating a new ParameterView from the filtered dictionary. This ensures parameters are properly passed up the inheritance hierarchy and avoids reflection-based initializations that could degrade performance.

The previous approach unnecessarily removed parameters after processing them and then created a new ParameterView, which would have prevented parent components from accessing these parameters.

Consider adding a comment explaining the rationale behind this approach to help future maintainers understand the design decision:

 else
 {
+    // Pass the original parameters up the inheritance hierarchy to ensure parent components 
+    // can access all parameters and avoid reflection-based initializations
     source.AppendLine("            return base.SetParametersAsync(parameters);");
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cf597c3 and 3e0c0fe.

📒 Files selected for processing (1)
  • src/SourceGenerators/Bit.SourceGenerators/Blazor/BlazorSetParametersSourceGenerator.cs (1 hunks)

@ysmoradi
Copy link
Member

ysmoradi commented Mar 8, 2025

Greetings!
Do we need parametersDictionary anymore?
Shall we iterate on parameter view itself? Or even better, do the fact that we know which properties we've on the component, then we could call TryGetValue method of the ParameterView structure to generate a source like this:

parameters.TryGetValue<string>("Prop1", out Prop1);
parameters.TryGetValue<int>("Prop2", out Prop2);

@msynk
Copy link
Member

msynk commented Mar 8, 2025

@ShahryarSaljoughi thanks for submitting this PR.

to have the best performance in this method, it's better to follow the instruction provided by the Microsoft:

https://learn.microsoft.com/en-us/aspnet/core/blazor/performance?view=aspnetcore-9.0#implement-setparametersasync-manually

so plz remove the conversion to Dictionary and use the ParameterView directly.

Note: using the TryGetValue is not appropriate here, since it will iterate (foreach loop) over elements for each call.

@ShahryarSaljoughi
Copy link
Contributor Author

@msynk I'm on this, I'll come up to you in 2 hours. Thank you for your invaluable feedback, this is so good.

@ShahryarSaljoughi
Copy link
Contributor Author

@msynk Hi. I added a new commit. The new commit simply avoids conversion to dictionary and directly uses the ParameterView. It iterates once over all ParameterView parameters and initializes the component parameter properties.

@msynk msynk changed the title Modify SetParametersAsync source generator so that ParameterView climbs up inheritance hierarchy AS IS Remove dictionary conversion from SetParametersAsync source generator (#9682) Mar 8, 2025
@msynk msynk merged commit 2a9d9b7 into bitfoundation:develop Mar 8, 2025
3 checks passed
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.

BlazorSetParametersSourceGenerator interferes with capturing CascadedEditContext
3 participants