From 335b928caedd42543caff7ded57cddd400ec7c34 Mon Sep 17 00:00:00 2001 From: rxi Date: Tue, 1 Apr 2014 12:17:55 +0100 Subject: [PATCH] Removed function in lume.slice() Moved the function index() from lume.slice()'s body to local var absindex, this improves the performance of lume.slice() on luajit significantly and improves it to a lesser extent on non-jit lua. --- lume.lua | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lume.lua b/lume.lua index 2c7e0c5..2933bd9 100644 --- a/lume.lua +++ b/lume.lua @@ -27,6 +27,10 @@ local patternescape = function(str) return str:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", "%%%1") end +local absindex = function(len, i) + return i < 0 and (len + i + 1) or i +end + function lume.clamp(x, min, max) return x < min and min or (x > max and max or x) @@ -194,9 +198,8 @@ end function lume.slice(t, i, j) - local function index(x) return x < 0 and (#t + x + 1) or x end - i = i and index(i) or 1 - j = j and index(j) or #t + i = i and absindex(#t, i) or 1 + j = j and absindex(#t, j) or #t local rtn = {} for i = math_max(i, 1), math_min(j, #t) do rtn[#rtn + 1] = t[i]