Skip to content

Commit 1ca3b7d

Browse files
committed
improve performance
1 parent c796d40 commit 1ca3b7d

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

script/parser/guide.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1253,9 +1253,13 @@ function m.getTopBlock(source)
12531253
if not m.isBlockType(block) then
12541254
return nil
12551255
end
1256-
if block.type ~= 'do' then
1256+
if block.type ~= 'do'
1257+
and block.type ~= 'in'
1258+
and block.type ~= 'loop'
1259+
and block.type ~= 'repeat' then
12571260
return block
12581261
end
1262+
source = block
12591263
end
12601264
return nil
12611265
end

script/vm/tracer.lua

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ local util = require 'utility'
2121
---@field castIndex integer?
2222
local mt = {}
2323
mt.__index = mt
24+
mt.fastCalc = true
2425

2526
---@return parser.object[]
2627
function mt:getCasts()
@@ -66,6 +67,21 @@ function mt:collectCare(obj)
6667
return
6768
end
6869
self.careMap[obj] = true
70+
71+
if self.fastCalc then
72+
if obj.type == 'if'
73+
or obj.type == 'while'
74+
or obj.type == 'binary' then
75+
self.fastCalc = false
76+
end
77+
if obj.type == 'call' and obj.node then
78+
if obj.node.special == 'assert'
79+
or obj.node.special == 'type' then
80+
self.fastCalc = false
81+
end
82+
end
83+
end
84+
6985
obj = obj.parent
7086
end
7187
end
@@ -104,6 +120,10 @@ function mt:collectLocal()
104120
self.casts[#self.casts+1] = cast
105121
end
106122
end
123+
124+
if #self.casts > 0 then
125+
self.fastCalc = false
126+
end
107127
end
108128

109129
function mt:collectGlobal()
@@ -139,15 +159,15 @@ end
139159
---@param finish integer
140160
---@return parser.object?
141161
function mt:getLastAssign(start, finish)
142-
local assign
162+
local assign = self.assigns[1]
143163
for _, obj in ipairs(self.assigns) do
144164
if obj.start < start then
145165
goto CONTINUE
146166
end
147167
if (obj.range or obj.start) >= finish then
148168
break
149169
end
150-
local objBlock = guide.getParentBlock(obj)
170+
local objBlock = guide.getTopBlock(obj)
151171
if not objBlock then
152172
break
153173
end
@@ -683,21 +703,35 @@ function mt:lookIntoBlock(block, start, node)
683703
end
684704
if self.careMap[action] then
685705
node = self:lookIntoChild(action, node)
706+
if action.type == 'do'
707+
or action.type == 'loop'
708+
or action.type == 'in'
709+
or action.type == 'repeat' then
710+
return
711+
end
686712
end
687713
if action.finish > start and self.assignMap[action] then
688714
return
689715
end
690716
::CONTINUE::
691717
end
692718
self.nodes[block] = node
719+
if block.type == 'do'
720+
or block.type == 'loop'
721+
or block.type == 'in'
722+
or block.type == 'repeat' then
723+
self:lookIntoBlock(block.parent, block.finish, node)
724+
end
693725
end
694726

695727
---@param source parser.object
696728
function mt:calcNode(source)
697729
if self.getMap[source] then
698730
local lastAssign = self:getLastAssign(0, source.finish)
699-
if not lastAssign then
700-
lastAssign = source.node
731+
assert(lastAssign)
732+
if self.fastCalc then
733+
self.nodes[source] = vm.compileNode(lastAssign)
734+
return
701735
end
702736
self:calcNode(lastAssign)
703737
return

0 commit comments

Comments
 (0)