Avoid iterate siblings when adding/removing stream tree

This commit is contained in:
Tatsuhiro Tsujikawa 2014-10-16 01:12:59 +09:00
parent 47692d113c
commit 502ff24568
2 changed files with 106 additions and 83 deletions

View File

@ -601,10 +601,37 @@ void nghttp2_stream_dep_insert(nghttp2_stream *dep_stream,
++stream->roots->num_streams; ++stream->roots->num_streams;
} }
static void link_dep(nghttp2_stream *dep_stream, nghttp2_stream *stream)
{
dep_stream->dep_next = stream;
stream->dep_prev = dep_stream;
}
static void link_sib(nghttp2_stream *prev_stream, nghttp2_stream *stream)
{
prev_stream->sib_next = stream;
stream->sib_prev = prev_stream;
}
static void insert_link_dep(nghttp2_stream *dep_stream,
nghttp2_stream *stream)
{
nghttp2_stream *sib_next;
assert(stream->sib_prev == NULL);
sib_next = dep_stream->dep_next;
link_sib(stream, sib_next);
sib_next->dep_prev = NULL;
link_dep(dep_stream, stream);
}
void nghttp2_stream_dep_add(nghttp2_stream *dep_stream, void nghttp2_stream_dep_add(nghttp2_stream *dep_stream,
nghttp2_stream *stream) nghttp2_stream *stream)
{ {
nghttp2_stream *last_sib;
nghttp2_stream *root_stream; nghttp2_stream *root_stream;
assert(stream->data_item == NULL); assert(stream->data_item == NULL);
@ -622,9 +649,7 @@ void nghttp2_stream_dep_add(nghttp2_stream *dep_stream,
dep_stream->dep_next = stream; dep_stream->dep_next = stream;
stream->dep_prev = dep_stream; stream->dep_prev = dep_stream;
} else { } else {
last_sib = stream_last_sib(dep_stream->dep_next); insert_link_dep(dep_stream, stream);
last_sib->sib_next = stream;
stream->sib_prev = last_sib;
} }
stream_update_dep_sum_norest_weight(root_stream); stream_update_dep_sum_norest_weight(root_stream);
@ -813,7 +838,6 @@ int nghttp2_stream_dep_add_subtree(nghttp2_stream *dep_stream,
nghttp2_pq *pq, nghttp2_pq *pq,
uint64_t cycle) uint64_t cycle)
{ {
nghttp2_stream *last_sib;
nghttp2_stream *root_stream; nghttp2_stream *root_stream;
DEBUGF(fprintf(stderr, "stream: dep_add_subtree dep_stream(%p)=%d " DEBUGF(fprintf(stderr, "stream: dep_add_subtree dep_stream(%p)=%d "
@ -826,10 +850,7 @@ int nghttp2_stream_dep_add_subtree(nghttp2_stream *dep_stream,
if(dep_stream->dep_next) { if(dep_stream->dep_next) {
dep_stream->sum_dep_weight += stream->weight; dep_stream->sum_dep_weight += stream->weight;
last_sib = stream_last_sib(dep_stream->dep_next); insert_link_dep(dep_stream, stream);
last_sib->sib_next = stream;
stream->sib_prev = last_sib;
} else { } else {
dep_stream->dep_next = stream; dep_stream->dep_next = stream;
stream->dep_prev = dep_stream; stream->dep_prev = dep_stream;
@ -961,12 +982,14 @@ int nghttp2_stream_dep_all_your_stream_are_belong_to_us
} }
if(stream->dep_next) { if(stream->dep_next) {
nghttp2_stream *last_sib; nghttp2_stream *sib_next;
last_sib = stream_last_sib(stream->dep_next); sib_next = stream->dep_next;
last_sib->sib_next = first; sib_next->dep_prev = NULL;
first->sib_prev = last_sib;
link_sib(first, sib_next);
link_dep(stream, prev);
} else { } else {
stream->dep_next = first; stream->dep_next = first;
first->dep_prev = stream; first->dep_prev = stream;

View File

@ -5339,8 +5339,8 @@ void test_nghttp2_session_stream_dep_add(void)
a = open_stream(session, 1); a = open_stream(session, 1);
b = open_stream_with_dep(session, 3, a);
c = open_stream_with_dep(session, 5, a); c = open_stream_with_dep(session, 5, a);
b = open_stream_with_dep(session, 3, a);
d = open_stream_with_dep(session, 7, c); d = open_stream_with_dep(session, 7, c);
/* a /* a
@ -5423,7 +5423,7 @@ void test_nghttp2_session_stream_dep_remove(void)
/* a /* a
* | * |
* b--c * c--b
* | * |
* d * d
*/ */
@ -5452,9 +5452,9 @@ void test_nghttp2_session_stream_dep_remove(void)
check_stream_dep_sib(d, c, NULL, NULL, NULL); check_stream_dep_sib(d, c, NULL, NULL, NULL);
CU_ASSERT(3 == session->roots.num_streams); CU_ASSERT(3 == session->roots.num_streams);
CU_ASSERT(c == session->roots.head); CU_ASSERT(b == session->roots.head);
CU_ASSERT(b == c->root_next); CU_ASSERT(c == b->root_next);
CU_ASSERT(NULL == b->root_next); CU_ASSERT(NULL == c->root_next);
nghttp2_session_del(session); nghttp2_session_del(session);
@ -5468,7 +5468,7 @@ void test_nghttp2_session_stream_dep_remove(void)
/* a /* a
* | * |
* b--c * c--b
* | * |
* d * d
*/ */
@ -5514,7 +5514,7 @@ void test_nghttp2_session_stream_dep_remove(void)
/* a /* a
* | * |
* b--c * c--b
* | * |
* d * d
*/ */
@ -5524,7 +5524,7 @@ void test_nghttp2_session_stream_dep_remove(void)
/* becomes: /* becomes:
* a * a
* | * |
* b--d * d--b
*/ */
CU_ASSERT(3 == a->num_substreams); CU_ASSERT(3 == a->num_substreams);
@ -5537,10 +5537,10 @@ void test_nghttp2_session_stream_dep_remove(void)
CU_ASSERT(0 == d->sum_dep_weight); CU_ASSERT(0 == d->sum_dep_weight);
CU_ASSERT(0 == c->sum_dep_weight); CU_ASSERT(0 == c->sum_dep_weight);
check_stream_dep_sib(a, NULL, b, NULL, NULL); check_stream_dep_sib(a, NULL, d, NULL, NULL);
check_stream_dep_sib(b, a, NULL, NULL, d); check_stream_dep_sib(b, NULL, NULL, d, NULL);
check_stream_dep_sib(c, NULL, NULL, NULL, NULL); check_stream_dep_sib(c, NULL, NULL, NULL, NULL);
check_stream_dep_sib(d, NULL, NULL, b, NULL); check_stream_dep_sib(d, a, NULL, NULL, b);
nghttp2_session_del(session); nghttp2_session_del(session);
@ -5556,9 +5556,9 @@ void test_nghttp2_session_stream_dep_remove(void)
/* a /* a
* | * |
* b--c--d * d--c--b
* | * |
* e--f * f--e
*/ */
CU_ASSERT(6 == a->num_substreams); CU_ASSERT(6 == a->num_substreams);
@ -5580,7 +5580,7 @@ void test_nghttp2_session_stream_dep_remove(void)
/* becomes: /* becomes:
* a * a
* | * |
* b--e--f--d * d--f--e--b
*/ */
CU_ASSERT(5 == a->num_substreams); CU_ASSERT(5 == a->num_substreams);
@ -5599,12 +5599,12 @@ void test_nghttp2_session_stream_dep_remove(void)
CU_ASSERT(0 == e->sum_dep_weight); CU_ASSERT(0 == e->sum_dep_weight);
CU_ASSERT(0 == f->sum_dep_weight); CU_ASSERT(0 == f->sum_dep_weight);
check_stream_dep_sib(a, NULL, b, NULL, NULL); check_stream_dep_sib(a, NULL, d, NULL, NULL);
check_stream_dep_sib(b, a, NULL, NULL, e); check_stream_dep_sib(b, NULL, NULL, e, NULL);
check_stream_dep_sib(c, NULL, NULL, NULL, NULL); check_stream_dep_sib(c, NULL, NULL, NULL, NULL);
check_stream_dep_sib(e, NULL, NULL, b, f); check_stream_dep_sib(e, NULL, NULL, f, b);
check_stream_dep_sib(f, NULL, NULL, e, d); check_stream_dep_sib(f, NULL, NULL, d, e);
check_stream_dep_sib(d, NULL, NULL, f, NULL); check_stream_dep_sib(d, a, NULL, NULL, f);
nghttp2_session_del(session); nghttp2_session_del(session);
} }
@ -5630,7 +5630,7 @@ void test_nghttp2_session_stream_dep_add_subtree(void)
/* a e /* a e
* | | * | |
* b--c f * c--b f
* | * |
* d * d
*/ */
@ -5641,9 +5641,9 @@ void test_nghttp2_session_stream_dep_add_subtree(void)
/* becomes /* becomes
* a * a
* | * |
* b--c--e * e--c--b
* | | * | |
* d f * f d
*/ */
CU_ASSERT(6 == a->num_substreams); CU_ASSERT(6 == a->num_substreams);
@ -5660,11 +5660,11 @@ void test_nghttp2_session_stream_dep_add_subtree(void)
CU_ASSERT(NGHTTP2_DEFAULT_WEIGHT == e->sum_dep_weight); CU_ASSERT(NGHTTP2_DEFAULT_WEIGHT == e->sum_dep_weight);
CU_ASSERT(0 == f->sum_dep_weight); CU_ASSERT(0 == f->sum_dep_weight);
check_stream_dep_sib(a, NULL, b, NULL, NULL); check_stream_dep_sib(a, NULL, e, NULL, NULL);
check_stream_dep_sib(b, a, NULL, NULL, c); check_stream_dep_sib(b, NULL, NULL, c, NULL);
check_stream_dep_sib(c, NULL, d, b, e); check_stream_dep_sib(c, NULL, d, e, b);
check_stream_dep_sib(d, c, NULL, NULL, NULL); check_stream_dep_sib(d, c, NULL, NULL, NULL);
check_stream_dep_sib(e, NULL, f, c, NULL); check_stream_dep_sib(e, a, f, NULL, c);
check_stream_dep_sib(f, e, NULL, NULL, NULL); check_stream_dep_sib(f, e, NULL, NULL, NULL);
nghttp2_session_del(session); nghttp2_session_del(session);
@ -5682,7 +5682,7 @@ void test_nghttp2_session_stream_dep_add_subtree(void)
/* a e /* a e
* | | * | |
* b--c f * c--b f
* | * |
* d * d
*/ */
@ -5695,7 +5695,7 @@ void test_nghttp2_session_stream_dep_add_subtree(void)
* | * |
* e * e
* | * |
* f--b--c * f--c--b
* | * |
* d * d
*/ */
@ -5716,9 +5716,9 @@ void test_nghttp2_session_stream_dep_add_subtree(void)
check_stream_dep_sib(a, NULL, e, NULL, NULL); check_stream_dep_sib(a, NULL, e, NULL, NULL);
check_stream_dep_sib(e, a, f, NULL, NULL); check_stream_dep_sib(e, a, f, NULL, NULL);
check_stream_dep_sib(f, e, NULL, NULL, b); check_stream_dep_sib(f, e, NULL, NULL, c);
check_stream_dep_sib(b, NULL, NULL, f, c); check_stream_dep_sib(b, NULL, NULL, c, NULL);
check_stream_dep_sib(c, NULL, d, b, NULL); check_stream_dep_sib(c, NULL, d, f, b);
check_stream_dep_sib(d, c, NULL, NULL, NULL); check_stream_dep_sib(d, c, NULL, NULL, NULL);
nghttp2_session_del(session); nghttp2_session_del(session);
@ -5742,7 +5742,7 @@ void test_nghttp2_session_stream_dep_remove_subtree(void)
/* a /* a
* | * |
* b--c * c--b
* | * |
* d * d
*/ */
@ -5782,7 +5782,7 @@ void test_nghttp2_session_stream_dep_remove_subtree(void)
/* a /* a
* | * |
* b--c * c--b
* | * |
* d * d
*/ */
@ -5818,10 +5818,10 @@ void test_nghttp2_session_stream_dep_remove_subtree(void)
nghttp2_session_server_new(&session, &callbacks, NULL); nghttp2_session_server_new(&session, &callbacks, NULL);
a = open_stream(session, 1); a = open_stream(session, 1);
b = open_stream_with_dep(session, 3, a);
c = open_stream_with_dep(session, 5, a);
d = open_stream_with_dep(session, 7, c);
e = open_stream_with_dep(session, 9, a); e = open_stream_with_dep(session, 9, a);
c = open_stream_with_dep(session, 5, a);
b = open_stream_with_dep(session, 3, a);
d = open_stream_with_dep(session, 7, c);
/* a /* a
* | * |
@ -5961,7 +5961,7 @@ void test_nghttp2_session_stream_dep_all_your_stream_are_belong_to_us(void)
/* /*
* c * c
* | * |
* d--a * a--d
* | * |
* b * b
*/ */
@ -5976,9 +5976,9 @@ void test_nghttp2_session_stream_dep_all_your_stream_are_belong_to_us(void)
CU_ASSERT(NGHTTP2_DEFAULT_WEIGHT == a->sum_dep_weight); CU_ASSERT(NGHTTP2_DEFAULT_WEIGHT == a->sum_dep_weight);
CU_ASSERT(0 == b->sum_dep_weight); CU_ASSERT(0 == b->sum_dep_weight);
check_stream_dep_sib(c, NULL, d, NULL, NULL); check_stream_dep_sib(c, NULL, a, NULL, NULL);
check_stream_dep_sib(d, c, NULL, NULL, a); check_stream_dep_sib(d, NULL, NULL, a, NULL);
check_stream_dep_sib(a, NULL, b, d, NULL); check_stream_dep_sib(a, c, b, NULL, d);
check_stream_dep_sib(b, a, NULL, NULL, NULL); check_stream_dep_sib(b, a, NULL, NULL, NULL);
nghttp2_session_del(session); nghttp2_session_del(session);
@ -6002,7 +6002,7 @@ void test_nghttp2_session_stream_attach_data(void)
/* a /* a
* | * |
* b--c * c--b
* | * |
* d * d
*/ */
@ -6121,7 +6121,7 @@ void test_nghttp2_session_stream_attach_data_subtree(void)
/* /*
* a e * a e
* | | * | |
* b--c f * c--b f
* | * |
* d * d
*/ */
@ -6155,7 +6155,7 @@ void test_nghttp2_session_stream_attach_data_subtree(void)
* | * |
* e * e
* | * |
* f--b--c * f--c--b
* | * |
* d * d
*/ */
@ -6242,9 +6242,9 @@ void test_nghttp2_session_stream_attach_data_subtree(void)
/* /*
* a b * a b
* | * |
* e--c * c--e
* | | * | |
* f d * d f
*/ */
CU_ASSERT(NGHTTP2_STREAM_DPRI_NO_DATA == a->dpri); CU_ASSERT(NGHTTP2_STREAM_DPRI_NO_DATA == a->dpri);