From 2073087909ae527ba46cce52e186b60ba2cf2866 Mon Sep 17 00:00:00 2001 From: YuanSheng Wang Date: Tue, 4 Aug 2020 09:30:13 +0800 Subject: [PATCH] perf: used empty table if the user is not enabled any plugin. (#1967) --- apisix/core.lua | 1 + apisix/init.lua | 6 +++--- apisix/plugin.lua | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/apisix/core.lua b/apisix/core.lua index 051dae36..1b4ebf42 100644 --- a/apisix/core.lua +++ b/apisix/core.lua @@ -38,4 +38,5 @@ return { etcd = require("apisix.core.etcd"), http = require("apisix.core.http"), tablepool= require("tablepool"), + empty_tab= {}, } diff --git a/apisix/init.lua b/apisix/init.lua index 99f75e2d..da2df142 100644 --- a/apisix/init.lua +++ b/apisix/init.lua @@ -451,8 +451,8 @@ function _M.http_access_phase() api_ctx.var.upstream_connection = api_ctx.var.http_connection end - local plugins = core.tablepool.fetch("plugins", 32, 0) - api_ctx.plugins = plugin.filter(route, plugins) + local plugins = plugin.filter(route) + api_ctx.plugins = plugins run_plugin("rewrite", plugins, api_ctx) if api_ctx.consumer then @@ -631,7 +631,7 @@ function _M.http_log_phase() end core.ctx.release_vars(api_ctx) - if api_ctx.plugins then + if api_ctx.plugins and api_ctx.plugins ~= core.empty_tab then core.tablepool.release("plugins", api_ctx.plugins) end diff --git a/apisix/plugin.lua b/apisix/plugin.lua index 2cf5296a..691860d1 100644 --- a/apisix/plugin.lua +++ b/apisix/plugin.lua @@ -229,16 +229,16 @@ end function _M.filter(user_route, plugins) - plugins = plugins or core.table.new(#local_plugins * 2, 0) local user_plugin_conf = user_route.value.plugins if user_plugin_conf == nil or core.table.nkeys(user_plugin_conf) == 0 then if local_conf and local_conf.apisix.enable_debug then core.response.set_header("Apisix-Plugins", "no plugin") end - return plugins + return core.empty_tab end + plugins = plugins or core.tablepool.fetch("plugins", 32, 0) for _, plugin_obj in ipairs(local_plugins) do local name = plugin_obj.name local plugin_conf = user_plugin_conf[name]