Skip to content

Commit

Permalink
O(n) instead of O(n^2) string concactenation (#54)
Browse files Browse the repository at this point in the history
Ported my optimisation from FiOne
[#19](Rerumu/FiOne#19)

TLDR; concat function is always faster even with pcall (at least in
Luau) Rerumu/FiOne#19 (comment)
  • Loading branch information
ccuser44 authored Nov 6, 2024
1 parent f359ba2 commit d75748c
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions Source.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit d75748c

Please sign in to comment.