Skip to content

Commit 1c79bbc

Browse files
committed
test multi core compile
1 parent 310fb60 commit 1c79bbc

File tree

5 files changed

+50
-11
lines changed

5 files changed

+50
-11
lines changed

script/brave/work.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,8 @@ brave.on('compile', function (param)
5858
, param.version
5959
, param.options
6060
)
61-
return state, err
61+
return {
62+
state = state,
63+
err = err,
64+
}
6265
end)

script/files.lua

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ local scope = require 'workspace.scope'
1616
local lazy = require 'lazytable'
1717
local cacher = require 'lazy-cacher'
1818
local sp = require 'bee.subprocess'
19+
local pub = require 'pub'
1920

2021
---@class file
2122
---@field uri uri
@@ -554,10 +555,13 @@ function m.compileStateThen(state, file)
554555
end
555556

556557
---@param uri uri
557-
---@param file file
558558
---@param async boolean?
559559
---@return parser.state?
560-
function m.compileState(uri, file, async)
560+
function m.compileState(uri, async)
561+
local file = m.fileMap[uri]
562+
if not file then
563+
return
564+
end
561565
if file.state then
562566
return file.state
563567
end
@@ -586,17 +590,41 @@ function m.compileState(uri, file, async)
586590
end
587591
return nil
588592
end
593+
594+
---@type brave.param.compile.options
595+
local options = {
596+
special = config.get(uri, 'Lua.runtime.special'),
597+
unicodeName = config.get(uri, 'Lua.runtime.unicodeName'),
598+
nonstandardSymbol = util.arrayToHash(config.get(uri, 'Lua.runtime.nonstandardSymbol')),
599+
}
600+
601+
if async then
602+
---@type brave.param.compile
603+
local params = {
604+
text = file.text,
605+
mode = 'Lua',
606+
version = config.get(uri, 'Lua.runtime.version'),
607+
options = options
608+
}
609+
pub.task('compile', params, function (result)
610+
if file.text ~= params.text then
611+
return
612+
end
613+
if not result.state then
614+
log.error('Compile failed:', uri, result.err)
615+
return
616+
end
617+
m.compileStateThen(result.state, file)
618+
end)
619+
end
620+
589621
local prog <close> = progress.create(uri, lang.script.WINDOW_COMPILING, 0.5)
590622
prog:setMessage(ws.getRelativePath(uri))
591623
local clock = os.clock()
592624
local state, err = parser.compile(file.text
593625
, 'Lua'
594626
, config.get(uri, 'Lua.runtime.version')
595-
, {
596-
special = config.get(uri, 'Lua.runtime.special'),
597-
unicodeName = config.get(uri, 'Lua.runtime.unicodeName'),
598-
nonstandardSymbol = util.arrayToHash(config.get(uri, 'Lua.runtime.nonstandardSymbol')),
599-
}
627+
, options
600628
)
601629
local passed = os.clock() - clock
602630
if passed > 0.1 then
@@ -626,7 +654,7 @@ function m.getState(uri)
626654
if not file then
627655
return nil
628656
end
629-
local state = m.compileState(uri, file)
657+
local state = m.compileState(uri)
630658
file.cacheActiveTime = timer.clock()
631659
return state
632660
end

script/global.d.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,7 @@ LAZY = false
6060
-- (experiment) Improve performance, but reduce accuracy
6161
---@type boolean
6262
CACHEALIVE = false
63+
64+
-- (experiment) Compile files in multi cpu cores
65+
---@type integer
66+
COMPILECORES = 0

script/service/service.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ end
260260
function m.start()
261261
util.enableCloseFunction()
262262
await.setErrorHandle(log.error)
263-
pub.recruitBraves(8)
263+
pub.recruitBraves(4 + (COMPILECORES or 0))
264264
proto.listen()
265265
m.report()
266266
m.testVersion()

script/workspace/loading.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ function mt:loadFile(uri, libraryUri)
9999
-- self._sets[#self._sets+1] = waker
100100
--end)
101101
files.setText(uri, content, false)
102-
files.getState(uri)
102+
if COMPILECORES then
103+
files.compileState(uri, true)
104+
else
105+
files.compileState(uri)
106+
end
103107
if not self._cache[uri] then
104108
files.addRef(uri)
105109
end

0 commit comments

Comments
 (0)