⚡ perf: Avoid redundant list allocations in MobileBy FindElements - #202
⚡ perf: Avoid redundant list allocations in MobileBy FindElements#202Dor-bl wants to merge 2 commits into
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.
|
Closing this PR as superseded — the same optimization (replacing Generated by Claude Code |
💡 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