Skip to content

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
merged 4 commits into from
Apr 12, 2021

Conversation

kaja47
Copy link
Contributor

@kaja47 kaja47 commented Apr 12, 2021

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.

kaja47 added 2 commits April 12, 2021 15:13
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.
@@ -233,10 +241,27 @@ static int spl_ptr_pqueue_elem_cmp(void *x, void *y, zval *object) { /* {{{ */
}
}


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spurious change.

@@ -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) { /* {{{ */
Copy link
Member

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.

@nikic nikic merged commit 3a05cda into php:master Apr 12, 2021
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.

3 participants