Skip to content

perfomrance(queue): eliminate O(N) intermediate slice allocation in Copy() - #67

Merged
zyedidia merged 1 commit into
zyedidia:masterfrom
mohitjoshi-hey:performance/optimise-queue-copy
Oct 15, 2026
Merged

zyedidia merged 1 commit into
zyedidia:masterfrom
mohitjoshi-hey:performance/optimise-queue-copy

Conversation

@mohitjoshi-hey

Copy link
Copy Markdown
Contributor

This PR optimizes the Queue.Copy() method by iterating directly over the underlying list instead of using q.PeekAll().

The Fix:
By using q.Each() to iterate and enqueue directly into the new queue, we bypass the slice allocation entirely, reducing auxiliary space from O(N) to O(1).

Performance Impact:
Benchmarking Copy() on a queue of 10,000 integers shows a 25% reduction in memory overhead and a ~10% speedup:

Before:
Screenshot 2026-10-14 122843

After:
Screenshot 2026-10-14 122927

Testing:

  • go test ./queue/... passes
  • go fmt applied

Closes #66

@mohitjoshi-hey

Copy link
Copy Markdown
Contributor Author

Hey, @zyedidia! Can you please review this PR.

@zyedidia
zyedidia merged commit fc09185 into zyedidia:master Oct 15, 2026
1 check passed
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.

Optimize Queue.Copy() to eliminate O(N) intermediate slice allocation

2 participants