From 735904a5496e9c9e416acc6840bcb6e90c527b89 Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Fri, 21 Apr 2023 17:28:28 +0300 Subject: [PATCH 1/2] Add better `bytesToString` implementation Add better `bytesToString` implementation from --- src/prometheus/util.lua | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/prometheus/util.lua b/src/prometheus/util.lua index 86960aa..b15fe1b 100644 --- a/src/prometheus/util.lua +++ b/src/prometheus/util.lua @@ -6,7 +6,7 @@ local logger = require("logger"); local bit32 = require("prometheus.bit").bit32; -local MAX_UNPACK_COUNT = 195; +local MAX_UNPACK_COUNT = 200; local function lookupify(tb) local tb2 = {}; @@ -223,21 +223,18 @@ local function readU32(arr) return val; end -local function bytesToString(arr) - local lenght = arr.n or #arr; +-- Adapted from https://github.com/Reselim/Base64/blob/master/Base64.lua#L16 +local function bytesToString(values) + local chunks = {} + local lenght = #values - if lenght < MAX_UNPACK_COUNT then - return string.char(table.unpack(arr)) + for i = 1, lenght, MAX_UNPACK_COUNT do + chunks[#chunks + 1] = string.char( + unpack(values, i, math.min(i + MAX_UNPACK_COUNT - 1, lenght)) + ) end - local str = ""; - local overflow = lenght % MAX_UNPACK_COUNT; - - for i = 1, (#arr - overflow) / MAX_UNPACK_COUNT do - str = str .. string.char(table.unpack(arr, (i - 1) * MAX_UNPACK_COUNT + 1, i * MAX_UNPACK_COUNT)); - end - - return str..(overflow > 0 and string.char(table.unpack(arr, lenght - overflow + 1, lenght)) or ""); + return table.concat(chunks, "") end local function isNaN(n) From 50f8129ab4bc95f496c4e8c8455e58509838ad1b Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Fri, 21 Apr 2023 17:37:01 +0300 Subject: [PATCH 2/2] Update util.lua --- src/prometheus/util.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/prometheus/util.lua b/src/prometheus/util.lua index b15fe1b..3cce31b 100644 --- a/src/prometheus/util.lua +++ b/src/prometheus/util.lua @@ -6,7 +6,7 @@ local logger = require("logger"); local bit32 = require("prometheus.bit").bit32; -local MAX_UNPACK_COUNT = 200; +local MAX_UNPACK_COUNT = 4096; local function lookupify(tb) local tb2 = {};