Skip to content

Commit 2d667ec

Browse files
author
doujiang
authored
optimization: use a new upvalue table for the ctxs holder to make LuaJIT JIT compiler happy to generate more efficient machine code. (openresty#360)
Now `ngx.ctx` is 80% faster than the old implementation.
1 parent efce370 commit 2d667ec

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

lib/resty/core/ctx.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ local _M = {
5353
}
5454

5555

56+
-- use a new ctxs table to make LuaJIT JIT compiler happy to generate more
57+
-- efficient machine code.
58+
local ctxs = {}
59+
registry.ngx_lua_ctx_tables = ctxs
60+
61+
5662
local get_ctx_table
5763
do
5864
local in_ssl_phase = ffi.new("int[1]")
@@ -70,7 +76,6 @@ do
7076
error("no request ctx found")
7177
end
7278

73-
local ctxs = registry.ngx_lua_ctx_tables
7479
if ctx_ref < 0 then
7580
ctx_ref = ssl_ctx_ref[0]
7681
if ctx_ref > 0 and ctxs[ctx_ref] then
@@ -129,7 +134,6 @@ local function set_ctx_table(ctx)
129134
error("no request ctx found")
130135
end
131136

132-
local ctxs = registry.ngx_lua_ctx_tables
133137
if ctx_ref < 0 then
134138
ctx_ref = ref_in_table(ctxs, ctx)
135139
ngx_lua_ffi_set_ctx_ref(r, ctx_ref)

0 commit comments

Comments
 (0)