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(tools/rand): speed up random string generation #13150

Closed
wants to merge 3 commits into from
Closed
Changes from 2 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
19 changes: 15 additions & 4 deletions kong/tools/rand.lua
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,21 @@ do
-- get 24 bytes, which will return a 32 char string after encoding
-- this is done in attempt to maintain backwards compatibility as
-- much as possible while improving the strength of this function
return encode_base64(get_rand_bytes(24, true))
:gsub("/", char(rand(48, 57))) -- 0 - 10
:gsub("+", char(rand(65, 90))) -- A - Z
:gsub("=", char(rand(97, 122))) -- a - z
local str = encode_base64(get_rand_bytes(24, true))

if str:find("/", 1, true) then
str = str:gsub("/", char(rand(48, 57))) -- 0 - 10
end

if str:find("+", 1, true) then
str = str:gsub("+", char(rand(65, 90))) -- A - Z
end

if str:find("=", 1, true) then
str = str:gsub("=", char(rand(97, 122))) -- a - z
end

return str
Copy link
Member

@bungle bungle Jun 4, 2024

Choose a reason for hiding this comment

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

Probably something like:

return encode_base58(get_rand_bytes(24, true))

would be the fastest.

Or doing:

local byte = string.byte
local OUTPUT = require("string.buffer").new(32)
local SLASH_BYTE = byte("/")
local PLUS_BYTE = byte("+")
local EQ_BYTE = byte("=")
random_string = function()
  OUTPUT:reset()
  local str = encode_base64(get_rand_bytes(24, true))
  for i = 1, 32 do
    local b = byte(str, i) 
    if b == SLASH_BYTE then
      OUTPUT:putf("%c", rand(48, 57))
    elseif b == PLUS_BYTE then
      OUTPUT:putf("%c", rand(65, 90))
    elseif b == EQ_BYTE then
      OUTPUT:putf("%c", rand(97, 122))
    else
      OUTPUT:putf("%c", b)
    end
  end
  str = OUTPUT:get()
  return str
end

Copy link
Member

@bungle bungle Jun 4, 2024

Choose a reason for hiding this comment

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

Actually even faster:

local byte = string.byte
local OUTPUT = require("string.buffer").new(32)
local SLASH_BYTE = byte("/")
local PLUS_BYTE = byte("+")
random_string = function()
  OUTPUT:reset()
  local str = encode_base64(get_rand_bytes(24, true), true)
  for i = 1, 32 do
    local b = byte(str, i) 
    if b == SLASH_BYTE then
      OUTPUT:putf("%c", rand(48, 57))
    elseif b == PLUS_BYTE then
      OUTPUT:putf("%c", rand(65, 90))
    else
      OUTPUT:putf("%c", b)
    end
  end
  str = OUTPUT:get()
  return str
end

Copy link
Member

@bungle bungle Jun 4, 2024

Choose a reason for hiding this comment

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

This is another alternative, but it is as fast as above:

local byte = string.byte
local OUTPUT = require("string.buffer").new(32)
local ALNUM = {
  "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
  "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
  "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",   
}
local function random_string()
  OUTPUT:reset()
  local str = get_rand_bytes(32, true)
  for i = 1, 32 do
    OUTPUT:put(ALNUM[byte(str, i) % 62 + 1])
  end
  str = OUTPUT:get()
  return str
end

Copy link
Member

Choose a reason for hiding this comment

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

Here is one without string buffer:

local byte = string.byte
local concat = table.concat
local OUTPUT = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", }
local ALNUM = {
  "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
  "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
  "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",                                                                                                                                                                                                                                  
}
                                                                                                                                                                                                                                  
local function random_string()
  local str = get_rand_bytes(32, true)
  for i = 1, 32 do
    OUTPUT[i] = ALNUM[byte(str, i) % 62 + 1]
  end
  str = concat(OUTPUT)
  return str
end

end

end
Expand Down
Loading