Skip to content

Commit

Permalink
perf(etcd): refactor some code to improve performance
Browse files Browse the repository at this point in the history
Signed-off-by: xuruidong <[email protected]>
  • Loading branch information
xuruidong committed Feb 28, 2025
1 parent 9375902 commit fccc82b
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions apisix/core/config_etcd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ local NGX_INFO = ngx.INFO
local check_schema = require("apisix.core.schema").check
local exiting = ngx.worker.exiting
local insert_tab = table.insert
local remove_tab = table.remove
local type = type
local ipairs = ipairs
local setmetatable = setmetatable
Expand Down Expand Up @@ -741,22 +742,23 @@ local function sync_data(self)

-- avoid space waste
if self.sync_times > 100 then
local values_original = table.clone(self.values)
table.clear(self.values)

for i = 1, #values_original do
local val = values_original[i]
local pre = 1
local cur = 1
table.clear(self.values_hash)
log.info("clear stale data in `values_hash` for key: ", key)
for _, val in ipairs(self.values) do
if val then
table.insert(self.values, val)
self.values[pre] = val
key = short_key(self, val.key)
self.values_hash[key] = pre
pre = pre + 1
end
end

table.clear(self.values_hash)
log.info("clear stale data in `values_hash` for key: ", key)
cur = cur + 1
end

for i = 1, #self.values do
key = short_key(self, self.values[i].key)
self.values_hash[key] = i
for i = cur - 1, pre, -1 do
remove_tab(self.values, i)
end

self.sync_times = 0
Expand Down

0 comments on commit fccc82b

Please sign in to comment.