-
Notifications
You must be signed in to change notification settings - Fork 7.9k
SplPriorityQueue performance improvements #6859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nikic
merged 4 commits into
php:master
from
kaja47:perf-priorityqueue-typespecialization
Apr 12, 2021
Merged
SplPriorityQueue performance improvements #6859
nikic
merged 4 commits into
php:master
from
kaja47:perf-priorityqueue-typespecialization
Apr 12, 2021
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This optimization is targeting cases when a SplPriorityQueue instance is used exclusively with double or long priorities. During the first insertion into an empty queue, the comparator is changed to the specialized one if the priority of inserted inserted key is long or double. During insertion to non-empty queue, comparator is swapped back to the generic one on type conflict. As a result code like following, where the weight field is always double or int, runs almost twice as fast. foreach ($items as $item) { $pqueue->insert($item, -$item->weight); if ($pqueue->count() > $size) { $pqueue->extract(); } }
Known size allows compiler to fully inline calls to memcpy.
nikic
reviewed
Apr 12, 2021
staabm
reviewed
Apr 12, 2021
nikic
approved these changes
Apr 12, 2021
ext/spl/spl_heap.c
Outdated
@@ -233,10 +241,27 @@ static int spl_ptr_pqueue_elem_cmp(void *x, void *y, zval *object) { /* {{{ */ | |||
} | |||
} | |||
|
|||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spurious change.
ext/spl/spl_heap.c
Outdated
@@ -254,7 +279,7 @@ static spl_ptr_heap *spl_ptr_heap_init(spl_ptr_heap_cmp_func cmp, spl_ptr_heap_c | |||
} | |||
/* }}} */ | |||
|
|||
static void spl_ptr_heap_insert(spl_ptr_heap *heap, void *elem, void *cmp_userdata) { /* {{{ */ | |||
static void spl_ptr_heap_insert(spl_ptr_heap *heap, void *elem, zval *object) { /* {{{ */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is no longer needed.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Performance improvements to SplPriorityQueue leading to almost 2x speedup.
The queue now tracks priorities of inserted keys and swaps to the specialized comparator if it contains only double or long priorities.