Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(runloop/wasm): optimize hash_chain_entity with string.buffer #11304

Merged
merged 3 commits into from
Nov 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 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,18 @@ 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()

buf:reset()

return sha256(s)
end
Expand Down
Loading