From f33b06fd928fd3541eea99b11ee6caf9b249a49b 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, 8 Apr 2022 16:27:36 +0800 Subject: try flush cache --- script/vm/compiler.lua | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) (limited to 'script/vm/compiler.lua') diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua index eeea3c87..08fc2296 100644 --- a/script/vm/compiler.lua +++ b/script/vm/compiler.lua @@ -659,6 +659,7 @@ local compilerSwitch = util.switch() if not source.value and source.ref and not hasMarkDoc then + m.pauseCache() for _, ref in ipairs(source.ref) do if ref.type == 'setlocal' then if ref.value and ref.value.type == 'table' then @@ -668,6 +669,7 @@ local compilerSwitch = util.switch() end end end + m.resumeCache() end -- function x.y(self, ...) --> function x:y(...) if source[1] == 'self' @@ -707,20 +709,6 @@ local compilerSwitch = util.switch() if source.parent.type == 'loop' then nodeMgr.setNode(source, globalMgr.getGlobal('type', 'integer')) end - - -- avoid self reference - -- `local x; x = x` - -- the third `x` is unknown here - -- x[1] -> value of x[2] -> x[3] -> x[1](locked!) - if source.ref then - local myNode = nodeMgr.getNode(source) - for _, ref in ipairs(source.ref) do - if ref.type == 'setlocal' - or ref.type == 'getlocal' then - nodeMgr.setNode(ref, myNode, true) - end - end - end end) : case 'setlocal' : case 'getlocal' @@ -1413,6 +1401,24 @@ local function compileByGlobal(source) end end +local pauseCacheCount = 0 +local originCacheKeys = {} +local originCacheValues = {} +function m.pauseCache() + pauseCacheCount = pauseCacheCount + 1 +end + +function m.resumeCache() + pauseCacheCount = pauseCacheCount - 1 + if pauseCacheCount == 0 then + for source in pairs(originCacheKeys) do + nodeMgr.nodeCache[source] = originCacheValues[source] + originCacheKeys[source] = nil + originCacheValues[source] = nil + end + end +end + ---@param source parser.object ---@return vm.node function m.compileNode(source) @@ -1422,6 +1428,13 @@ function m.compileNode(source) if nodeMgr.nodeCache[source] ~= nil then return nodeMgr.nodeCache[source] end + + local pauseCache = pauseCacheCount > 0 + if pauseCache and not originCacheKeys[source] then + originCacheKeys[source] = true + originCacheValues[source] = nodeMgr.nodeCache[source] + end + nodeMgr.nodeCache[source] = false compileByGlobal(source) compileByNode(source) -- cgit v1.2.3