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:
taylor.fish
2025-07-25 18:12:12 -07:00
committed by GitHub
parent 2f0ddf3018
commit 60823348c5
2 changed files with 50 additions and 28 deletions

View File

@@ -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");
}