Skip to content

Commit b62eba6

Browse files
committed
improve performance
1 parent a6cfc11 commit b62eba6

File tree

8 files changed

+103
-31
lines changed

8 files changed

+103
-31
lines changed

script/core/completion.lua

+23
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,9 @@ local function checkCommon(ast, word, text, offset, results)
604604
if config.config.completion.workspaceWord and #word >= 2 then
605605
local myHead = word:sub(1, 2)
606606
for uri in files.eachFile() do
607+
if #results >= 100 then
608+
break
609+
end
607610
if myUri and files.eq(myUri, uri) then
608611
goto CONTINUE
609612
end
@@ -623,6 +626,9 @@ local function checkCommon(ast, word, text, offset, results)
623626
end
624627
end
625628
for _, str in ipairs(cache.commonWords[myHead] or {}) do
629+
if #results >= 100 then
630+
break
631+
end
626632
if not used[str]
627633
and str ~= word then
628634
used[str] = true
@@ -637,8 +643,14 @@ local function checkCommon(ast, word, text, offset, results)
637643
::CONTINUE::
638644
end
639645
for uri in files.eachDll() do
646+
if #results >= 100 then
647+
break
648+
end
640649
local words = files.getDllWords(uri) or {}
641650
for _, str in ipairs(words) do
651+
if #results >= 100 then
652+
break
653+
end
642654
if #str >= 3 and not used[str] and str ~= word then
643655
used[str] = true
644656
if matchKey(word, str) then
@@ -652,6 +664,9 @@ local function checkCommon(ast, word, text, offset, results)
652664
end
653665
end
654666
for str, pos in text:gmatch '([%a_][%w_]+)()' do
667+
if #results >= 100 then
668+
break
669+
end
655670
if #str >= 3 and not used[str] and pos - 1 ~= offset then
656671
used[str] = true
657672
if matchKey(word, str) then
@@ -1216,7 +1231,9 @@ local function trySymbol(ast, text, offset, results)
12161231
or symbol == ':' then
12171232
local parent, oop = findParent(ast, text, start)
12181233
if parent then
1234+
tracy.ZoneBeginN 'completion.trySymbol'
12191235
checkField(ast, '', start, offset, parent, oop, results)
1236+
tracy.ZoneEnd()
12201237
end
12211238
end
12221239
if symbol == '(' then
@@ -1673,10 +1690,15 @@ local function tryComment(ast, text, offset, results)
16731690
end
16741691

16751692
local function completion(uri, offset)
1693+
tracy.ZoneBeginN 'completion.getAst'
16761694
local ast = files.getAst(uri)
1695+
tracy.ZoneEnd()
1696+
tracy.ZoneBeginN 'completion.getText'
16771697
local text = files.getText(uri)
1698+
tracy.ZoneEnd()
16781699
local results = {}
16791700
clearStack()
1701+
tracy.ZoneBeginN 'completion'
16801702
if ast then
16811703
if getComment(ast, offset) then
16821704
tryLuaDoc(ast, text, offset, results)
@@ -1694,6 +1716,7 @@ local function completion(uri, offset)
16941716
checkCommon(ast, word, text, offset, results)
16951717
end
16961718
end
1719+
tracy.ZoneEnd()
16971720

16981721
if #results == 0 then
16991722
return nil

script/files.lua

+6
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ function m.setText(uri, text)
103103
if not text then
104104
return
105105
end
106+
--log.debug('setText', uri)
106107
local originUri = uri
107108
uri = getUriKey(uri)
108109
local create
@@ -284,7 +285,12 @@ function m.compileAst(uri, text)
284285
if state then
285286
state.uri = uri
286287
state.ast.uri = uri
288+
local clock = os.clock()
287289
parser:luadoc(state)
290+
local passed = os.clock() - clock
291+
if passed > 0.1 then
292+
log.warn(('Parse LuaDoc of [%s] takes [%.3f] sec, size [%.3f] kb.'):format(uri, passed, #text / 1000))
293+
end
288294
return state
289295
else
290296
log.error(err)

script/parser/guide.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -2535,9 +2535,9 @@ function m.searchSameFields(status, simple, mode)
25352535
max = max + 1
25362536
status.share.count = status.share.count + 1
25372537
if status.share.count % 10000 == 0 then
2538-
if TEST then
2539-
print('####', status.share.count, osClock() - status.clock)
2540-
end
2538+
--if TEST then
2539+
-- print('####', status.share.count, osClock() - status.clock)
2540+
--end
25412541
if status.interface and status.interface.pulse then
25422542
status.interface.pulse()
25432543
end
@@ -4212,7 +4212,7 @@ function m.requestReference(obj, interface, deep)
42124212
m.searchRefsAsFunction(status, obj, 'ref')
42134213

42144214
if m.debugMode then
4215-
print('count:', status.share.count)
4215+
--print('count:', status.share.count)
42164216
end
42174217

42184218
return status.results, status.share.count

script/parser/luadoc.lua

+55-22
Original file line numberDiff line numberDiff line change
@@ -992,28 +992,43 @@ local function bindGeneric(binded)
992992
end
993993
end
994994

995-
local function bindDocsBetween(state, binded, bindSources, start, finish)
996-
guide.eachSourceBetween(state.ast, start, finish, function (src)
997-
if src.start and src.start < start then
998-
return
995+
local function bindDocsBetween(sources, binded, bindSources, start, finish)
996+
-- 用二分法找到第一个
997+
local max = #sources
998+
local index
999+
local left = 1
1000+
local right = max
1001+
for _ = 1, 1000 do
1002+
index = left + (right - left) // 2
1003+
if index <= left then
1004+
index = left
1005+
break
1006+
elseif index >= right then
1007+
index = right
1008+
break
9991009
end
1000-
if src.type == 'local'
1001-
or src.type == 'setlocal'
1002-
or src.type == 'setglobal'
1003-
or src.type == 'setfield'
1004-
or src.type == 'setmethod'
1005-
or src.type == 'setindex'
1006-
or src.type == 'tablefield'
1007-
or src.type == 'tableindex'
1008-
or src.type == 'function'
1009-
or src.type == '...' then
1010-
src.bindDocs = binded
1011-
bindSources[#bindSources+1] = src
1010+
local src = sources[index]
1011+
if src.start < start then
1012+
left = index
1013+
else
1014+
right = index
10121015
end
1013-
end)
1016+
end
1017+
for i = index - 1, max do
1018+
local src = sources[i]
1019+
if src then
1020+
if src.start > finish then
1021+
break
1022+
end
1023+
if src.start >= start then
1024+
src.bindDocs = binded
1025+
bindSources[#bindSources+1] = src
1026+
end
1027+
end
1028+
end
10141029
end
10151030

1016-
local function bindDoc(state, lns, binded)
1031+
local function bindDoc(sources, lns, binded)
10171032
if not binded then
10181033
return
10191034
end
@@ -1030,24 +1045,42 @@ local function bindDoc(state, lns, binded)
10301045
local row = guide.positionOf(lns, lastDoc.finish)
10311046
local cstart, cfinish = guide.lineRange(lns, row)
10321047
local nstart, nfinish = guide.lineRange(lns, row + 1)
1033-
bindDocsBetween(state, binded, bindSources, cstart, cfinish)
1048+
bindDocsBetween(sources, binded, bindSources, cstart, cfinish)
10341049
if #bindSources == 0 then
1035-
bindDocsBetween(state, binded, bindSources, nstart, nfinish)
1050+
bindDocsBetween(sources, binded, bindSources, nstart, nfinish)
10361051
end
10371052
end
10381053

10391054
local function bindDocs(state)
10401055
local lns = lines(nil, state.lua)
1056+
local sources = {}
1057+
guide.eachSource(state.ast, function (src)
1058+
if src.type == 'local'
1059+
or src.type == 'setlocal'
1060+
or src.type == 'setglobal'
1061+
or src.type == 'setfield'
1062+
or src.type == 'setmethod'
1063+
or src.type == 'setindex'
1064+
or src.type == 'tablefield'
1065+
or src.type == 'tableindex'
1066+
or src.type == 'function'
1067+
or src.type == '...' then
1068+
sources[#sources+1] = src
1069+
end
1070+
end)
1071+
table.sort(sources, function (a, b)
1072+
return a.start < b.start
1073+
end)
10411074
local binded
10421075
for _, doc in ipairs(state.ast.docs) do
10431076
if not isNextLine(lns, binded, doc) then
1044-
bindDoc(state, lns, binded)
1077+
bindDoc(sources, lns, binded)
10451078
binded = {}
10461079
state.ast.docs.groups[#state.ast.docs.groups+1] = binded
10471080
end
10481081
binded[#binded+1] = doc
10491082
end
1050-
bindDoc(state, lns, binded)
1083+
bindDoc(sources, lns, binded)
10511084
end
10521085

10531086
return function (_, state)

script/plugin.lua

+2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ function m.dispatch(event, ...)
1313
if type(method) ~= 'function' then
1414
return false
1515
end
16+
tracy.ZoneBeginN('plugin dispatch:' .. event)
1617
local suc, res1, res2 = xpcall(method, log.error, ...)
18+
tracy.ZoneEnd()
1719
if suc then
1820
return true, res1, res2
1921
end

script/provider/provider.lua

+1
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ end)
353353
proto.on('textDocument/completion', function (params)
354354
--log.info(util.dump(params))
355355
local core = require 'core.completion'
356+
--log.debug('textDocument/completion')
356357
--log.debug('completion:', params.context and params.context.triggerKind, params.context and params.context.triggerCharacter)
357358
local uri = params.textDocument.uri
358359
if not files.exists(uri) then

script/vm/getGlobals.lua

-2
Original file line numberDiff line numberDiff line change
@@ -238,5 +238,3 @@ files.watch(function (ev, uri)
238238
end
239239
end
240240
end)
241-
242-
require 'tracy'.enable()

test/full/example.lua

+12-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ local parser = require 'parser'
33
local files = require 'files'
44
local diag = require 'core.diagnostics'
55
local config = require 'config'
6+
local fs = require 'bee.filesystem'
7+
local luadoc = require "parser.luadoc"
68

79
-- 临时
810
local function testIfExit(path)
@@ -16,23 +18,29 @@ local function testIfExit(path)
1618
local need
1719
local parseClock = 0
1820
local compileClock = 0
21+
local luadocClock = 0
1922
local total
2023
for i = 1, max do
2124
vm = TEST(buf)
25+
local luadocStart = os.clock()
26+
luadoc(nil, vm)
27+
local luadocPassed = os.clock() - luadocStart
2228
local passed = os.clock() - clock
23-
parseClock = parseClock + vm.parseClock
29+
parseClock = parseClock + vm.parseClock
2430
compileClock = compileClock + vm.compileClock
31+
luadocClock = luadocClock + luadocPassed
2532
if passed >= 1.0 or i == max then
2633
need = passed / i
2734
total = i
2835
break
2936
end
3037
end
31-
print(('基准编译测试[%s]单次耗时:%.10f(解析:%.10f, 编译:%.10f)'):format(
38+
print(('基准编译测试[%s]单次耗时:%.10f(解析:%.10f, 编译:%.10f, LuaDoc: %.10f)'):format(
3239
path:filename():string(),
3340
need,
3441
parseClock / total,
35-
compileClock / total
42+
compileClock / total,
43+
luadocClock / total
3644
))
3745

3846
local clock = os.clock()
@@ -58,3 +66,4 @@ require 'tracy' .enable()
5866
testIfExit(ROOT / 'test' / 'example' / 'vm.txt')
5967
testIfExit(ROOT / 'test' / 'example' / 'largeGlobal.txt')
6068
testIfExit(ROOT / 'test' / 'example' / 'guide.txt')
69+
testIfExit(fs.path [[D:\github\test\ECObject.lua]])

0 commit comments

Comments
 (0)