Skip to content

Commit 83a61d6

Browse files
authored
optimize: post zombie thread: avoided the linear search. (openresty#1823)
This function can be very expensive when there are a lot of zombie threads in the current context.
1 parent 15197e7 commit 83a61d6

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

src/ngx_http_lua_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ struct ngx_http_lua_co_ctx_s {
446446
ngx_http_lua_co_ctx_t *parent_co_ctx;
447447

448448
ngx_http_lua_posted_thread_t *zombie_child_threads;
449+
ngx_http_lua_posted_thread_t **next_zombie_child_thread;
449450

450451
ngx_http_cleanup_pt cleanup;
451452

src/ngx_http_lua_coroutine.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ ngx_http_lua_coroutine_create_helper(lua_State *L, ngx_http_request_t *r,
140140

141141
} else {
142142
ngx_memzero(coctx, sizeof(ngx_http_lua_co_ctx_t));
143+
coctx->next_zombie_child_thread = &coctx->zombie_child_threads;
143144
coctx->co_ref = LUA_NOREF;
144145
}
145146

src/ngx_http_lua_util.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,9 @@ ngx_http_lua_reset_ctx(ngx_http_request_t *r, lua_State *L,
986986

987987
ngx_memzero(&ctx->entry_co_ctx, sizeof(ngx_http_lua_co_ctx_t));
988988

989+
ctx->entry_co_ctx.next_zombie_child_thread =
990+
&ctx->entry_co_ctx.zombie_child_threads;
991+
989992
ctx->entry_co_ctx.co_ref = LUA_NOREF;
990993

991994
ctx->entered_rewrite_phase = 0;
@@ -3268,6 +3271,7 @@ ngx_http_lua_create_co_ctx(ngx_http_request_t *r, ngx_http_lua_ctx_t *ctx)
32683271

32693272
ngx_memzero(coctx, sizeof(ngx_http_lua_co_ctx_t));
32703273

3274+
coctx->next_zombie_child_thread = &coctx->zombie_child_threads;
32713275
coctx->co_ref = LUA_NOREF;
32723276

32733277
return coctx;
@@ -3425,7 +3429,6 @@ static ngx_int_t
34253429
ngx_http_lua_post_zombie_thread(ngx_http_request_t *r,
34263430
ngx_http_lua_co_ctx_t *parent, ngx_http_lua_co_ctx_t *thread)
34273431
{
3428-
ngx_http_lua_posted_thread_t **p;
34293432
ngx_http_lua_posted_thread_t *pt;
34303433

34313434
pt = ngx_palloc(r->pool, sizeof(ngx_http_lua_posted_thread_t));
@@ -3436,9 +3439,10 @@ ngx_http_lua_post_zombie_thread(ngx_http_request_t *r,
34363439
pt->co_ctx = thread;
34373440
pt->next = NULL;
34383441

3439-
for (p = &parent->zombie_child_threads; *p; p = &(*p)->next) { /* void */ }
3442+
ngx_http_lua_assert(parent->next_zombie_child_thread != NULL);
34403443

3441-
*p = pt;
3444+
*parent->next_zombie_child_thread = pt;
3445+
parent->next_zombie_child_thread = &pt->next;
34423446

34433447
return NGX_OK;
34443448
}
@@ -3458,6 +3462,7 @@ ngx_http_lua_cleanup_zombie_child_uthreads(ngx_http_request_t *r,
34583462
}
34593463

34603464
coctx->zombie_child_threads = NULL;
3465+
coctx->next_zombie_child_thread = &coctx->zombie_child_threads;
34613466
}
34623467

34633468

src/ngx_http_lua_util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ ngx_http_lua_init_ctx(ngx_http_request_t *r, ngx_http_lua_ctx_t *ctx)
266266
ngx_memzero(ctx, sizeof(ngx_http_lua_ctx_t));
267267
ctx->ctx_ref = LUA_NOREF;
268268
ctx->entry_co_ctx.co_ref = LUA_NOREF;
269+
ctx->entry_co_ctx.next_zombie_child_thread =
270+
&ctx->entry_co_ctx.zombie_child_threads;
269271
ctx->resume_handler = ngx_http_lua_wev_handler;
270272
ctx->request = r;
271273
}

0 commit comments

Comments
 (0)