Skip to content

⚡ Optimize ToDictionary() in PointerInputDevice#200

Open
Dor-bl wants to merge 2 commits intomainfrom
perf-pointerinputdevice-dictionary-allocation-104955595590661477
Open

⚡ Optimize ToDictionary() in PointerInputDevice#200
Dor-bl wants to merge 2 commits intomainfrom
perf-pointerinputdevice-dictionary-allocation-104955595590661477

Conversation

@Dor-bl
Copy link
Owner

@Dor-bl Dor-bl commented Mar 22, 2026

💡 What: Replaced the LINQ ToList().ForEach(...) iteration with a standard foreach loop inside PointerInputDevice.cs.

🎯 Why: The previous implementation called ToList() on a Dictionary, which creates an unnecessary List<KeyValuePair<string, object>> allocation. Using a standard foreach loop directly iterates the dictionary's enumerator without allocating extra memory, resulting in faster execution.

📊 Measured Improvement:
A focused benchmark mimicking the exact dictionary copy operation (with 100 extra attributes per iteration, tested over 100,000 iterations) yielded the following results locally:

  • Baseline (LINQ ToList().ForEach): 788 ms
  • Optimized (Standard foreach): 404 ms
  • Improvement: ~48% reduction in execution time for this specific code path, alongside completely eliminating the intermediate list allocations.

PR created automatically by Jules for task 104955595590661477 started by @Dor-bl

Replace LINQ ToList().ForEach() with a standard foreach loop when appending extra attributes.
This avoids an unnecessary O(N) memory allocation and improves execution speed for dictionary translation.
Copilot AI review requested due to automatic review settings March 22, 2026 05:18
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes the payload merge logic in PointerInputDevice by replacing an allocation-heavy LINQ iteration with a direct dictionary enumeration, and adds a small standalone benchmark project to compare the two approaches.

Changes:

  • Replaced ToList().ForEach(...) with a foreach loop when copying pointer extra attributes into the interaction payload.
  • Added a benchmark/ console project to compare the LINQ vs foreach approaches.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/Appium.Net/Appium/Interactions/PointerInputDevice.cs Removes intermediate list allocation by iterating the attributes dictionary directly.
benchmark/Program.cs Adds an ad-hoc stopwatch benchmark comparing LINQ vs foreach.
benchmark/benchmark.csproj Introduces a new benchmark console project and its target framework configuration.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Replace LINQ ToList().ForEach() with a standard foreach loop when appending extra attributes.
This avoids an unnecessary O(N) memory allocation and improves execution speed for dictionary translation.
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.

2 participants