mirror of
https://github.com/oven-sh/bun
synced 2026-02-02 15:08:46 +00:00
Optimize WaitGroup implementation (#21260)
`add` no longer locks a mutex, and `finish` no longer locks a mutex except for the last task. This could meaningfully improve performance in cases where we spawn a large number of tasks on a thread pool. This change doesn't alter the semantics of the type, unlike the standard library's `WaitGroup`, which also uses atomics but has to be explicitly reset. (For internal tracking: fixes ENG-19722) --------- Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
This commit is contained in:
@@ -102,6 +102,10 @@ function parseDeclarations(
|
||||
continue;
|
||||
}
|
||||
|
||||
if (declarations.has(name)) {
|
||||
unusedLineIndices.push(i);
|
||||
continue;
|
||||
}
|
||||
declarations.set(name, {
|
||||
whole: line,
|
||||
index: i,
|
||||
@@ -341,20 +345,33 @@ async function processFile(filePath: string): Promise<void> {
|
||||
}
|
||||
}
|
||||
if (thisDeclaration) {
|
||||
sortedLines[thisDeclaration.index] = DELETED_LINE;
|
||||
}
|
||||
if (thisDeclaration) {
|
||||
let firstNonFileCommentLine = 0;
|
||||
for (const line of sortedLines) {
|
||||
if (line.startsWith("//!")) {
|
||||
firstNonFileCommentLine++;
|
||||
} else {
|
||||
var onlyCommentsBeforeThis = true;
|
||||
for (const [i, line] of sortedLines.entries()) {
|
||||
if (i >= thisDeclaration.index) {
|
||||
break;
|
||||
}
|
||||
if (line === "" || line === DELETED_LINE) {
|
||||
continue;
|
||||
}
|
||||
if (!line.startsWith("//")) {
|
||||
onlyCommentsBeforeThis = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
const insert = [thisDeclaration.whole, ""];
|
||||
if (firstNonFileCommentLine > 0) insert.unshift("");
|
||||
sortedLines.splice(firstNonFileCommentLine, 0, ...insert);
|
||||
if (!onlyCommentsBeforeThis) {
|
||||
sortedLines[thisDeclaration.index] = DELETED_LINE;
|
||||
let firstNonFileCommentLine = 0;
|
||||
for (const line of sortedLines) {
|
||||
if (line.startsWith("//!")) {
|
||||
firstNonFileCommentLine++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
const insert = [thisDeclaration.whole, ""];
|
||||
if (firstNonFileCommentLine > 0) insert.unshift("");
|
||||
sortedLines.splice(firstNonFileCommentLine, 0, ...insert);
|
||||
}
|
||||
}
|
||||
fileContents = sortedLines.join("\n");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user