Skip to content

perf: use deque for FormData.add_fields() queue#12166

Open
giulio-leone wants to merge 1 commit intoaio-libs:masterfrom
giulio-leone:fix/formdata-deque
Open

perf: use deque for FormData.add_fields() queue#12166
giulio-leone wants to merge 1 commit intoaio-libs:masterfrom
giulio-leone:fix/formdata-deque

Conversation

@giulio-leone
Copy link

Summary

Replace list.pop(0) with collections.deque.popleft() in FormData.add_fields().

Problem

add_fields() uses a work-list that drains from the front via .pop(0) and may grow via .extend() when MultiDict entries are encountered (BFS pattern). Each list.pop(0) is O(n) because CPython must shift all remaining elements.

Solution

Switch from list to collections.deque and use .popleft() for O(1) front removal.

Compatibility

  • .extend(), truthiness, and iteration are identical on deque
  • No API surface changes; purely internal optimisation
  • Zero behaviour change

Complexity

Operation Before After
pop front O(n) O(1)

add_fields() drains a work-list via .pop(0) and may grow it with
.extend() when MultiDict entries are encountered (BFS pattern).
Each .pop(0) is O(n); switching to collections.deque with .popleft()
gives O(1) front removal.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@codspeed-hq
Copy link

codspeed-hq bot commented Feb 28, 2026

Merging this PR will not alter performance

✅ 59 untouched benchmarks


Comparing giulio-leone:fix/formdata-deque (c25e610) with master (c1981ef)

Open in CodSpeed

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