From d75748c60ccadad4a3e80a73d460e59e05ea8267 Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Wed, 6 Nov 2024 20:30:21 +0200 Subject: [PATCH] O(n) instead of O(n^2) string concactenation (#54) Ported my optimisation from FiOne [#19](https://github.com/Rerumu/FiOne/pull/19) TLDR; concat function is always faster even with pcall (at least in Luau) https://github.com/Rerumu/FiOne/pull/19#issuecomment-2077599885 --- Source.lua | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Source.lua b/Source.lua index 4d3e3ef..c43e39a 100644 --- a/Source.lua +++ b/Source.lua @@ -14,6 +14,7 @@ local table_unpack = table.unpack local table_create = table.create local table_insert = table.insert local table_remove = table.remove +local table_concat = table.concat local coroutine_create = coroutine.create local coroutine_yield = coroutine.yield @@ -1018,10 +1019,17 @@ local function luau_load(module, env, luau_settings) local value = stack[inst.B] stack[inst.A] = if value then value else inst.K or false elseif op == 49 then --[[ CONCAT ]] - local s = "" - for i = inst.B, inst.C do - s ..= stack[i] + local B, C = inst.B, inst.C + local success, s = pcall(table_concat, stack, "", B, C) + + if not success then + str = stack[B] + + for i = B + 1, C do + str ..= stack[i] + end end + stack[inst.A] = s elseif op == 50 then --[[ NOT ]] stack[inst.A] = not stack[inst.B]