-
Notifications
You must be signed in to change notification settings - Fork 16
Add startup benchmark helper #158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
camillobruni
merged 8 commits into
WebKit:main
from
camillobruni:2025-08-20_startup_helpers
Aug 26, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bae2939
adding tedsts
camillobruni 872bfb0
adding code
camillobruni 64f97bd
adding no-reuse test
camillobruni 74483ff
adding more tests
camillobruni d943dcb
Merge branch 'main' into 2025-08-20_startup_helpers
camillobruni bc5fa33
update code and add more tests
camillobruni 677c139
more cleanup
camillobruni 38011f2
format document
camillobruni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Babel plugin that adds CACHE_BUST_COMMENT to every function body. | ||
const CACHE_BUST_COMMENT = "ThouShaltNotCache"; | ||
|
||
module.exports = function ({ types: t }) { | ||
return { | ||
visitor: { | ||
Function(path) { | ||
const bodyPath = path.get("body"); | ||
// Handle arrow functions: () => "value" | ||
// Convert them to block statements: () => { return "value"; } | ||
if (!bodyPath.isBlockStatement()) { | ||
const newBody = t.blockStatement([t.returnStatement(bodyPath.node)]); | ||
path.set("body", newBody); | ||
} | ||
|
||
// Handle empty function bodies: function foo() {} | ||
// Add an empty statement so we have a first node to attach the comment to. | ||
if (path.get("body.body").length === 0) { | ||
path.get("body").pushContainer("body", t.emptyStatement()); | ||
} | ||
|
||
const firstNode = path.node.body.body[0]; | ||
t.addComment(firstNode, "leading", CACHE_BUST_COMMENT); | ||
}, | ||
}, | ||
}; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
const CACHE_BUST_COMMENT = "/*ThouShaltNotCache*/"; | ||
const CACHE_BUST_COMMENT_RE = new RegExp( | ||
`\n${RegExp.escape(CACHE_BUST_COMMENT)}\n`, | ||
"g" | ||
); | ||
|
||
class StartupBenchmark { | ||
// Total iterations for this benchmark. | ||
#iterationCount = 0; | ||
// Original source code. | ||
#sourceCode; | ||
// quickHahs(this.#sourceCode) for use in custom validate() methods. | ||
#sourceHash = 0; | ||
// Number of no-cache comments in the original #sourceCode. | ||
#expectedCacheCommentCount = 0; | ||
// How many times (separate iterations) should we reuse the source code. | ||
// Use 0 to skip and only use a single #sourceCode string. | ||
#sourceCodeReuseCount = 1; | ||
// #sourceCode for each iteration, number of unique sources is controlled | ||
// by codeReuseCount; | ||
#iterationSourceCodes = []; | ||
|
||
constructor({ | ||
iterationCount, | ||
expectedCacheCommentCount, | ||
sourceCodeReuseCount = 1, | ||
} = {}) { | ||
console.assert(iterationCount > 0); | ||
this.#iterationCount = iterationCount; | ||
console.assert(expectedCacheCommentCount > 0); | ||
this.#expectedCacheCommentCount = expectedCacheCommentCount; | ||
console.assert(sourceCodeReuseCount >= 0); | ||
this.#sourceCodeReuseCount = sourceCodeReuseCount; | ||
} | ||
|
||
get iterationCount() { | ||
return this.#iterationCount; | ||
} | ||
|
||
get sourceCode() { | ||
return this.#sourceCode; | ||
} | ||
|
||
get sourceHash() { | ||
return this.#sourceHash; | ||
} | ||
|
||
get expectedCacheCommentCount() { | ||
return this.#expectedCacheCommentCount; | ||
} | ||
|
||
get sourceCodeReuseCount() { | ||
return this.#sourceCodeReuseCount; | ||
} | ||
|
||
get iterationSourceCodes() { | ||
return this.#iterationSourceCodes; | ||
} | ||
|
||
async init() { | ||
this.#sourceCode = await JetStream.getString(JetStream.preload.BUNDLE); | ||
const cacheCommentCount = this.sourceCode.match( | ||
CACHE_BUST_COMMENT_RE | ||
).length; | ||
this.#sourceHash = this.quickHash(this.sourceCode); | ||
this.validateSourceCacheComments(cacheCommentCount); | ||
for (let i = 0; i < this.iterationCount; i++) | ||
this.#iterationSourceCodes[i] = this.createIterationSourceCode(i); | ||
this.validateIterationSourceCodes(); | ||
} | ||
|
||
validateSourceCacheComments(cacheCommentCount) { | ||
console.assert( | ||
cacheCommentCount === this.expectedCacheCommentCount, | ||
`Invalid cache comment count ${cacheCommentCount} expected ${this.expectedCacheCommentCount}.` | ||
); | ||
} | ||
|
||
validateIterationSourceCodes() { | ||
if (this.#iterationSourceCodes.some((each) => !each?.length)) | ||
throw new Error(`Got invalid iterationSourceCodes`); | ||
let expectedSize = 1; | ||
if (this.sourceCodeReuseCount !== 0) | ||
expectedSize = Math.ceil(this.iterationCount / this.sourceCodeReuseCount); | ||
const uniqueSources = new Set(this.iterationSourceCodes); | ||
if (uniqueSources.size != expectedSize) | ||
throw new Error( | ||
`Expected ${expectedSize} unique sources, but got ${uniqueSources.size}.` | ||
); | ||
} | ||
|
||
createIterationSourceCode(iteration) { | ||
// Alter the code per iteration to prevent caching. | ||
const cacheId = | ||
Math.floor(iteration / this.sourceCodeReuseCount) * | ||
this.sourceCodeReuseCount; | ||
// Reuse existing sources if this.codeReuseCount > 1: | ||
if (cacheId < this.iterationSourceCodes.length) | ||
return this.iterationSourceCodes[cacheId]; | ||
|
||
const sourceCode = this.sourceCode.replaceAll( | ||
CACHE_BUST_COMMENT_RE, | ||
`/*${cacheId}*/` | ||
); | ||
// Warm up quickHash. | ||
this.quickHash(sourceCode); | ||
return sourceCode; | ||
} | ||
|
||
quickHash(str) { | ||
let hash = 5381; | ||
let i = str.length; | ||
while (i > 0) { | ||
hash = (hash * 33) ^ (str.charCodeAt(i) | 0); | ||
i -= 919; | ||
} | ||
return hash | 0; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.