Skip to content

Commit 6c77d46

Browse files
committed
optimization some t[#t + 1]
1 parent 3289e0d commit 6c77d46

5 files changed

Lines changed: 35 additions & 31 deletions

File tree

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Collection.lua‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ local coroutine = coroutine
4141
local ccreate = coroutine.create
4242
local cresume = coroutine.resume
4343
local cyield = coroutine.yield
44-
local pairs = pairs
4544

4645
local Collection = {}
4746
local null = {}
@@ -532,7 +531,7 @@ local function ipairsArray(t)
532531
end, t, 1
533532
end
534533

535-
local pairsFn = pairs(null)
534+
local pairsFn = next
536535

537536
local function pairsDict(t)
538537
local version = versions[t]
@@ -647,8 +646,10 @@ function Collection.toArray(t)
647646
if isArrayLike(t) then
648647
tmove(t, 1, #t, 1, array)
649648
else
649+
local count = 1
650650
for _, v in each(t) do
651-
array[#array + 1] = wrap(v)
651+
array[count] = wrap(v)
652+
count = count + 1
652653
end
653654
end
654655
return System.arrayFromTable(array, t.__genericT__)
@@ -714,7 +715,7 @@ function DictionaryEnumerator.MoveNext(this)
714715
pair.Value = unWrap(v)
715716
this.current = pair
716717
elseif this.kind == 1 then
717-
this.current = unWrap(k)
718+
this.current = k
718719
else
719720
this.current = unWrap(v)
720721
end

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Convert.lua‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,8 +625,7 @@ local function toString(value, startIndex, length)
625625
t[len + 2] = 45
626626
len = len + 3
627627
end
628-
t[len - 1] = nil
629-
return schar(unpack(t))
628+
return schar(unpack(t, 1, len - 1))
630629
end
631630

632631
local function doubleToInt64Bits(value)

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Delegate.lua‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,15 @@ local function equalsMulticast(fn1, fn2, start, count)
8181
end
8282

8383
local function delete(fn, count, deleteIndex, deleteCount)
84-
local t = {}
85-
local count = 1
86-
setmetatable(t, multicast)
84+
local t = setmetatable({}, multicast)
85+
local len = 1
8786
for i = 1, deleteIndex - 1 do
88-
t[count] = fn[i]
89-
count = count + 1
87+
t[len] = fn[i]
88+
len = len + 1
9089
end
9190
for i = deleteIndex + deleteCount, count do
92-
t[count] = fn[i]
93-
count = count + 1
91+
t[len] = fn[i]
92+
len = len + 1
9493
end
9594
return t
9695
end

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Text/StringBuilder.lua‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,10 @@ function StringBuilder.AppendCharRepeat(this, v, repeatCount)
120120
if repeatCount < 0 then throw(ArgumentOutOfRangeException("repeatCount")) end
121121
if repeatCount == 0 then return this end
122122
v = schar(v)
123+
local count = #this + 1
123124
for i = 1, repeatCount do
124-
this[#this + 1] = v
125+
this[count] = v
126+
count = count + 1
125127
end
126128
addCount(this, repeatCount)
127129
return this
@@ -135,12 +137,14 @@ function StringBuilder.AppendFormat(this, format, ...)
135137
end
136138

137139
function StringBuilder.AppendLine(this, value)
138-
local count = 1;
140+
local count = 1
141+
local len = #this + 1
139142
if value ~= nil then
140-
this[#this + 1] = value
143+
this[len] = value
144+
len = len + 1
141145
count = count + #value
142146
end
143-
this[#this + 1] = "\n"
147+
this[len] = "\n"
144148
addCount(this, count)
145149
return this
146150
end

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Utilities.lua‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -249,21 +249,22 @@ define("System.Guid", {
249249
local b = ...
250250
if b == nil then throw(ArgumentNullException()) end
251251
if type(b) ~= "string" then
252-
local i = sl(b:get(3), 24)
253-
i = bor(i, sl(b:get(2), 16))
254-
i = bor(i, sl(b:get(1), 8))
255-
i = bor(i, b:get(0))
252+
local get = b.get
253+
local i = sl(get(b, 3), 24)
254+
i = bor(i, sl(get(b, 2), 16))
255+
i = bor(i, sl(get(b, 1), 8))
256+
i = bor(i, get(b, 0))
256257
this.a = i
257-
this.b = bor(sl(b:get(5), 8), b:get(4))
258-
this.c = bor(sl(b:get(7), 8), b:get(6))
259-
this.d = b:get(8)
260-
this.e = b:get(9)
261-
this.f = b:get(10)
262-
this.g = b:get(11)
263-
this.h = b:get(12)
264-
this.i = b:get(13)
265-
this.j = b:get(14)
266-
this.k = b:get(15)
258+
this.b = bor(sl(get(b, 5), 8), get(b, 4))
259+
this.c = bor(sl(get(b, 7), 8), get(b, 6))
260+
this.d = get(b, 8)
261+
this.e = get(b, 9)
262+
this.f = get(b, 10)
263+
this.g = get(b, 11)
264+
this.h = get(b, 12)
265+
this.i = get(b, 13)
266+
this.j = get(b, 14)
267+
this.k = get(b, 15)
267268
else
268269
assert(false)
269270
end

0 commit comments

Comments
 (0)