Skip to content

Reimplement Queue to avoid shift/push performance problem#311

Merged
sodabrew merged 1 commit intoeventmachine:masterfrom
grddev:faster_queue
Nov 2, 2015
Merged

Reimplement Queue to avoid shift/push performance problem#311
sodabrew merged 1 commit intoeventmachine:masterfrom
grddev:faster_queue

Conversation

@grddev
Copy link
Copy Markdown
Contributor

@grddev grddev commented Apr 3, 2012

The existing EM::Queue implementation uses push and shift to shuffle the queued items through an array. Unfortunately, a shift followed by a push has linear time complexity in the size of the array, making queue operations very expensive for long queues.

The patch replaces the @Items array with two separate arrays @sink for pushing elements into it and @drain for shifting out elements. This ensures that both push and shift get amortized constant time complexity.

In addition, the patch introduces a test that demonstrates the performance issues. For me, the new test passes with both Queue implementations, but takes minutes with the old implementation and much less than a second with the new implementation.

@tmm1
Copy link
Copy Markdown
Contributor

tmm1 commented Apr 3, 2012

/cc @raggi

@ibc
Copy link
Copy Markdown
Contributor

ibc commented Apr 4, 2012

@raggi
Copy link
Copy Markdown
Member

raggi commented Apr 5, 2012

It's a little more complex, but that seems par for the course. If the tests all still pass, then 👍

@sodabrew sodabrew added this to the v1.2.0 milestone Feb 2, 2015
sodabrew added a commit that referenced this pull request Nov 2, 2015
Reimplement Queue to avoid shift/push performance problem
@sodabrew sodabrew merged commit 5dee602 into eventmachine:master Nov 2, 2015
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.

5 participants