-
Notifications
You must be signed in to change notification settings - Fork 226
Start working on a benchmark for the spread desugaring. #210
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 all commits
Commits
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
1 change: 1 addition & 0 deletions
1
accepted/future-releases/spread-collections/benchmarks/.gitignore
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 @@ | ||
/out |
34 changes: 34 additions & 0 deletions
34
accepted/future-releases/spread-collections/benchmarks/Makefile
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,34 @@ | ||
# Runs the benchmarks on a few platforms. | ||
|
||
# Note: this assumes: | ||
# | ||
# - The language repo's root directory is adjacent to the root directory of the | ||
# Dart SDK repo, which is named "dart". | ||
# | ||
# - "node" is on your PATH. | ||
# | ||
# - You have built the AoT runtime by running this from the SDK root directory: | ||
# | ||
# tools/build.py -m release runtime dart_precompiled_runtime | ||
|
||
SDK := "../../../../../dart/sdk" | ||
|
||
all: jit dart2js aot | ||
|
||
aot: | ||
@ echo "AoT" | ||
@ mkdir -p out | ||
@ $(SDK)/pkg/vm/tool/precompiler2 bin/profile_list_spread.dart out/profile_list_spread.snapshot | ||
@ $(SDK)/pkg/vm/tool/dart_precompiled_runtime2 out/profile_list_spread.snapshot | ||
|
||
dart2js: | ||
@ echo "dart2js" | ||
@ mkdir -p out | ||
@ $(SDK)/sdk/bin/dart2js bin/profile_list_spread.dart -o out/profile_list_spread.js | ||
@ node out/profile_list_spread.js | ||
|
||
jit: | ||
@ echo "VM JIT" | ||
@ $(SDK)/sdk/bin/dart bin/profile_list_spread.dart | ||
|
||
.PHONY: all aot dart2js jit |
135 changes: 135 additions & 0 deletions
135
accepted/future-releases/spread-collections/benchmarks/bin/profile_list_spread.dart
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,135 @@ | ||
import 'dart:collection'; | ||
|
||
const trialMs = 100; | ||
const lengths = [0, 1, 2, 5, 10, 20, 50, 100, 1000]; | ||
|
||
var csv = StringBuffer(); | ||
|
||
class CustomList<T> extends ListBase<T> { | ||
final List<T> _inner; | ||
|
||
int get length => _inner.length; | ||
|
||
set length(int value) => _inner.length = value; | ||
|
||
CustomList(this._inner); | ||
|
||
T operator [](int index) => _inner[index]; | ||
|
||
void operator []=(int index, T value) => _inner[index] = value; | ||
} | ||
|
||
void main() { | ||
for (var length in lengths) { | ||
var baseline = runBench("iterate", length, iterate); | ||
runBench("List for", length, addList, baseline); | ||
runBench("resize and set", length, resizeAndSet, baseline); | ||
runBench("addAll()", length, addAll, baseline); | ||
runBench("forEach()", length, forEach, baseline); | ||
print(""); | ||
} | ||
|
||
// print(""); | ||
// print(csv); | ||
} | ||
|
||
double runBench( | ||
String name, int length, void Function(List<String>, List<String>) action, | ||
[double baseline]) { | ||
var from = <String>[]; | ||
for (var i = 0; i < length; i++) { | ||
from.add(String.fromCharCode(i % 26 + 65)); | ||
} | ||
|
||
var froms = [from, CustomList(from)]; | ||
|
||
var rate = benchBest(froms, action); | ||
|
||
if (baseline == null) { | ||
print("${length.toString().padLeft(4)} ${name.padRight(15)} " | ||
"${rate.toStringAsFixed(2).padLeft(10)} spreads/ms " | ||
" ${'-' * 20}"); | ||
} else { | ||
var comparison = rate / baseline; | ||
var bar = "=" * (comparison * 20).toInt(); | ||
if (comparison > 4.0) bar = "!!!"; | ||
print("${length.toString().padLeft(4)} ${name.padRight(15)} " | ||
"${rate.toStringAsFixed(2).padLeft(10)} spreads/ms " | ||
"${comparison.toStringAsFixed(2).padLeft(6)}x baseline $bar"); | ||
} | ||
|
||
csv.writeln("$length,$name,$rate"); | ||
return rate; | ||
} | ||
|
||
/// Runs [bench] a number of times and returns the best (highest) result. | ||
double benchBest( | ||
List<List<String>> froms, void Function(List<String>, List<String>) action) { | ||
var best = 0.0; | ||
for (var i = 0; i < 4; i++) { | ||
var result = bench(froms, action); | ||
if (result > best) best = result; | ||
} | ||
|
||
return best; | ||
} | ||
|
||
/// Spreads each list in [froms] into the middle of a list using [action]. | ||
/// | ||
/// Returns the number of times it was able to do this per millisecond, on | ||
/// average. Higher is better. | ||
double bench(List<List<String>> froms, | ||
void Function(List<String>, List<String>) action) { | ||
var elapsed = 0; | ||
var count = 0; | ||
var watch = Stopwatch()..start(); | ||
do { | ||
for (var i = 0; i < froms.length; i++) { | ||
var from = froms[i]; | ||
var to = <String>["a", "b"]; | ||
|
||
action(from, to); | ||
to.add("b"); | ||
to.add("c"); | ||
|
||
count++; | ||
} | ||
elapsed = watch.elapsedMilliseconds; | ||
} while (elapsed < trialMs); | ||
|
||
return count / elapsed; | ||
} | ||
|
||
void iterate(List<String> from, List<String> to) { | ||
for (var e in from) { | ||
to.add(e); | ||
} | ||
} | ||
|
||
void addList(List<String> from, List<String> to) { | ||
var length = from.length; | ||
for (var i = 0; i < length; i++) { | ||
to.add(from[i]); | ||
} | ||
} | ||
|
||
void resizeAndSet(List<String> from, List<String> to) { | ||
var length = from.length; | ||
var j = to.length; | ||
to.length = to.length + length; | ||
for (var i = 0; i < length; i++) { | ||
to[j] = from[i]; | ||
} | ||
} | ||
|
||
void addAll(List<String> from, List<String> to) { | ||
to.addAll(from); | ||
} | ||
|
||
void forEach(List<String> from, List<String> to) { | ||
var temp = to; | ||
from.forEach((s) { | ||
temp.add(s); | ||
}); | ||
temp = null; | ||
} |
1 change: 1 addition & 0 deletions
1
accepted/future-releases/spread-collections/benchmarks/pubspec.yaml
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 @@ | ||
name: benchmarks |
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.