Commit 71623b67 authored by Tatsuhiro Tsujikawa's avatar Tatsuhiro Tsujikawa

Optimize pq

parent 6a511aef
...@@ -52,14 +52,14 @@ static void swap(nghttp2_pq *pq, size_t i, size_t j) { ...@@ -52,14 +52,14 @@ static void swap(nghttp2_pq *pq, size_t i, size_t j) {
} }
static void bubble_up(nghttp2_pq *pq, size_t index) { static void bubble_up(nghttp2_pq *pq, size_t index) {
if (index == 0) { size_t parent;
return; while (index != 0) {
} else { parent = (index - 1) / 2;
size_t parent = (index - 1) / 2; if (!pq->less(pq->q[index], pq->q[parent])) {
if (pq->less(pq->q[index], pq->q[parent])) { return;
swap(pq, parent, index);
bubble_up(pq, parent);
} }
swap(pq, parent, index);
index = parent;
} }
} }
...@@ -94,19 +94,23 @@ nghttp2_pq_entry *nghttp2_pq_top(nghttp2_pq *pq) { ...@@ -94,19 +94,23 @@ nghttp2_pq_entry *nghttp2_pq_top(nghttp2_pq *pq) {
} }
static void bubble_down(nghttp2_pq *pq, size_t index) { static void bubble_down(nghttp2_pq *pq, size_t index) {
size_t i, j = index * 2 + 1; size_t i, j, minindex;
size_t minindex = index; for (;;) {
for (i = 0; i < 2; ++i, ++j) { j = index * 2 + 1;
if (j >= pq->length) { minindex = index;
break; for (i = 0; i < 2; ++i, ++j) {
if (j >= pq->length) {
break;
}
if (pq->less(pq->q[j], pq->q[minindex])) {
minindex = j;
}
} }
if (pq->less(pq->q[j], pq->q[minindex])) { if (minindex == index) {
minindex = j; return;
} }
}
if (minindex != index) {
swap(pq, index, minindex); swap(pq, index, minindex);
bubble_down(pq, minindex); index = minindex;
} }
} }
...@@ -122,6 +126,11 @@ void nghttp2_pq_pop(nghttp2_pq *pq) { ...@@ -122,6 +126,11 @@ void nghttp2_pq_pop(nghttp2_pq *pq) {
void nghttp2_pq_remove(nghttp2_pq *pq, nghttp2_pq_entry *item) { void nghttp2_pq_remove(nghttp2_pq *pq, nghttp2_pq_entry *item) {
assert(pq->q[item->index] == item); assert(pq->q[item->index] == item);
if (item->index == 0) {
nghttp2_pq_pop(pq);
return;
}
if (item->index == pq->length - 1) { if (item->index == pq->length - 1) {
--pq->length; --pq->length;
return; return;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment