Skip to content

Commit

Permalink
refactor(runloop/wsam): optimize hash_chain_entity with string.buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw committed Nov 3, 2023
1 parent d4ff0e8 commit b6d89fb
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions kong/runloop/wasm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ local tostring = tostring
local ipairs = ipairs
local type = type
local assert = assert
local concat = table.concat
local insert = table.insert
local sha256 = utils.sha256_bin
local cjson_encode = cjson.encode
local cjson_decode = cjson.decode
local fmt = string.format
Expand Down Expand Up @@ -106,10 +104,14 @@ local STATUS = STATUS_DISABLED

local hash_chain
do
local buffer = require "string.buffer"

local sha256 = utils.sha256_bin

local HASH_DISABLED = sha256("disabled")
local HASH_NONE = sha256("none")

local buf = {}
local buf = buffer.new()

---@param chain kong.db.schema.entities.filter_chain
---@return string
Expand All @@ -121,16 +123,16 @@ do
return HASH_DISABLED
end

local n = 0
for _, filter in ipairs(chain.filters) do
buf[n + 1] = filter.name
buf[n + 2] = tostring(filter.enabled)
buf[n + 3] = tostring(filter.enabled and sha256(filter.config))
n = n + 3
local filters = chain.filters
for i = 1, #filters do
local filter = filters[i]

buf:put(filter.name)
buf:put(tostring(filter.enabled))
buf:put(tostring(filter.enabled and sha256(filter.config)))
end

local s = concat(buf, "", 1, n)
clear_tab(buf)
local s = buf:get()

return sha256(s)
end
Expand Down

0 comments on commit b6d89fb

Please sign in to comment.