From 4b87fcfa6ccea234a4878a95ffb2c9bbf32c6f17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Tue, 7 Jun 2022 20:15:05 +0800 Subject: #1192 improve `[[--@as number]]` cache ases and search with dichotomy --- script/vm/compiler.lua | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) (limited to 'script') diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua index 797fa901..1c3be7c8 100644 --- a/script/vm/compiler.lua +++ b/script/vm/compiler.lua @@ -519,14 +519,47 @@ local function bindAs(source) if not docs then return end - for _, doc in ipairs(docs) do - if doc.type == 'doc.as' and doc.originalComment.start == source.finish + 2 then - if doc.as then - vm.setNode(source, vm.compileNode(doc.as), true) + local ases = docs._asCache + if not ases then + ases = {} + docs._asCache = ases + for _, doc in ipairs(docs) do + if doc.type == 'doc.as' and doc.as then + ases[#ases+1] = doc end - return true + end + table.sort(ases, function (a, b) + return a.start < b.start + end) + end + + local max = #ases + local index + local left = 1 + local right = max + for _ = 1, 1000 do + index = left + (right - left) // 2 + if index <= left then + index = left + break + elseif index >= right then + index = right + break + end + local doc = ases[index] + if doc.originalComment.start < source.finish + 2 then + left = index + 1 + else + right = index end end + + local doc = ases[index] + if doc and doc.originalComment.start == source.finish + 2 then + vm.setNode(source, vm.compileNode(doc.as), true) + return true + end + return false end -- cgit v1.2.3