Skip to content

[Stdlib] Use memcpy in List.pop() for trivially movable types#5947

Open
msaelices wants to merge 3 commits intomodular:mainfrom
msaelices:list-pop-optimization
Open

[Stdlib] Use memcpy in List.pop() for trivially movable types#5947
msaelices wants to merge 3 commits intomodular:mainfrom
msaelices:list-pop-optimization

Conversation

@msaelices
Copy link
Contributor

@msaelices msaelices commented Feb 13, 2026

For types with trivial move semantics, use memcpy to shift trailing elements in List.pop() instead of an element-by-element loop. This follows the same pattern already used in List._realloc() and List.extend().

Benchmarks (median, 10 repetitions)

Benchmark main (ms) optimized (ms) speedup
pop_front[100] 0.0035 0.0031 1.13x
pop_front[1000] 0.1729 0.1259 1.37x
pop_front[10000] 24.08 10.23 2.35x
pop_middle[1000] 0.1096 0.0573 1.92x
pop_middle[10000] 12.36 4.16 2.97x

Non-trivially movable types (e.g. String) are unaffected and continue using the element-by-element loop.

For types with trivial move semantics, use memcpy to shift trailing
elements instead of an element-by-element loop. This follows the same
pattern already used in List._realloc() and List.extend().

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Add benchmarks for List.pop() covering pop from end, front, and middle
positions, as well as non-trivially movable types (String) for comparison.

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Use unified closures, black_box, and remove unnecessary raises.

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
@msaelices msaelices marked this pull request as ready for review February 13, 2026 22:25
@msaelices msaelices requested a review from a team as a code owner February 13, 2026 22:25
Copilot AI review requested due to automatic review settings February 13, 2026 22:25
Copy link
Contributor

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 List.pop() to use memcpy for types with trivial move semantics instead of an element-by-element loop when shifting trailing elements. This optimization pattern is already used in other List methods like _realloc() and extend(). The PR also adds comprehensive benchmarks to measure the performance improvement.

Changes:

  • Modified List.pop() to use memcpy for trivially movable types, achieving up to 2.97x speedup for middle pops on large lists
  • Added benchmark file with tests for pop operations from different positions (end, front, middle) and for non-trivially movable types

Reviewed changes

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

File Description
mojo/stdlib/std/collections/list.mojo Added conditional memcpy optimization in pop() method for trivially movable types
mojo/stdlib/benchmarks/collections/bench_list.mojo New benchmark file measuring pop performance for various positions and type characteristics

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

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.

1 participant