Skip to content

Commit b281121

Browse files
committed
optimize: now we make use of the new lua_getexdata2 and lua_setexdata2 to attach coctx to the Lua thread objects directly which save the lookup overhead which can become the bottleneck when there are many Lua light threads around in a single request handler.
1 parent 021b75b commit b281121

10 files changed

+37
-1
lines changed

src/ngx_http_lua_accessby.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ ngx_http_lua_access_by_chunk(lua_State *L, ngx_http_request_t *r)
287287
ctx->cur_co_ctx->co_top = 1;
288288
#endif
289289

290+
ngx_http_lua_attach_co_ctx_to_L(co, ctx->cur_co_ctx);
291+
290292
/* }}} */
291293

292294
/* {{{ register request cleanup hooks */

src/ngx_http_lua_contentby.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ ngx_http_lua_content_by_chunk(lua_State *L, ngx_http_request_t *r)
7979
ctx->cur_co_ctx->co_top = 1;
8080
#endif
8181

82+
ngx_http_lua_attach_co_ctx_to_L(co, ctx->cur_co_ctx);
83+
8284
/* {{{ register request cleanup hooks */
8385
if (ctx->cleanup == NULL) {
8486
cln = ngx_http_cleanup_add(r, 0);

src/ngx_http_lua_coroutine.c

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

149149
#ifdef OPENRESTY_LUAJIT
150150
ngx_http_lua_set_req(co, r);
151+
ngx_http_lua_attach_co_ctx_to_L(co, coctx);
151152
#else
152153
/* make new coroutine share globals of the parent coroutine.
153154
* NOTE: globals don't have to be separated! */

src/ngx_http_lua_module.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,13 +800,23 @@ ngx_http_lua_init(ngx_conf_t *cf)
800800
"the OpenResty releases from https://openresty.org/"
801801
"en/download.html)");
802802
}
803-
#elif !defined(HAVE_LUA_RESETTHREAD)
803+
#else
804+
# if !defined(HAVE_LUA_RESETTHREAD)
804805
ngx_log_error(NGX_LOG_ALERT, cf->log, 0,
805806
"detected an old version of OpenResty's LuaJIT missing "
806807
"the lua_resetthread API and thus the "
807808
"performance will be compromised; please upgrade to the "
808809
"latest version of OpenResty's LuaJIT: "
809810
"https://github.com/openresty/luajit2");
811+
# endif
812+
# if !defined(HAVE_LUA_EXDATA2)
813+
ngx_log_error(NGX_LOG_ALERT, cf->log, 0,
814+
"detected an old version of OpenResty's LuaJIT missing "
815+
"the exdata2 API and thus the "
816+
"performance will be compromised; please upgrade to the "
817+
"latest version of OpenResty's LuaJIT: "
818+
"https://github.com/openresty/luajit2");
819+
# endif
810820
#endif
811821

812822
ngx_http_lua_content_length_hash =

src/ngx_http_lua_rewriteby.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ ngx_http_lua_rewrite_by_chunk(lua_State *L, ngx_http_request_t *r)
287287
ctx->cur_co_ctx->co_top = 1;
288288
#endif
289289

290+
ngx_http_lua_attach_co_ctx_to_L(co, ctx->cur_co_ctx);
291+
290292
/* }}} */
291293

292294
/* {{{ register request cleanup hooks */

src/ngx_http_lua_ssl_certby.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,8 @@ ngx_http_lua_ssl_cert_by_chunk(lua_State *L, ngx_http_request_t *r)
491491
ctx->cur_co_ctx->co_top = 1;
492492
#endif
493493

494+
ngx_http_lua_attach_co_ctx_to_L(co, ctx->cur_co_ctx);
495+
494496
/* register request cleanup hooks */
495497
if (ctx->cleanup == NULL) {
496498
cln = ngx_http_cleanup_add(r, 0);

src/ngx_http_lua_ssl_session_fetchby.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,8 @@ ngx_http_lua_ssl_sess_fetch_by_chunk(lua_State *L, ngx_http_request_t *r)
513513
ctx->cur_co_ctx->co_top = 1;
514514
#endif
515515

516+
ngx_http_lua_attach_co_ctx_to_L(co, ctx->cur_co_ctx);
517+
516518
/* register request cleanup hooks */
517519
if (ctx->cleanup == NULL) {
518520
cln = ngx_http_cleanup_add(r, 0);

src/ngx_http_lua_timer.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,8 @@ ngx_http_lua_timer_handler(ngx_event_t *ev)
621621
/* save the request in coroutine globals table */
622622
ngx_http_lua_set_req(tctx.co, r);
623623

624+
ngx_http_lua_attach_co_ctx_to_L(tctx.co, ctx->cur_co_ctx);
625+
624626
lmcf->running_timers++;
625627

626628
lua_pushboolean(tctx.co, tctx.premature);

src/ngx_http_lua_util.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3168,6 +3168,9 @@ ngx_http_lua_param_set(lua_State *L)
31683168
ngx_http_lua_co_ctx_t *
31693169
ngx_http_lua_get_co_ctx(lua_State *L, ngx_http_lua_ctx_t *ctx)
31703170
{
3171+
#ifdef HAVE_LUA_EXDATA2
3172+
return (ngx_http_lua_co_ctx_t *) lua_getexdata2(L);
3173+
#else
31713174
ngx_uint_t i;
31723175
ngx_list_part_t *part;
31733176
ngx_http_lua_co_ctx_t *coctx;
@@ -3203,6 +3206,7 @@ ngx_http_lua_get_co_ctx(lua_State *L, ngx_http_lua_ctx_t *ctx)
32033206
}
32043207

32053208
return NULL;
3209+
#endif
32063210
}
32073211

32083212

src/ngx_http_lua_util.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,15 @@ ngx_http_lua_set_req(lua_State *L, ngx_http_request_t *r)
398398
}
399399

400400

401+
static ngx_inline void
402+
ngx_http_lua_attach_co_ctx_to_L(lua_State *L, ngx_http_lua_co_ctx_t *coctx)
403+
{
404+
#ifdef HAVE_LUA_EXDATA2
405+
lua_setexdata2(L, (void *) coctx);
406+
#endif
407+
}
408+
409+
401410
#ifndef OPENRESTY_LUAJIT
402411
static ngx_inline void
403412
ngx_http_lua_get_globals_table(lua_State *L)

0 commit comments

Comments
 (0)