Skip to content

Commit fea3b6d

Browse files
committed
perf: Optimize work distribution in multi-threaded --check
The current hash function used to distribute work seems not perfect. We can actually use round robin to distribute after sorting file list.
1 parent 3886d2e commit fea3b6d

File tree

1 file changed

+2
-11
lines changed

1 file changed

+2
-11
lines changed

script/cli/check_worker.lua

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,6 @@ local checkLevel = define.DiagnosticSeverity[CHECKLEVEL] or define.DiagnosticSev
4343

4444
util.enableCloseFunction()
4545

46-
-- Hash function used to distribute work.
47-
local function hashString(str)
48-
local hash = 0
49-
for i = 1, #str do
50-
hash = (hash * 37 & 0xFFFFFFFF) + str:byte(i, i)
51-
end
52-
return hash
53-
end
54-
5546
local lastClock = os.clock()
5647
local results = {}
5748

@@ -109,9 +100,9 @@ xpcall(lclient.start, errorhandler, lclient, function (client)
109100

110101
local uris = files.getChildFiles(rootUri)
111102
local max = #uris
103+
table.sort(uris) -- sort file list to ensure the work distribution order across multiple threads
112104
for i, uri in ipairs(uris) do
113-
local hash = hashString(uri) % numThreads + 1
114-
if hash == threadId then
105+
if (i % numThreads + 1) == threadId then
115106
files.open(uri)
116107
diag.doDiagnostic(uri, true)
117108
-- Print regularly but always print the last entry to ensure that logs written to files don't look incomplete.

0 commit comments

Comments
 (0)