optimize rbtree initialization and insert

This commit is contained in:
Igor Sysoev 2007-12-17 08:52:00 +00:00
parent c0cadf1f34
commit 7912e4ba5d
5 changed files with 55 additions and 117 deletions

View File

@ -53,11 +53,8 @@ ngx_open_file_cache_init(ngx_pool_t *pool, ngx_uint_t max, time_t inactive)
return NULL;
}
ngx_rbtree_sentinel_init(sentinel);
cache->rbtree.root = sentinel;
cache->rbtree.sentinel = sentinel;
cache->rbtree.insert = ngx_open_file_cache_rbtree_insert_value;
ngx_rbtree_init(&cache->rbtree, sentinel,
ngx_open_file_cache_rbtree_insert_value);
cache->current = 0;
cache->max = max;

View File

@ -97,28 +97,20 @@ void
ngx_rbtree_insert_value(ngx_rbtree_node_t *temp, ngx_rbtree_node_t *node,
ngx_rbtree_node_t *sentinel)
{
ngx_rbtree_node_t **p;
for ( ;; ) {
if (node->key < temp->key) {
p = (node->key < temp->key) ? &temp->left : &temp->right;
if (temp->left == sentinel) {
temp->left = node;
break;
}
temp = temp->left;
} else {
if (temp->right == sentinel) {
temp->right = node;
break;
}
temp = temp->right;
if (*p == sentinel) {
break;
}
temp = *p;
}
*p = node;
node->parent = temp;
node->left = sentinel;
node->right = sentinel;
@ -130,6 +122,8 @@ void
ngx_rbtree_insert_timer_value(ngx_rbtree_node_t *temp, ngx_rbtree_node_t *node,
ngx_rbtree_node_t *sentinel)
{
ngx_rbtree_node_t **p;
for ( ;; ) {
/*
@ -139,29 +133,20 @@ ngx_rbtree_insert_timer_value(ngx_rbtree_node_t *temp, ngx_rbtree_node_t *node,
* The comparison takes into account that overflow.
*/
if ((ngx_rbtree_key_int_t) node->key - (ngx_rbtree_key_int_t) temp->key
< 0)
{
/* node->key < temp->key */
/* node->key < temp->key */
if (temp->left == sentinel) {
temp->left = node;
break;
}
p = ((ngx_rbtree_key_int_t) node->key - (ngx_rbtree_key_int_t) temp->key
< 0)
? &temp->left : &temp->right;
temp = temp->left;
} else {
if (temp->right == sentinel) {
temp->right = node;
break;
}
temp = temp->right;
if (*p == sentinel) {
break;
}
}
temp = *p;
}
*p = node;
node->parent = temp;
node->left = sentinel;
node->right = sentinel;

View File

@ -1241,11 +1241,8 @@ ngx_ssl_session_cache_init(ngx_shm_zone_t *shm_zone, void *data)
return NGX_ERROR;
}
ngx_rbtree_sentinel_init(sentinel);
cache->session_rbtree->root = sentinel;
cache->session_rbtree->sentinel = sentinel;
cache->session_rbtree->insert = ngx_ssl_session_rbtree_insert_value;
ngx_rbtree_init(cache->session_rbtree, sentinel,
ngx_ssl_session_rbtree_insert_value);
shm_zone->data = cache;
@ -1625,56 +1622,37 @@ static void
ngx_ssl_session_rbtree_insert_value(ngx_rbtree_node_t *temp,
ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
{
ngx_ssl_sess_id_t *sess_id, *sess_id_temp;
ngx_rbtree_node_t **p;
ngx_ssl_sess_id_t *sess_id, *sess_id_temp;
for ( ;; ) {
if (node->key < temp->key) {
if (temp->left == sentinel) {
temp->left = node;
break;
}
temp = temp->left;
p = &temp->left;
} else if (node->key > temp->key) {
if (temp->right == sentinel) {
temp->right = node;
break;
}
temp = temp->right;
p = &temp->right;
} else { /* node->key == temp->key */
sess_id = (ngx_ssl_sess_id_t *) node;
sess_id_temp = (ngx_ssl_sess_id_t *) temp;
if (ngx_memn2cmp(sess_id->id, sess_id_temp->id,
(size_t) node->data, (size_t) temp->data)
< 0)
{
if (temp->left == sentinel) {
temp->left = node;
break;
}
temp = temp->left;
} else {
if (temp->right == sentinel) {
temp->right = node;
break;
}
temp = temp->right;
}
p = (ngx_memn2cmp(sess_id->id, sess_id_temp->id,
(size_t) node->data, (size_t) temp->data)
< 0) ? &temp->left : &temp->right;
}
if (*p == sentinel) {
break;
}
temp = *p;
}
*p = node;
node->parent = temp;
node->left = sentinel;
node->right = sentinel;

View File

@ -26,9 +26,8 @@ static ngx_rbtree_node_t ngx_event_timer_sentinel;
ngx_int_t
ngx_event_timer_init(ngx_log_t *log)
{
ngx_event_timer_rbtree.root = &ngx_event_timer_sentinel;
ngx_event_timer_rbtree.sentinel = &ngx_event_timer_sentinel;
ngx_event_timer_rbtree.insert = ngx_rbtree_insert_timer_value;
ngx_rbtree_init(&ngx_event_timer_rbtree, &ngx_event_timer_sentinel,
ngx_rbtree_insert_timer_value);
#if (NGX_THREADS)

View File

@ -239,54 +239,36 @@ static void
ngx_http_limit_zone_rbtree_insert_value(ngx_rbtree_node_t *temp,
ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
{
ngx_http_limit_zone_node_t *lzn, *lznt;
ngx_rbtree_node_t **p;
ngx_http_limit_zone_node_t *lzn, *lznt;
for ( ;; ) {
if (node->key < temp->key) {
if (temp->left == sentinel) {
temp->left = node;
break;
}
temp = temp->left;
p = &temp->left;
} else if (node->key > temp->key) {
if (temp->right == sentinel) {
temp->right = node;
break;
}
temp = temp->right;
p = &temp->right;
} else { /* node->key == temp->key */
lzn = (ngx_http_limit_zone_node_t *) &node->color;
lznt = (ngx_http_limit_zone_node_t *) &temp->color;
if (ngx_memn2cmp(lzn->data, lznt->data, lzn->len, lznt->len) < 0) {
if (temp->left == sentinel) {
temp->left = node;
break;
}
temp = temp->left;
} else {
if (temp->right == sentinel) {
temp->right = node;
break;
}
temp = temp->right;
}
p = (ngx_memn2cmp(lzn->data, lznt->data, lzn->len, lznt->len) < 0)
? &temp->left : &temp->right;
}
if (*p == sentinel) {
break;
}
temp = *p;
}
*p = node;
node->parent = temp;
node->left = sentinel;
node->right = sentinel;
@ -362,11 +344,8 @@ ngx_http_limit_zone_init_zone(ngx_shm_zone_t *shm_zone, void *data)
return NGX_ERROR;
}
ngx_rbtree_sentinel_init(sentinel);
ctx->rbtree->root = sentinel;
ctx->rbtree->sentinel = sentinel;
ctx->rbtree->insert = ngx_http_limit_zone_rbtree_insert_value;
ngx_rbtree_init(ctx->rbtree, sentinel,
ngx_http_limit_zone_rbtree_insert_value);
return NGX_OK;
}