Skip to content

Commit ba6cecf

Browse files
committed
#1192 improve local id
In most cases, we only need to get `sets`. In general, there are few `sets` and many `gets`. Therefore, separating these two cases can significantly improve performance.
1 parent c701cce commit ba6cecf

File tree

6 files changed

+62
-25
lines changed

6 files changed

+62
-25
lines changed

script/vm/compiler.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ local searchFieldSwitch = util.switch()
5959
: call(function (suri, node, key, ref, pushResult)
6060
local fields
6161
if key then
62-
fields = vm.getLocalSources(node, key)
62+
fields = vm.getLocalSourcesSets(node, key)
6363
else
64-
fields = vm.getLocalFields(node)
64+
fields = vm.getLocalFields(node, false)
6565
end
6666
if fields then
6767
for _, src in ipairs(fields) do
@@ -586,7 +586,7 @@ local function bindDocs(source)
586586
end
587587

588588
local function compileByLocalID(source)
589-
local sources = vm.getLocalSources(source)
589+
local sources = vm.getLocalSourcesSets(source)
590590
if not sources then
591591
return
592592
end

script/vm/def.lua

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,10 @@ local searchFieldSwitch = util.switch()
115115
end)
116116
: case 'local'
117117
: call(function (suri, obj, key, pushResult)
118-
local sources = vm.getLocalSources(obj, key)
118+
local sources = vm.getLocalSourcesSets(obj, key)
119119
if sources then
120120
for _, src in ipairs(sources) do
121-
if guide.isSet(src) then
122-
pushResult(src)
123-
end
121+
pushResult(src)
124122
end
125123
end
126124
end)
@@ -194,14 +192,12 @@ end
194192
---@param source parser.object
195193
---@param pushResult fun(src: parser.object)
196194
local function searchByLocalID(source, pushResult)
197-
local idSources = vm.getLocalSources(source)
195+
local idSources = vm.getLocalSourcesSets(source)
198196
if not idSources then
199197
return
200198
end
201199
for _, src in ipairs(idSources) do
202-
if guide.isSet(src) then
203-
pushResult(src)
204-
end
200+
pushResult(src)
205201
end
206202
end
207203

script/vm/field.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ local searchByNodeSwitch = util.switch()
1616
end)
1717

1818
local function searchByLocalID(source, pushResult)
19-
local fields = vm.getLocalFields(source)
19+
local fields = vm.getLocalFields(source, true)
2020
if fields then
2121
for _, field in ipairs(fields) do
2222
pushResult(field)

script/vm/global.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ local function compileSelf(source)
446446
if not node then
447447
return
448448
end
449-
local fields = vm.getLocalFields(source)
449+
local fields = vm.getLocalFields(source, false)
450450
if not fields then
451451
return
452452
end

script/vm/local-id.lua

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local vm = require 'vm.vm'
55

66
---@class parser.object
77
---@field _localID string
8-
---@field _localIDs table<string, parser.object[]>
8+
---@field _localIDs table<string, { sets: parser.object[], gets: parser.object[] }[]>
99

1010
local compileLocalID, getLocal
1111

@@ -114,10 +114,19 @@ end
114114
function vm.insertLocalID(id, source)
115115
local root = guide.getRoot(source)
116116
if not root._localIDs then
117-
root._localIDs = util.multiTable(2)
117+
root._localIDs = util.multiTable(2, function ()
118+
return {
119+
sets = {},
120+
gets = {},
121+
}
122+
end)
118123
end
119124
local sources = root._localIDs[id]
120-
sources[#sources+1] = source
125+
if guide.isSet(source) then
126+
sources.sets[#sources.sets+1] = source
127+
else
128+
sources.gets[#sources.gets+1] = source
129+
end
121130
end
122131

123132
function compileLocalID(source)
@@ -154,7 +163,7 @@ end
154163
---@param source parser.object
155164
---@param key? string
156165
---@return parser.object[]?
157-
function vm.getLocalSources(source, key)
166+
function vm.getLocalSourcesSets(source, key)
158167
local id = vm.getLocalID(source)
159168
if not id then
160169
return nil
@@ -169,12 +178,34 @@ function vm.getLocalSources(source, key)
169178
end
170179
id = id .. vm.ID_SPLITE .. key
171180
end
172-
return root._localIDs[id]
181+
return root._localIDs[id].sets
173182
end
174183

175184
---@param source parser.object
185+
---@param key? string
186+
---@return parser.object[]?
187+
function vm.getLocalSourcesGets(source, key)
188+
local id = vm.getLocalID(source)
189+
if not id then
190+
return nil
191+
end
192+
local root = guide.getRoot(source)
193+
if not root._localIDs then
194+
return nil
195+
end
196+
if key then
197+
if type(key) ~= 'string' then
198+
return nil
199+
end
200+
id = id .. vm.ID_SPLITE .. key
201+
end
202+
return root._localIDs[id].gets
203+
end
204+
205+
---@param source parser.object
206+
---@param includeGets boolean
176207
---@return parser.object[]
177-
function vm.getLocalFields(source)
208+
function vm.getLocalFields(source, includeGets)
178209
local id = vm.getLocalID(source)
179210
if not id then
180211
return nil
@@ -192,9 +223,14 @@ function vm.getLocalFields(source)
192223
and lid:sub(#id + 1, #id + 1) == vm.ID_SPLITE
193224
-- only one field
194225
and not lid:find(vm.ID_SPLITE, #id + 2) then
195-
for _, src in ipairs(sources) do
226+
for _, src in ipairs(sources.sets) do
196227
fields[#fields+1] = src
197228
end
229+
if includeGets then
230+
for _, src in ipairs(sources.gets) do
231+
fields[#fields+1] = src
232+
end
233+
end
198234
end
199235
end
200236
local cost = os.clock() - clock

script/vm/ref.lua

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,17 @@ end
240240
---@param source parser.object
241241
---@param pushResult fun(src: parser.object)
242242
local function searchByLocalID(source, pushResult)
243-
local idSources = vm.getLocalSources(source)
244-
if not idSources then
245-
return
243+
local sourceSets = vm.getLocalSourcesSets(source)
244+
if sourceSets then
245+
for _, src in ipairs(sourceSets) do
246+
pushResult(src)
247+
end
246248
end
247-
for _, src in ipairs(idSources) do
248-
pushResult(src)
249+
local sourceGets = vm.getLocalSourcesGets(source)
250+
if sourceGets then
251+
for _, src in ipairs(sourceGets) do
252+
pushResult(src)
253+
end
249254
end
250255
end
251256

0 commit comments

Comments
 (0)