Skip to content

Commit 4b87fcf

Browse files
committed
#1192 improve [[--@as number]]
cache ases and search with dichotomy
1 parent 83b2804 commit 4b87fcf

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

script/vm/compiler.lua

+38-5
Original file line numberDiff line numberDiff line change
@@ -519,14 +519,47 @@ local function bindAs(source)
519519
if not docs then
520520
return
521521
end
522-
for _, doc in ipairs(docs) do
523-
if doc.type == 'doc.as' and doc.originalComment.start == source.finish + 2 then
524-
if doc.as then
525-
vm.setNode(source, vm.compileNode(doc.as), true)
522+
local ases = docs._asCache
523+
if not ases then
524+
ases = {}
525+
docs._asCache = ases
526+
for _, doc in ipairs(docs) do
527+
if doc.type == 'doc.as' and doc.as then
528+
ases[#ases+1] = doc
526529
end
527-
return true
530+
end
531+
table.sort(ases, function (a, b)
532+
return a.start < b.start
533+
end)
534+
end
535+
536+
local max = #ases
537+
local index
538+
local left = 1
539+
local right = max
540+
for _ = 1, 1000 do
541+
index = left + (right - left) // 2
542+
if index <= left then
543+
index = left
544+
break
545+
elseif index >= right then
546+
index = right
547+
break
548+
end
549+
local doc = ases[index]
550+
if doc.originalComment.start < source.finish + 2 then
551+
left = index + 1
552+
else
553+
right = index
528554
end
529555
end
556+
557+
local doc = ases[index]
558+
if doc and doc.originalComment.start == source.finish + 2 then
559+
vm.setNode(source, vm.compileNode(doc.as), true)
560+
return true
561+
end
562+
530563
return false
531564
end
532565

0 commit comments

Comments
 (0)