Skip to content

Commit 6df6b2a

Browse files
committedJun 1, 2015
Localize inside whole misc_helpers.lua
1 parent 617a3d4 commit 6df6b2a

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed
 

‎builtin/common/misc_helpers.lua

+22-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
-- Minetest: builtin/misc_helpers.lua
22

3+
--------------------------------------------------------------------------------
4+
-- Localize functions to avoid table lookups (better performance).
5+
local table_insert = table.insert
6+
local string_sub, string_sub = string.sub, string.find
7+
38
--------------------------------------------------------------------------------
49
function basic_dump(o)
510
local tp = type(o)
@@ -89,13 +94,13 @@ function dump2(o, name, dumped)
8994
-- the form _G["table: 0xFFFFFFF"]
9095
keyStr = string.format("_G[%q]", tostring(k))
9196
-- Dump key table
92-
table.insert(t, dump2(k, keyStr, dumped))
97+
table_insert(t, dump2(k, keyStr, dumped))
9398
end
9499
else
95100
keyStr = basic_dump(k)
96101
end
97102
local vname = string.format("%s[%s]", name, keyStr)
98-
table.insert(t, dump2(v, vname, dumped))
103+
table_insert(t, dump2(v, vname, dumped))
99104
end
100105
return string.format("%s = {}\n%s", name, table.concat(t))
101106
end
@@ -130,7 +135,7 @@ function dump(o, indent, nested, level)
130135
local t = {}
131136
local dumped_indexes = {}
132137
for i, v in ipairs(o) do
133-
table.insert(t, dump(v, indent, nested, level + 1))
138+
table_insert(t, dump(v, indent, nested, level + 1))
134139
dumped_indexes[i] = true
135140
end
136141
for k, v in pairs(o) do
@@ -139,7 +144,7 @@ function dump(o, indent, nested, level)
139144
k = "["..dump(k, indent, nested, level + 1).."]"
140145
end
141146
v = dump(v, indent, nested, level + 1)
142-
table.insert(t, k.." = "..v)
147+
table_insert(t, k.." = "..v)
143148
end
144149
end
145150
nested[o] = nil
@@ -154,10 +159,6 @@ function dump(o, indent, nested, level)
154159
return "{"..table.concat(t, ", ").."}"
155160
end
156161

157-
--------------------------------------------------------------------------------
158-
-- Localize functions to avoid table lookups (better performance).
159-
local table_insert = table.insert
160-
local str_sub, str_find = string.sub, string.find
161162
function string.split(str, delim, include_empty, max_splits, sep_is_pattern)
162163
delim = delim or ","
163164
max_splits = max_splits or -1
@@ -166,13 +167,13 @@ function string.split(str, delim, include_empty, max_splits, sep_is_pattern)
166167
local plain = not sep_is_pattern
167168
max_splits = max_splits + 1
168169
repeat
169-
local np, npe = str_find(str, delim, pos, plain)
170+
local np, npe = string_sub(str, delim, pos, plain)
170171
np, npe = (np or (len+1)), (npe or (len+1))
171172
if (not np) or (max_splits == 1) then
172173
np = len + 1
173174
npe = np
174175
end
175-
local s = str_sub(str, pos, np - 1)
176+
local s = string_sub(str, pos, np - 1)
176177
if include_empty or (s ~= "") then
177178
max_splits = max_splits - 1
178179
table_insert(items, s)
@@ -298,8 +299,8 @@ function core.splittext(text,charlimit)
298299

299300
local current_idx = 1
300301

301-
local start,stop = string.find(text," ",current_idx)
302-
local nl_start,nl_stop = string.find(text,"\n",current_idx)
302+
local start,stop = string_find(text, " ", current_idx)
303+
local nl_start,nl_stop = string_find(text, "\n", current_idx)
303304
local gotnewline = false
304305
if nl_start ~= nil and (start == nil or nl_start < start) then
305306
start = nl_start
@@ -309,25 +310,25 @@ function core.splittext(text,charlimit)
309310
local last_line = ""
310311
while start ~= nil do
311312
if string.len(last_line) + (stop-start) > charlimit then
312-
table.insert(retval,last_line)
313+
table_insert(retval, last_line)
313314
last_line = ""
314315
end
315316

316317
if last_line ~= "" then
317318
last_line = last_line .. " "
318319
end
319320

320-
last_line = last_line .. string.sub(text,current_idx,stop -1)
321+
last_line = last_line .. string_sub(text, current_idx, stop - 1)
321322

322323
if gotnewline then
323-
table.insert(retval,last_line)
324+
table_insert(retval, last_line)
324325
last_line = ""
325326
gotnewline = false
326327
end
327328
current_idx = stop+1
328329

329-
start,stop = string.find(text," ",current_idx)
330-
nl_start,nl_stop = string.find(text,"\n",current_idx)
330+
start,stop = string_find(text, " ", current_idx)
331+
nl_start,nl_stop = string_find(text, "\n", current_idx)
331332

332333
if nl_start ~= nil and (start == nil or nl_start < start) then
333334
start = nl_start
@@ -338,11 +339,11 @@ function core.splittext(text,charlimit)
338339

339340
--add last part of text
340341
if string.len(last_line) + (string.len(text) - current_idx) > charlimit then
341-
table.insert(retval,last_line)
342-
table.insert(retval,string.sub(text,current_idx))
342+
table_insert(retval, last_line)
343+
table_insert(retval, string_sub(text, current_idx))
343344
else
344-
last_line = last_line .. " " .. string.sub(text,current_idx)
345-
table.insert(retval,last_line)
345+
last_line = last_line .. " " .. string_sub(text, current_idx)
346+
table_insert(retval, last_line)
346347
end
347348

348349
return retval

0 commit comments

Comments
 (0)
Please sign in to comment.