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

[dotnet] Trim away CDP when publishing AOT apps #15217

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Feb 2, 2025

User description

Thanks for contributing to Selenium!
A PR well described will help maintainers to quickly review and merge it

Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, help reviewers by making them as simple and short as possible.

Motivation and Context

Sample app:

Console.WriteLine("Start");

Log.SetLevel(LogEventLevel.Trace);

var options = new ChromeOptions { UseWebSocketUrl = true, BrowserVersion = "131" };

using var driver = new ChromeDriver(options);

driver.Url = "https://www.google.com";

Thread.Sleep(2500);
driver.FindElements(By.CssSelector("div"));

Size before PR: 9.0 MB
Size after PR: 6.7 MB

Size savings: 2.3 MB
image

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

PR Type

Enhancement


Description

  • Optimized the WebDriver class to reduce application size.

  • Deferred initialization of NetworkManager to save resources.

  • Removed unused or redundant code for cleaner implementation.


Changes walkthrough 📝

Relevant files
Enhancement
WebDriver.cs
Optimize and streamline `WebDriver` initialization             

dotnet/src/webdriver/WebDriver.cs

  • Removed direct initialization of NetworkManager.
  • Deferred NetworkManager initialization using a null-coalescing
    assignment.
  • Deleted unused Network property setter.
  • Cleaned up redundant code for better maintainability.
  • +1/-5     

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • @RenderMichael RenderMichael marked this pull request as ready for review February 2, 2025 03:45
    Copy link
    Contributor

    qodo-merge-pro bot commented Feb 2, 2025

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Thread Safety

    The lazy initialization of Network property using null-coalescing assignment is not thread-safe. Multiple threads accessing this property simultaneously could potentially create multiple NetworkManager instances.

    internal INetwork Network => this.network ??= new NetworkManager(this);

    Copy link
    Contributor

    qodo-merge-pro bot commented Feb 2, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add thread-safe initialization pattern

    Add null check before accessing this.network to prevent potential
    NullReferenceException in multi-threaded scenarios. The current implementation might
    have race conditions.

    dotnet/src/webdriver/WebDriver.cs [218]

    -internal INetwork Network => this.network ??= new NetworkManager(this);
    +internal INetwork Network
    +{
    +    get
    +    {
    +        if (this.network == null)
    +        {
    +            lock (this)
    +            {
    +                this.network ??= new NetworkManager(this);
    +            }
    +        }
    +        return this.network;
    +    }
    +}
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion addresses a critical thread-safety issue by implementing a double-checked locking pattern, which prevents potential race conditions during NetworkManager initialization in multi-threaded environments.

    8
    Learned
    best practice
    Use modern C# pattern matching instead of explicit null checks when checking interface implementation

    While the PR introduces the null-coalescing assignment (??=) operator for the
    Network property, the null check for ISupportsLogs interface could be simplified
    using the is pattern matching operator for more concise and modern C# code.

    dotnet/src/webdriver/WebDriver.cs [81]

    -if ((this as ISupportsLogs) != null)
    +if (this is ISupportsLogs)
     {
    • Apply this suggestion
    6

    {
    get { return this.network; }
    }
    internal INetwork Network => this.network ??= new NetworkManager(this);
    Copy link
    Contributor Author

    @RenderMichael RenderMichael Feb 2, 2025

    Choose a reason for hiding this comment

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

    NetworkManager captures IDevTools.GetDevToolsSession in the constructor, which pulls in many CDP types.

    We do not want to touch NetworkManager unless user calls driver.Network.

    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.

    1 participant