Skip to content

Commit

Permalink
refactor(runloop/wasm): optimize hash_chain_entity with string.buffer (
Browse files Browse the repository at this point in the history
…#11304)

* refactor(runloop/wsam): optimize hash_chain_entity with string.buffer

* buf:free()

* buf:reset()
  • Loading branch information
chronolaw authored Nov 4, 2023
1 parent d4ff0e8 commit bd1ac6a
Showing 1 changed file with 15 additions and 11 deletions.
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

1 comment on commit bd1ac6a

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:bd1ac6abc42ccca0567f5ce34f7ebed71e3cafd6
Artifacts available https://github.com/Kong/kong/actions/runs/6757155209

Please sign in to comment.