⚡ perf: Avoid redundant list allocations in MobileBy FindElements#202
⚡ perf: Avoid redundant list allocations in MobileBy FindElements#202
Conversation
Replaced unnecessary `ToList().AsReadOnly()` calls with a smart `AsReadOnly()` extension method that uses the underlying `IList` if available. This avoids redundant O(N) memory allocations and array copies when wrapping the results of `FindElements`.
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
Pull request overview
Improves MobileBy.FindElements performance by avoiding redundant list materialization when converting IReadOnlyCollection<T> results into ReadOnlyCollection<T>.
Changes:
- Added an internal
AsReadOnly<T>(this IReadOnlyCollection<T>)extension that reuses existingReadOnlyCollection<T>instances or wrapsIList<T>directly. - Updated
MobileByFindElementsimplementations to use the new extension instead of.ToList().AsReadOnly().
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/Appium.Net/Appium/ReadOnlyCollectionExtensions.cs | Adds an internal extension to efficiently produce ReadOnlyCollection<T> without unnecessary copies. |
| src/Appium.Net/Appium/MobileBy.cs | Switches multiple FindElements paths to the new extension method to reduce allocations. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Collections.ObjectModel; | ||
| using System.Linq; |
There was a problem hiding this comment.
using System; is unused in this new file; removing it avoids compiler warnings and keeps the usings minimal.
| { | ||
| internal static class ReadOnlyCollectionExtensions | ||
| { | ||
| public static ReadOnlyCollection<T> AsReadOnly<T>(this IReadOnlyCollection<T> collection) |
There was a problem hiding this comment.
ReadOnlyCollectionExtensions is internal, so this public method is effectively internal anyway. Consider making the method internal as well to reflect its intended visibility and avoid confusing API surface expectations inside the assembly.
| public static ReadOnlyCollection<T> AsReadOnly<T>(this IReadOnlyCollection<T> collection) | |
| internal static ReadOnlyCollection<T> AsReadOnly<T>(this IReadOnlyCollection<T> collection) |
| public override ReadOnlyCollection<IWebElement> FindElements(ISearchContext context) | ||
| { | ||
| if (context is IFindsByFluentSelector<IWebElement> finder) | ||
| return finder.FindElements(_searchingCriteriaName, selector).ToList().AsReadOnly(); | ||
| return finder.FindElements(_searchingCriteriaName, selector).AsReadOnly(); | ||
| throw new InvalidCastException($"Unable to cast {context.GetType().FullName} " + |
There was a problem hiding this comment.
After replacing .ToList().AsReadOnly() with .AsReadOnly(), this file no longer appears to use any LINQ extensions. using System.Linq; at the top of MobileBy.cs can likely be removed to avoid an unused-using warning.
💡 What:
Replaced
ToList().AsReadOnly()with an internal extension methodAsReadOnly<T>()inReadOnlyCollectionExtensionsthat directly wrapsIList<T>instances intoReadOnlyCollection<T>or casts them if they already are, avoiding intermediateList<T>creation.🎯 Why:
The
FindElementsmethods returned anIReadOnlyCollection<IWebElement>(which is almost always internally implemented as an array orList). Calling.ToList()created a redundant array copy of all elements, incurring O(N) time and memory overhead.📊 Measured Improvement:
A benchmark simulating the underlying operations (wrapping an
IListintoReadOnlyCollectionvs calling.ToList().AsReadOnly()) on a 1000-element collection for 100,000 iterations showed:.ToList().AsReadOnly()): ~336msnew ReadOnlyCollectionwrapper): ~1msPR created automatically by Jules for task 6392073121482763632 started by @Dor-bl