refactor: Optimize MobileBy FindElements to reduce list allocations - #1103
Merged
Dor-bl merged 2 commits intoAug 21, 2026
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`.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors how MobileBy converts IReadOnlyCollection<IWebElement> results into ReadOnlyCollection<IWebElement>, aiming to reduce unnecessary allocations by avoiding eager .ToList().AsReadOnly() conversions and centralizing the conversion logic in a shared helper.
Changes:
- Replaced multiple
.ToList().AsReadOnly()usages inMobileBy.FindElements(...)overrides with a newAsReadOnly()extension method. - Added
ReadOnlyCollectionExtensions.AsReadOnly<T>(this IReadOnlyCollection<T>)to standardize conversion toReadOnlyCollection<T>. - Removed
using System.Linq;fromMobileBy.cssince it’s no longer directly needed there.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Appium.Net/Appium/MobileBy.cs | Switches element-collection conversion to the new AsReadOnly() helper to reduce per-call allocations. |
| src/Appium.Net/Appium/ReadOnlyCollectionExtensions.cs | Introduces a shared conversion helper for IReadOnlyCollection<T> → ReadOnlyCollection<T>. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+1
to
+18
| using System.Collections.Generic; | ||
| using System.Collections.ObjectModel; | ||
| using System.Linq; | ||
|
|
||
| namespace OpenQA.Selenium.Appium | ||
| { | ||
| internal static class ReadOnlyCollectionExtensions | ||
| { | ||
| internal static ReadOnlyCollection<T> AsReadOnly<T>(this IReadOnlyCollection<T> collection) | ||
| { | ||
| if (collection is ReadOnlyCollection<T> readOnlyCollection) | ||
| { | ||
| return readOnlyCollection; | ||
| } | ||
| return new ReadOnlyCollection<T>(collection as IList<T> ?? collection.ToList()); | ||
| } | ||
| } | ||
| } |
KazuCocoa
approved these changes
Aug 21, 2026
nvborisenko
reviewed
Aug 21, 2026
| { | ||
| return readOnlyCollection; | ||
| } | ||
| return new ReadOnlyCollection<T>(collection as IList<T> ?? collection.ToList()); |
Contributor
There was a problem hiding this comment.
potential NRE at collection.ToList()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
List of changes
This pull request refactors how
ReadOnlyCollection<IWebElement>is created in theMobileByclass, improving code efficiency and consistency. The main change is the removal of unnecessary.ToList()calls before converting collections to read-only, and the introduction of a new extension method to handle this conversion more robustly.Refactoring and Code Simplification
.ToList().AsReadOnly()with a newAsReadOnly()extension method, which ensures that collections are converted toReadOnlyCollection<T>efficiently and safely. (src/Appium.Net/Appium/MobileBy.cs,src/Appium.Net/Appium/ReadOnlyCollectionExtensions.cs) [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]Code Organization
ReadOnlyCollectionExtensions.cs, containing theAsReadOnlyextension method forIReadOnlyCollection<T>, centralizing the logic for converting collections.Minor Cleanup
using System.Linq;directive fromMobileBy.cssince it is no longer needed after the refactor.These changes make the codebase cleaner, reduce unnecessary list allocations, and improve maintainability.
Types of changes
What types of changes are you proposing/introducing to the .NET client?
Put an
xin the boxes that applyDocumentation
This can be done by navigating to the documentation section on http://appium.io selecting the appropriate command/endpoint and clicking the 'Edit this doc' link to update the C# example
Integration tests
Details
Please provide more details about changes if necessary. You can provide code samples showing how they work and possible use cases if there are new features. Also, you can create gists with pasted C# code samples or put them here using markdown.
About markdown please read Mastering markdown and Writing on GitHub