From ba6cecf5d9f233dbdcfe5b721e9794658f74e056 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 17:06:45 +0800 Subject: #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. --- script/vm/local-id.lua | 50 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) (limited to 'script/vm/local-id.lua') diff --git a/script/vm/local-id.lua b/script/vm/local-id.lua index 80c68769..bb12a927 100644 --- a/script/vm/local-id.lua +++ b/script/vm/local-id.lua @@ -5,7 +5,7 @@ local vm = require 'vm.vm' ---@class parser.object ---@field _localID string ----@field _localIDs table +---@field _localIDs table local compileLocalID, getLocal @@ -114,10 +114,19 @@ end function vm.insertLocalID(id, source) local root = guide.getRoot(source) if not root._localIDs then - root._localIDs = util.multiTable(2) + root._localIDs = util.multiTable(2, function () + return { + sets = {}, + gets = {}, + } + end) end local sources = root._localIDs[id] - sources[#sources+1] = source + if guide.isSet(source) then + sources.sets[#sources.sets+1] = source + else + sources.gets[#sources.gets+1] = source + end end function compileLocalID(source) @@ -154,7 +163,7 @@ end ---@param source parser.object ---@param key? string ---@return parser.object[]? -function vm.getLocalSources(source, key) +function vm.getLocalSourcesSets(source, key) local id = vm.getLocalID(source) if not id then return nil @@ -169,12 +178,34 @@ function vm.getLocalSources(source, key) end id = id .. vm.ID_SPLITE .. key end - return root._localIDs[id] + return root._localIDs[id].sets end ---@param source parser.object +---@param key? string +---@return parser.object[]? +function vm.getLocalSourcesGets(source, key) + local id = vm.getLocalID(source) + if not id then + return nil + end + local root = guide.getRoot(source) + if not root._localIDs then + return nil + end + if key then + if type(key) ~= 'string' then + return nil + end + id = id .. vm.ID_SPLITE .. key + end + return root._localIDs[id].gets +end + +---@param source parser.object +---@param includeGets boolean ---@return parser.object[] -function vm.getLocalFields(source) +function vm.getLocalFields(source, includeGets) local id = vm.getLocalID(source) if not id then return nil @@ -192,9 +223,14 @@ function vm.getLocalFields(source) and lid:sub(#id + 1, #id + 1) == vm.ID_SPLITE -- only one field and not lid:find(vm.ID_SPLITE, #id + 2) then - for _, src in ipairs(sources) do + for _, src in ipairs(sources.sets) do fields[#fields+1] = src end + if includeGets then + for _, src in ipairs(sources.gets) do + fields[#fields+1] = src + end + end end end local cost = os.clock() - clock -- cgit v1.2.3 From 07bdb57b7358bc62630c6cdaaacd68b151aa6d08 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 17:23:07 +0800 Subject: #1192 don't search local in simple use the methods provided by `local-id` to avoid wasting on `gets` --- script/vm/local-id.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'script/vm/local-id.lua') diff --git a/script/vm/local-id.lua b/script/vm/local-id.lua index bb12a927..e0c8770d 100644 --- a/script/vm/local-id.lua +++ b/script/vm/local-id.lua @@ -21,6 +21,7 @@ local compileSwitch = util.switch() compileLocalID(ref) end end) + : case 'setlocal' : case 'getlocal' : call(function (source) source._localID = ('%d'):format(source.node.start) -- cgit v1.2.3 From 6431519640a087499ea497066a78a6079993b945 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 17 Jun 2022 17:47:20 +0800 Subject: fix --- script/vm/local-id.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'script/vm/local-id.lua') diff --git a/script/vm/local-id.lua b/script/vm/local-id.lua index e0c8770d..ae3c711e 100644 --- a/script/vm/local-id.lua +++ b/script/vm/local-id.lua @@ -5,7 +5,7 @@ local vm = require 'vm.vm' ---@class parser.object ---@field _localID string ----@field _localIDs table +---@field _localIDs table local compileLocalID, getLocal -- cgit v1.2.3 From a04cffb43132645f63e5a319f6ca69e0df87dcdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 20 Jun 2022 19:20:53 +0800 Subject: update --- script/vm/local-id.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'script/vm/local-id.lua') diff --git a/script/vm/local-id.lua b/script/vm/local-id.lua index ae3c711e..802cea3a 100644 --- a/script/vm/local-id.lua +++ b/script/vm/local-id.lua @@ -4,7 +4,7 @@ local guide = require 'parser.guide' local vm = require 'vm.vm' ---@class parser.object ----@field _localID string +---@field _localID string|false ---@field _localIDs table local compileLocalID, getLocal -- cgit v1.2.3 From 08b9dd387a9b912c68749018b7dfe3c1df7094d5 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, 28 Jun 2022 21:06:53 +0800 Subject: cleanup --- script/vm/local-id.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'script/vm/local-id.lua') diff --git a/script/vm/local-id.lua b/script/vm/local-id.lua index 802cea3a..9168d680 100644 --- a/script/vm/local-id.lua +++ b/script/vm/local-id.lua @@ -147,7 +147,7 @@ function compileLocalID(source) end ---@param source parser.object ----@return string? +---@return string|false function vm.getLocalID(source) if source._localID ~= nil then return source._localID @@ -205,7 +205,7 @@ end ---@param source parser.object ---@param includeGets boolean ----@return parser.object[] +---@return parser.object[]? function vm.getLocalFields(source, includeGets) local id = vm.getLocalID(source) if not id then -- cgit v1.2.3