-
Notifications
You must be signed in to change notification settings - Fork 843
Cost analysis: Remove "Unacceptable" hack #6782
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
Merged
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d7cf929
yolo
kripken 26a8171
work
kripken b2f2340
work
kripken 4cb66c4
work
kripken a68bf16
more
kripken 2080449
work
kripken d555059
work
kripken 05fc595
work
kripken e557efe
undo
kripken 58eca12
test
kripken 89cbb81
test
kripken 252f45b
test
kripken 5b66de3
typo
kripken de4a9fa
Add comments + asserts
kripken f1506e5
make conditions match the code below
kripken 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,148 @@ | ||
|
|
||
| // Benchmarking script. This runs on compiled bench.wat and prints out timings. | ||
| // | ||
| // Usage: | ||
| // | ||
| // * wasm-opt scripts/benchmarking/bench.wat -all --inline-functions-with-loops --always-inline-max-function-size=1000 --inlining --precompute-propagate --optimize-instructions --inlining --simplify-locals --coalesce-locals --vacuum --remove-unused-module-elements -o bench.wasm -g | ||
| // * Inspect the optimized wasm to see that inlining etc. worked properly | ||
| // (we rely on inlining to let us write bench.wat in a short/simple form, and | ||
| // we use very specific optimizations in order to not optimize away the | ||
| // differences we care about). | ||
| // * d8 bench.js -- bench.wasm | ||
| // etc. | ||
| // | ||
|
|
||
| // Shell integration. | ||
| if (typeof console === 'undefined') { | ||
| console = { log: print }; | ||
| } | ||
| var tempRet0; | ||
| var binary; | ||
| if (typeof process === 'object' && typeof require === 'function' /* node.js detection */) { | ||
| var args = process.argv.slice(2); | ||
| binary = require('fs').readFileSync(args[0]); | ||
| if (!binary.buffer) binary = new Uint8Array(binary); | ||
| } else { | ||
| var args; | ||
| if (typeof scriptArgs != 'undefined') { | ||
| args = scriptArgs; | ||
| } else if (typeof arguments != 'undefined') { | ||
| args = arguments; | ||
| } | ||
| if (typeof readbuffer === 'function') { | ||
| binary = new Uint8Array(readbuffer(args[0])); | ||
| } else { | ||
| binary = read(args[0], 'binary'); | ||
| } | ||
| } | ||
|
|
||
| // Create the wasm. | ||
| const module = new WebAssembly.Module(binary); | ||
| const instance = new WebAssembly.Instance(module, {}); | ||
| const exports = instance.exports; | ||
|
|
||
| // Create the benchmarkers. | ||
| function makeBenchmarker(name) { | ||
| return { | ||
| name: name, | ||
| func: exports[name], | ||
| time: 0, | ||
| sum: 0, | ||
| iters: 0, | ||
| }; | ||
| } | ||
|
|
||
| const benchmarkers = [ | ||
| makeBenchmarker('len'), | ||
| makeBenchmarker('and'), | ||
| makeBenchmarker('iff-both'), | ||
| makeBenchmarker('or'), | ||
| makeBenchmarker('iff-either'), | ||
| makeBenchmarker('select'), | ||
| makeBenchmarker('iff-nextor'), | ||
| makeBenchmarker('select-three'), | ||
| makeBenchmarker('iff-three'), | ||
| ]; | ||
|
|
||
| // We'll call the benchmark functions in random orders. | ||
| function makeOrders(prefix) { | ||
| // Given a prefix of an order, like [] or [0, 3], return all the possible | ||
| // orders beginning with that prefix. | ||
|
|
||
| // We cannot repeat anything already seen. | ||
| const seen = new Set(); | ||
| for (var x of prefix) { | ||
| seen.add(x); | ||
| } | ||
|
|
||
| // Starting from the prefix, extend it by one item in all valid ways. | ||
| const extensions = []; | ||
| for (var i = 0; i < benchmarkers.length; i++) { | ||
| if (!seen.has(i)) { | ||
| extensions.push(prefix.concat(i)); | ||
| } | ||
| } | ||
|
|
||
| if (prefix.length == benchmarkers.length - 1) { | ||
| // The extensions are complete orders; stop the recursion. | ||
| return extensions; | ||
| } | ||
|
|
||
| // Recursively generate the full orders. | ||
| const ret = []; | ||
| for (var extension of extensions) { | ||
| for (var order of makeOrders(extension)) { | ||
| ret.push(order); | ||
| } | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| const orders = makeOrders([]); | ||
|
|
||
| // Params. | ||
| const M = 10000000; | ||
| const N = 100; | ||
|
|
||
| console.log('iters :', M); | ||
| console.log('list len :', N); | ||
| console.log('benchmarkers:', benchmarkers.length); | ||
| console.log('orderings :', orders.length); | ||
|
|
||
| // Create a long linked list of objects of both type $A and $B. | ||
| var list = null; | ||
| for (var i = 0; i < N; i++) { | ||
| list = Math.random() < 0.5 ? exports.makeA(list) : exports.makeB(list); | ||
| } | ||
|
|
||
| console.log('benchmarking...'); | ||
|
|
||
| // Call the benchmark functions. | ||
|
|
||
| for (var i = 0; i < M; i++) { | ||
| const order = orders[Math.floor(Math.random() * orders.length)]; | ||
| for (var k = 0; k < benchmarkers.length; k++) { | ||
| const benchmarker = benchmarkers[order[k]]; | ||
| const start = performance.now(); | ||
| const result = benchmarker.func(list); | ||
| benchmarker.time += performance.now() - start; | ||
| benchmarker.sum += result; | ||
| benchmarker.iters++; | ||
| } | ||
| } | ||
|
|
||
| for (var benchmarker of benchmarkers) { | ||
| if (benchmarker.iters != M) { | ||
| throw 'wat'; | ||
| } | ||
| } | ||
|
|
||
| console.log(); | ||
| for (var benchmarker of benchmarkers) { | ||
| console.log(`${benchmarker.name} time: \t${benchmarker.time}`) | ||
| } | ||
| console.log(); | ||
| for (var benchmarker of benchmarkers) { | ||
| console.log(`${benchmarker.name} mean sum: \t${benchmarker.sum / M}`) | ||
| } | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it standard benchmarking practice to run the benchmarks in random orders? Is this meant to defeat unwanted optimizations? Generating every possible order up front is not going to scale to a larger number of benchmarks. Is there something simpler and more scalable we can do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are other ways to deal with benchmark interactions, like running all the tests for A first, then all the tests for B, etc., rather than interleaving. But interleaving actually makes it more realistic since real-world code is mixed in with other stuff, and it's simple enough to handle here, so it seems appropriate to me.
Mainly to avoid order being an issue. Imagine that running A, B, C happens to have A warm up the cache for B, or B reset the branch predictor for C. Random orders avoid that.
Yeah, past some point it can't work, but so long as we don't hit that limit it is faster to do it this way. The other way would be to generate an unbiased random order on the fly each time, which is not hard, but just takes more work.