diff --git a/benchmarks/ForEachLoop/dart/ForEachLoop.dart b/benchmarks/ForEachLoop/dart/ForEachLoop.dart new file mode 100644 index 00000000000..de0fd9d7f1f --- /dev/null +++ b/benchmarks/ForEachLoop/dart/ForEachLoop.dart @@ -0,0 +1,25 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:benchmark_harness/benchmark_harness.dart'; + +class IterationBenchmark extends BenchmarkBase { + List list = List.generate(1000, (i) => i); + var r = 0; + void fn(int i) => r = 123 * i; + IterationBenchmark(name) : super(name); +} + +class ForEach extends IterationBenchmark { + ForEach() : super('ForEachLoop'); + + @override + void run() { + list.forEach(fn); + } +} + +void main() { + ForEach().report(); +} diff --git a/benchmarks/ForEachLoop/dart2/ForEachLoop.dart b/benchmarks/ForEachLoop/dart2/ForEachLoop.dart new file mode 100644 index 00000000000..222e1a96c9d --- /dev/null +++ b/benchmarks/ForEachLoop/dart2/ForEachLoop.dart @@ -0,0 +1,27 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// @dart=2.9 + +import 'package:benchmark_harness/benchmark_harness.dart'; + +class IterationBenchmark extends BenchmarkBase { + List list = List.generate(1000, (i) => i); + var r = 0; + void fn(int i) => r = 123 * i; + IterationBenchmark(name) : super(name); +} + +class ForEach extends IterationBenchmark { + ForEach() : super('ForEachLoop'); + + @override + void run() { + list.forEach(fn); + } +} + +void main() { + ForEach().report(); +} diff --git a/benchmarks/ForInGeneratedLoop/dart/ForInGeneratedLoop.dart b/benchmarks/ForInGeneratedLoop/dart/ForInGeneratedLoop.dart new file mode 100644 index 00000000000..46656050a30 --- /dev/null +++ b/benchmarks/ForInGeneratedLoop/dart/ForInGeneratedLoop.dart @@ -0,0 +1,33 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:benchmark_harness/benchmark_harness.dart'; + +class IterationBenchmark extends BenchmarkBase { + List list = List.generate(1000, (i) => i); + var r = 0; + void fn(int i) => r = 123 * i; + IterationBenchmark(name) : super(name); +} + +Iterable generateElements(List list) sync* { + for (var i = 0; i < list.length; i++) { + yield list[i]; + } +} + +class ForInGenerated extends IterationBenchmark { + ForInGenerated() : super('ForInGeneratedLoop'); + + @override + void run() { + for (var item in generateElements(list)) { + fn(item); + } + } +} + +void main() { + ForInGenerated().report(); +} diff --git a/benchmarks/ForInGeneratedLoop/dart2/ForInGeneratedLoop.dart b/benchmarks/ForInGeneratedLoop/dart2/ForInGeneratedLoop.dart new file mode 100644 index 00000000000..acf89b5bf4e --- /dev/null +++ b/benchmarks/ForInGeneratedLoop/dart2/ForInGeneratedLoop.dart @@ -0,0 +1,35 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// @dart=2.9 + +import 'package:benchmark_harness/benchmark_harness.dart'; + +class IterationBenchmark extends BenchmarkBase { + List list = List.generate(1000, (i) => i); + var r = 0; + void fn(int i) => r = 123 * i; + IterationBenchmark(name) : super(name); +} + +Iterable generateElements(List list) sync* { + for (var i = 0; i < list.length; i++) { + yield list[i]; + } +} + +class ForInGenerated extends IterationBenchmark { + ForInGenerated() : super('ForInGeneratedLoop'); + + @override + void run() { + for (var item in generateElements(list)) { + fn(item); + } + } +} + +void main() { + ForInGenerated().report(); +} diff --git a/benchmarks/ForInLoop/dart/ForInLoop.dart b/benchmarks/ForInLoop/dart/ForInLoop.dart new file mode 100644 index 00000000000..09a5f6c3f9e --- /dev/null +++ b/benchmarks/ForInLoop/dart/ForInLoop.dart @@ -0,0 +1,27 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:benchmark_harness/benchmark_harness.dart'; + +class IterationBenchmark extends BenchmarkBase { + List list = List.generate(1000, (i) => i); + var r = 0; + void fn(int i) => r = 123 * i; + IterationBenchmark(name) : super(name); +} + +class ForIn extends IterationBenchmark { + ForIn() : super('ForInLoop'); + + @override + void run() { + for (var item in list) { + fn(item); + } + } +} + +void main() { + ForIn().report(); +} diff --git a/benchmarks/ForInLoop/dart2/ForInLoop.dart b/benchmarks/ForInLoop/dart2/ForInLoop.dart new file mode 100644 index 00000000000..857691fa3cc --- /dev/null +++ b/benchmarks/ForInLoop/dart2/ForInLoop.dart @@ -0,0 +1,29 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// @dart=2.9 + +import 'package:benchmark_harness/benchmark_harness.dart'; + +class IterationBenchmark extends BenchmarkBase { + List list = List.generate(1000, (i) => i); + var r = 0; + void fn(int i) => r = 123 * i; + IterationBenchmark(name) : super(name); +} + +class ForIn extends IterationBenchmark { + ForIn() : super('ForInLoop'); + + @override + void run() { + for (var item in list) { + fn(item); + } + } +} + +void main() { + ForIn().report(); +} diff --git a/benchmarks/ForLoop/dart/ForLoop.dart b/benchmarks/ForLoop/dart/ForLoop.dart new file mode 100644 index 00000000000..da2b9977268 --- /dev/null +++ b/benchmarks/ForLoop/dart/ForLoop.dart @@ -0,0 +1,27 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:benchmark_harness/benchmark_harness.dart'; + +class IterationBenchmark extends BenchmarkBase { + List list = List.generate(1000, (i) => i); + var r = 0; + void fn(int i) => r = 123 * i; + IterationBenchmark(name) : super(name); +} + +class ForLoop extends IterationBenchmark { + ForLoop() : super('ForLoop'); + + @override + void run() { + for (var i = 0; i < list.length; i++) { + fn(list[i]); + } + } +} + +void main() { + ForLoop().report(); +} diff --git a/benchmarks/ForLoop/dart2/ForLoop.dart b/benchmarks/ForLoop/dart2/ForLoop.dart new file mode 100644 index 00000000000..cd9e9612f27 --- /dev/null +++ b/benchmarks/ForLoop/dart2/ForLoop.dart @@ -0,0 +1,29 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// @dart=2.9 + +import 'package:benchmark_harness/benchmark_harness.dart'; + +class IterationBenchmark extends BenchmarkBase { + List list = List.generate(1000, (i) => i); + var r = 0; + void fn(int i) => r = 123 * i; + IterationBenchmark(name) : super(name); +} + +class ForLoop extends IterationBenchmark { + ForLoop() : super('ForLoop'); + + @override + void run() { + for (var i = 0; i < list.length; i++) { + fn(list[i]); + } + } +} + +void main() { + ForLoop().report(); +} diff --git a/benchmarks/TypedData/dart/TypedData.dart b/benchmarks/TypedData/dart/TypedData.dart new file mode 100644 index 00000000000..8c98bad35e7 --- /dev/null +++ b/benchmarks/TypedData/dart/TypedData.dart @@ -0,0 +1,878 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. +// +// Micro-benchmarks for typed data setters and getters. +// + +import 'dart:typed_data'; +import 'package:benchmark_harness/benchmark_harness.dart'; + +// +// Typed constant setters. +// + +void doSetInt8(Int8List list) { + for (int i = 0; i < list.length; i++) { + list[i] = 1; + } +} + +void doSetUint8(Uint8List list) { + for (int i = 0; i < list.length; i++) { + list[i] = 1; + } +} + +void doSetUint8Clamped(Uint8ClampedList list) { + for (int i = 0; i < list.length; i++) { + list[i] = 1; + } +} + +void doSetInt16(Int16List list) { + for (int i = 0; i < list.length; i++) { + list[i] = 1; + } +} + +void doSetUint16(Uint16List list) { + for (int i = 0; i < list.length; i++) { + list[i] = 1; + } +} + +void doSetInt32(Int32List list) { + for (int i = 0; i < list.length; i++) { + list[i] = 1; + } +} + +void doSetUint32(Uint32List list) { + for (int i = 0; i < list.length; i++) { + list[i] = 1; + } +} + +void doSetInt64(Int64List list) { + for (int i = 0; i < list.length; i++) { + list[i] = 1; + } +} + +void doSetUint64(Uint64List list) { + for (int i = 0; i < list.length; i++) { + list[i] = 1; + } +} + +void doSetFloat32(Float32List list) { + for (int i = 0; i < list.length; i++) { + list[i] = 1.0; + } +} + +void doSetFloat64(Float64List list) { + for (int i = 0; i < list.length; i++) { + list[i] = 1.0; + } +} + +// +// Typed variable setters. +// + +void doSetInt8Var(Int8List list) { + for (int i = 0; i < list.length; i++) { + list[i] = i; + } +} + +void doSetUint8Var(Uint8List list) { + for (int i = 0; i < list.length; i++) { + list[i] = i; + } +} + +void doSetUint8ClampedVar(Uint8ClampedList list) { + for (int i = 0; i < list.length; i++) { + list[i] = i; + } +} + +void doSetInt16Var(Int16List list) { + for (int i = 0; i < list.length; i++) { + list[i] = i; + } +} + +void doSetUint16Var(Uint16List list) { + for (int i = 0; i < list.length; i++) { + list[i] = i; + } +} + +void doSetInt32Var(Int32List list) { + for (int i = 0; i < list.length; i++) { + list[i] = i; + } +} + +void doSetUint32Var(Uint32List list) { + for (int i = 0; i < list.length; i++) { + list[i] = i; + } +} + +void doSetInt64Var(Int64List list) { + for (int i = 0; i < list.length; i++) { + list[i] = i; + } +} + +void doSetUint64Var(Uint64List list) { + for (int i = 0; i < list.length; i++) { + list[i] = i; + } +} + +void doSetFloat32Var(Float32List list) { + double x = 0.0; + for (int i = 0; i < list.length; i++) { + list[i] = x++; + } +} + +void doSetFloat64Var(Float64List list) { + double x = 0.0; + for (int i = 0; i < list.length; i++) { + list[i] = x++; + } +} + +// +// Typed getters. +// + +int doGetInt8(Int8List list) { + int x = 0; + for (int i = 0; i < list.length; i++) { + x += list[i]; + } + return x; +} + +int doGetUint8(Uint8List list) { + int x = 0; + for (int i = 0; i < list.length; i++) { + x += list[i]; + } + return x; +} + +int doGetUint8Clamped(Uint8ClampedList list) { + int x = 0; + for (int i = 0; i < list.length; i++) { + x += list[i]; + } + return x; +} + +int doGetInt16(Int16List list) { + int x = 0; + for (int i = 0; i < list.length; i++) { + x += list[i]; + } + return x; +} + +int doGetUint16(Uint16List list) { + int x = 0; + for (int i = 0; i < list.length; i++) { + x += list[i]; + } + return x; +} + +int doGetInt32(Int32List list) { + int x = 0; + for (int i = 0; i < list.length; i++) { + x += list[i]; + } + return x; +} + +int doGetUint32(Uint32List list) { + int x = 0; + for (int i = 0; i < list.length; i++) { + x += list[i]; + } + return x; +} + +int doGetInt64(Int64List list) { + int x = 0; + for (int i = 0; i < list.length; i++) { + x += list[i]; + } + return x; +} + +int doGetUint64(Uint64List list) { + int x = 0; + for (int i = 0; i < list.length; i++) { + x += list[i]; + } + return x; +} + +double doGetFloat32(Float32List list) { + double x = 0.0; + for (int i = 0; i < list.length; i++) { + x += list[i]; + } + return x; +} + +double doGetFloat64(Float64List list) { + double x = 0.0; + for (int i = 0; i < list.length; i++) { + x += list[i]; + } + return x; +} + +// +// Benchmark fixtures. +// + +const N = 1000; + +class Int8ListBench extends BenchmarkBase { + var list = Int8List(N); + Int8ListBench() : super('TypedData.Int8ListBench'); + @override + void run() { + doSetInt8(list); + final int x = doGetInt8(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint8ListBench extends BenchmarkBase { + var list = Uint8List(N); + Uint8ListBench() : super('TypedData.Uint8ListBench'); + @override + void run() { + doSetUint8(list); + final int x = doGetUint8(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint8ClampedListBench extends BenchmarkBase { + var list = Uint8ClampedList(N); + Uint8ClampedListBench() : super('TypedData.Uint8ClampedListBench'); + @override + void run() { + doSetUint8Clamped(list); + final int x = doGetUint8Clamped(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int16ListBench extends BenchmarkBase { + var list = Int16List(N); + Int16ListBench() : super('TypedData.Int16ListBench'); + @override + void run() { + doSetInt16(list); + final int x = doGetInt16(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint16ListBench extends BenchmarkBase { + var list = Uint16List(N); + Uint16ListBench() : super('TypedData.Uint16ListBench'); + @override + void run() { + doSetUint16(list); + final int x = doGetUint16(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int32ListBench extends BenchmarkBase { + var list = Int32List(N); + Int32ListBench() : super('TypedData.Int32ListBench'); + @override + void run() { + doSetInt32(list); + final int x = doGetInt32(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint32ListBench extends BenchmarkBase { + var list = Uint32List(N); + Uint32ListBench() : super('TypedData.Uint32ListBench'); + @override + void run() { + doSetUint32(list); + final int x = doGetUint32(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int64ListBench extends BenchmarkBase { + var list = Int64List(N); + Int64ListBench() : super('TypedData.Int64ListBench'); + @override + void run() { + doSetInt64(list); + final int x = doGetInt64(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint64ListBench extends BenchmarkBase { + var list = Uint64List(N); + Uint64ListBench() : super('TypedData.Uint64ListBench'); + @override + void run() { + doSetUint64(list); + final int x = doGetUint64(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Float32ListBench extends BenchmarkBase { + var list = Float32List(N); + Float32ListBench() : super('TypedData.Float32ListBench'); + @override + void run() { + doSetFloat32(list); + final double x = doGetFloat32(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Float64ListBench extends BenchmarkBase { + var list = Float64List(N); + Float64ListBench() : super('TypedData.Float64ListBench'); + @override + void run() { + doSetFloat64(list); + final double x = doGetFloat64(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int8ListViewBench extends BenchmarkBase { + var list = Int8List.view(Int8List(N).buffer); + Int8ListViewBench() : super('TypedData.Int8ListViewBench'); + @override + void run() { + doSetInt8(list); + final int x = doGetInt8(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint8ListViewBench extends BenchmarkBase { + var list = Uint8List.view(Uint8List(N).buffer); + Uint8ListViewBench() : super('TypedData.Uint8ListViewBench'); + @override + void run() { + doSetUint8(list); + final int x = doGetUint8(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint8ClampedListViewBench extends BenchmarkBase { + var list = Uint8ClampedList.view(Uint8ClampedList(N).buffer); + Uint8ClampedListViewBench() : super('TypedData.Uint8ClampedListViewBench'); + @override + void run() { + doSetUint8Clamped(list); + final int x = doGetUint8Clamped(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int16ListViewBench extends BenchmarkBase { + var list = Int16List.view(Int16List(N).buffer); + Int16ListViewBench() : super('TypedData.Int16ListViewBench'); + @override + void run() { + doSetInt16(list); + final int x = doGetInt16(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint16ListViewBench extends BenchmarkBase { + var list = Uint16List.view(Uint16List(N).buffer); + Uint16ListViewBench() : super('TypedData.Uint16ListViewBench'); + @override + void run() { + doSetUint16(list); + final int x = doGetUint16(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int32ListViewBench extends BenchmarkBase { + var list = Int32List.view(Int32List(N).buffer); + Int32ListViewBench() : super('TypedData.Int32ListViewBench'); + @override + void run() { + doSetInt32(list); + final int x = doGetInt32(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint32ListViewBench extends BenchmarkBase { + var list = Uint32List.view(Uint32List(N).buffer); + Uint32ListViewBench() : super('TypedData.Uint32ListViewBench'); + @override + void run() { + doSetUint32(list); + final int x = doGetUint32(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int64ListViewBench extends BenchmarkBase { + var list = Int64List.view(Int64List(N).buffer); + Int64ListViewBench() : super('TypedData.Int64ListViewBench'); + @override + void run() { + doSetInt64(list); + final int x = doGetInt64(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint64ListViewBench extends BenchmarkBase { + var list = Uint64List.view(Uint64List(N).buffer); + Uint64ListViewBench() : super('TypedData.Uint64ListViewBench'); + @override + void run() { + doSetUint64(list); + final int x = doGetUint64(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Float32ListViewBench extends BenchmarkBase { + var list = Float32List.view(Float32List(N).buffer); + Float32ListViewBench() : super('TypedData.Float32ListViewBench'); + @override + void run() { + doSetFloat32(list); + final double x = doGetFloat32(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Float64ListViewBench extends BenchmarkBase { + var list = Float64List.view(Float64List(N).buffer); + Float64ListViewBench() : super('TypedData.Float64ListViewBench'); + @override + void run() { + doSetFloat64(list); + final double x = doGetFloat64(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int8ListVarBench extends BenchmarkBase { + var list = Int8List(N); + Int8ListVarBench() : super('TypedData.Int8ListVarBench'); + @override + void run() { + doSetInt8Var(list); + final int x = doGetInt8(list); + if (x != -212) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint8ListVarBench extends BenchmarkBase { + var list = Uint8List(N); + Uint8ListVarBench() : super('TypedData.Uint8ListVarBench'); + @override + void run() { + doSetUint8Var(list); + final int x = doGetUint8(list); + if (x != 124716) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint8ClampedListVarBench extends BenchmarkBase { + var list = Uint8ClampedList(N); + Uint8ClampedListVarBench() : super('TypedData.Uint8ClampedListVarBench'); + @override + void run() { + doSetUint8ClampedVar(list); + final int x = doGetUint8Clamped(list); + if (x != 222360) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int16ListVarBench extends BenchmarkBase { + var list = Int16List(N); + Int16ListVarBench() : super('TypedData.Int16ListVarBench'); + @override + void run() { + doSetInt16Var(list); + final int x = doGetInt16(list); + if (x != 499500) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint16ListVarBench extends BenchmarkBase { + var list = Uint16List(N); + Uint16ListVarBench() : super('TypedData.Uint16ListVarBench'); + @override + void run() { + doSetUint16Var(list); + final int x = doGetUint16(list); + if (x != 499500) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int32ListVarBench extends BenchmarkBase { + var list = Int32List(N); + Int32ListVarBench() : super('TypedData.Int32ListVarBench'); + @override + void run() { + doSetInt32Var(list); + final int x = doGetInt32(list); + if (x != 499500) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint32ListVarBench extends BenchmarkBase { + var list = Uint32List(N); + Uint32ListVarBench() : super('TypedData.Uint32ListVarBench'); + @override + void run() { + doSetUint32Var(list); + final int x = doGetUint32(list); + if (x != 499500) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int64ListVarBench extends BenchmarkBase { + var list = Int64List(N); + Int64ListVarBench() : super('TypedData.Int64ListVarBench'); + @override + void run() { + doSetInt64Var(list); + final int x = doGetInt64(list); + if (x != 499500) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint64ListVarBench extends BenchmarkBase { + var list = Uint64List(N); + Uint64ListVarBench() : super('TypedData.Uint64ListVarBench'); + @override + void run() { + doSetUint64Var(list); + final int x = doGetUint64(list); + if (x != 499500) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Float32ListVarBench extends BenchmarkBase { + var list = Float32List(N); + Float32ListVarBench() : super('TypedData.Float32ListVarBench'); + @override + void run() { + doSetFloat32Var(list); + final double x = doGetFloat32(list); + if (x != 499500.0) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Float64ListVarBench extends BenchmarkBase { + var list = Float64List(N); + Float64ListVarBench() : super('TypedData.Float64ListVarBench'); + @override + void run() { + doSetFloat64Var(list); + final double x = doGetFloat64(list); + if (x != 499500.0) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int8ListViewVarBench extends BenchmarkBase { + var list = Int8List.view(Int8List(N).buffer); + Int8ListViewVarBench() : super('TypedData.Int8ListViewVarBench'); + @override + void run() { + doSetInt8Var(list); + final int x = doGetInt8(list); + if (x != -212) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint8ListViewVarBench extends BenchmarkBase { + var list = Uint8List.view(Uint8List(N).buffer); + Uint8ListViewVarBench() : super('TypedData.Uint8ListViewVarBench'); + @override + void run() { + doSetUint8Var(list); + final int x = doGetUint8(list); + if (x != 124716) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint8ClampedListViewVarBench extends BenchmarkBase { + var list = Uint8ClampedList.view(Uint8ClampedList(N).buffer); + Uint8ClampedListViewVarBench() + : super('TypedData.Uint8ClampedListViewVarBench'); + @override + void run() { + doSetUint8ClampedVar(list); + final int x = doGetUint8Clamped(list); + if (x != 222360) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int16ListViewVarBench extends BenchmarkBase { + var list = Int16List.view(Int16List(N).buffer); + Int16ListViewVarBench() : super('TypedData.Int16ListViewVarBench'); + @override + void run() { + doSetInt16Var(list); + final int x = doGetInt16(list); + if (x != 499500) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint16ListViewVarBench extends BenchmarkBase { + var list = Uint16List.view(Uint16List(N).buffer); + Uint16ListViewVarBench() : super('TypedData.Uint16ListViewVarBench'); + @override + void run() { + doSetUint16Var(list); + final int x = doGetUint16(list); + if (x != 499500) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int32ListViewVarBench extends BenchmarkBase { + var list = Int32List.view(Int32List(N).buffer); + Int32ListViewVarBench() : super('TypedData.Int32ListViewVarBench'); + @override + void run() { + doSetInt32Var(list); + final int x = doGetInt32(list); + if (x != 499500) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint32ListViewVarBench extends BenchmarkBase { + var list = Uint32List.view(Uint32List(N).buffer); + Uint32ListViewVarBench() : super('TypedData.Uint32ListViewVarBench'); + @override + void run() { + doSetUint32Var(list); + final int x = doGetUint32(list); + if (x != 499500) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int64ListViewVarBench extends BenchmarkBase { + var list = Int64List.view(Int64List(N).buffer); + Int64ListViewVarBench() : super('TypedData.Int64ListViewVarBench'); + @override + void run() { + doSetInt64Var(list); + final int x = doGetInt64(list); + if (x != 499500) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint64ListViewVarBench extends BenchmarkBase { + var list = Uint64List.view(Uint64List(N).buffer); + Uint64ListViewVarBench() : super('TypedData.Uint64ListViewVarBench'); + @override + void run() { + doSetUint64Var(list); + final int x = doGetUint64(list); + if (x != 499500) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Float32ListViewVarBench extends BenchmarkBase { + var list = Float32List.view(Float32List(N).buffer); + Float32ListViewVarBench() : super('TypedData.Float32ListViewVarBench'); + @override + void run() { + doSetFloat32Var(list); + final double x = doGetFloat32(list); + if (x != 499500.0) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Float64ListViewVarBench extends BenchmarkBase { + var list = Float64List.view(Float64List(N).buffer); + Float64ListViewVarBench() : super('TypedData.Float64ListViewVarBench'); + @override + void run() { + doSetFloat64Var(list); + final double x = doGetFloat64(list); + if (x != 499500.0) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +// +// Main driver. +// + +void main() { + final microBenchmarks = [ + () => Int8ListBench(), + () => Uint8ListBench(), + () => Uint8ClampedListBench(), + () => Int16ListBench(), + () => Uint16ListBench(), + () => Int32ListBench(), + () => Uint32ListBench(), + () => Int64ListBench(), + () => Uint64ListBench(), + () => Float32ListBench(), + () => Float64ListBench(), + () => Int8ListViewBench(), + () => Uint8ListViewBench(), + () => Uint8ClampedListViewBench(), + () => Int16ListViewBench(), + () => Uint16ListViewBench(), + () => Int32ListViewBench(), + () => Uint32ListViewBench(), + () => Int64ListViewBench(), + () => Uint64ListViewBench(), + () => Float32ListViewBench(), + () => Float64ListViewBench(), + () => Int8ListVarBench(), + () => Uint8ListVarBench(), + () => Uint8ClampedListVarBench(), + () => Int16ListVarBench(), + () => Uint16ListVarBench(), + () => Int32ListVarBench(), + () => Uint32ListVarBench(), + () => Int64ListVarBench(), + () => Uint64ListVarBench(), + () => Float32ListVarBench(), + () => Float64ListVarBench(), + () => Int8ListViewVarBench(), + () => Uint8ListViewVarBench(), + () => Uint8ClampedListViewVarBench(), + () => Int16ListViewVarBench(), + () => Uint16ListViewVarBench(), + () => Int32ListViewVarBench(), + () => Uint32ListViewVarBench(), + () => Int64ListViewVarBench(), + () => Uint64ListViewVarBench(), + () => Float32ListViewVarBench(), + () => Float64ListViewVarBench(), + ]; + for (var mbm in microBenchmarks) { + mbm().report(); + } +} diff --git a/benchmarks/TypedData/dart2/TypedData.dart b/benchmarks/TypedData/dart2/TypedData.dart new file mode 100644 index 00000000000..be9b38167ab --- /dev/null +++ b/benchmarks/TypedData/dart2/TypedData.dart @@ -0,0 +1,879 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. +// +// Micro-benchmarks for typed data setters and getters. +// +// @dart=2.9 + +import 'dart:typed_data'; +import 'package:benchmark_harness/benchmark_harness.dart'; + +// +// Typed constant setters. +// + +void doSetInt8(Int8List list) { + for (int i = 0; i < list.length; i++) { + list[i] = 1; + } +} + +void doSetUint8(Uint8List list) { + for (int i = 0; i < list.length; i++) { + list[i] = 1; + } +} + +void doSetUint8Clamped(Uint8ClampedList list) { + for (int i = 0; i < list.length; i++) { + list[i] = 1; + } +} + +void doSetInt16(Int16List list) { + for (int i = 0; i < list.length; i++) { + list[i] = 1; + } +} + +void doSetUint16(Uint16List list) { + for (int i = 0; i < list.length; i++) { + list[i] = 1; + } +} + +void doSetInt32(Int32List list) { + for (int i = 0; i < list.length; i++) { + list[i] = 1; + } +} + +void doSetUint32(Uint32List list) { + for (int i = 0; i < list.length; i++) { + list[i] = 1; + } +} + +void doSetInt64(Int64List list) { + for (int i = 0; i < list.length; i++) { + list[i] = 1; + } +} + +void doSetUint64(Uint64List list) { + for (int i = 0; i < list.length; i++) { + list[i] = 1; + } +} + +void doSetFloat32(Float32List list) { + for (int i = 0; i < list.length; i++) { + list[i] = 1.0; + } +} + +void doSetFloat64(Float64List list) { + for (int i = 0; i < list.length; i++) { + list[i] = 1.0; + } +} + +// +// Typed variable setters. +// + +void doSetInt8Var(Int8List list) { + for (int i = 0; i < list.length; i++) { + list[i] = i; + } +} + +void doSetUint8Var(Uint8List list) { + for (int i = 0; i < list.length; i++) { + list[i] = i; + } +} + +void doSetUint8ClampedVar(Uint8ClampedList list) { + for (int i = 0; i < list.length; i++) { + list[i] = i; + } +} + +void doSetInt16Var(Int16List list) { + for (int i = 0; i < list.length; i++) { + list[i] = i; + } +} + +void doSetUint16Var(Uint16List list) { + for (int i = 0; i < list.length; i++) { + list[i] = i; + } +} + +void doSetInt32Var(Int32List list) { + for (int i = 0; i < list.length; i++) { + list[i] = i; + } +} + +void doSetUint32Var(Uint32List list) { + for (int i = 0; i < list.length; i++) { + list[i] = i; + } +} + +void doSetInt64Var(Int64List list) { + for (int i = 0; i < list.length; i++) { + list[i] = i; + } +} + +void doSetUint64Var(Uint64List list) { + for (int i = 0; i < list.length; i++) { + list[i] = i; + } +} + +void doSetFloat32Var(Float32List list) { + double x = 0.0; + for (int i = 0; i < list.length; i++) { + list[i] = x++; + } +} + +void doSetFloat64Var(Float64List list) { + double x = 0.0; + for (int i = 0; i < list.length; i++) { + list[i] = x++; + } +} + +// +// Typed getters. +// + +int doGetInt8(Int8List list) { + int x = 0; + for (int i = 0; i < list.length; i++) { + x += list[i]; + } + return x; +} + +int doGetUint8(Uint8List list) { + int x = 0; + for (int i = 0; i < list.length; i++) { + x += list[i]; + } + return x; +} + +int doGetUint8Clamped(Uint8ClampedList list) { + int x = 0; + for (int i = 0; i < list.length; i++) { + x += list[i]; + } + return x; +} + +int doGetInt16(Int16List list) { + int x = 0; + for (int i = 0; i < list.length; i++) { + x += list[i]; + } + return x; +} + +int doGetUint16(Uint16List list) { + int x = 0; + for (int i = 0; i < list.length; i++) { + x += list[i]; + } + return x; +} + +int doGetInt32(Int32List list) { + int x = 0; + for (int i = 0; i < list.length; i++) { + x += list[i]; + } + return x; +} + +int doGetUint32(Uint32List list) { + int x = 0; + for (int i = 0; i < list.length; i++) { + x += list[i]; + } + return x; +} + +int doGetInt64(Int64List list) { + int x = 0; + for (int i = 0; i < list.length; i++) { + x += list[i]; + } + return x; +} + +int doGetUint64(Uint64List list) { + int x = 0; + for (int i = 0; i < list.length; i++) { + x += list[i]; + } + return x; +} + +double doGetFloat32(Float32List list) { + double x = 0.0; + for (int i = 0; i < list.length; i++) { + x += list[i]; + } + return x; +} + +double doGetFloat64(Float64List list) { + double x = 0.0; + for (int i = 0; i < list.length; i++) { + x += list[i]; + } + return x; +} + +// +// Benchmark fixtures. +// + +const N = 1000; + +class Int8ListBench extends BenchmarkBase { + var list = Int8List(N); + Int8ListBench() : super('TypedData.Int8ListBench'); + @override + void run() { + doSetInt8(list); + final int x = doGetInt8(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint8ListBench extends BenchmarkBase { + var list = Uint8List(N); + Uint8ListBench() : super('TypedData.Uint8ListBench'); + @override + void run() { + doSetUint8(list); + final int x = doGetUint8(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint8ClampedListBench extends BenchmarkBase { + var list = Uint8ClampedList(N); + Uint8ClampedListBench() : super('TypedData.Uint8ClampedListBench'); + @override + void run() { + doSetUint8Clamped(list); + final int x = doGetUint8Clamped(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int16ListBench extends BenchmarkBase { + var list = Int16List(N); + Int16ListBench() : super('TypedData.Int16ListBench'); + @override + void run() { + doSetInt16(list); + final int x = doGetInt16(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint16ListBench extends BenchmarkBase { + var list = Uint16List(N); + Uint16ListBench() : super('TypedData.Uint16ListBench'); + @override + void run() { + doSetUint16(list); + final int x = doGetUint16(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int32ListBench extends BenchmarkBase { + var list = Int32List(N); + Int32ListBench() : super('TypedData.Int32ListBench'); + @override + void run() { + doSetInt32(list); + final int x = doGetInt32(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint32ListBench extends BenchmarkBase { + var list = Uint32List(N); + Uint32ListBench() : super('TypedData.Uint32ListBench'); + @override + void run() { + doSetUint32(list); + final int x = doGetUint32(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int64ListBench extends BenchmarkBase { + var list = Int64List(N); + Int64ListBench() : super('TypedData.Int64ListBench'); + @override + void run() { + doSetInt64(list); + final int x = doGetInt64(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint64ListBench extends BenchmarkBase { + var list = Uint64List(N); + Uint64ListBench() : super('TypedData.Uint64ListBench'); + @override + void run() { + doSetUint64(list); + final int x = doGetUint64(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Float32ListBench extends BenchmarkBase { + var list = Float32List(N); + Float32ListBench() : super('TypedData.Float32ListBench'); + @override + void run() { + doSetFloat32(list); + final double x = doGetFloat32(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Float64ListBench extends BenchmarkBase { + var list = Float64List(N); + Float64ListBench() : super('TypedData.Float64ListBench'); + @override + void run() { + doSetFloat64(list); + final double x = doGetFloat64(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int8ListViewBench extends BenchmarkBase { + var list = Int8List.view(Int8List(N).buffer); + Int8ListViewBench() : super('TypedData.Int8ListViewBench'); + @override + void run() { + doSetInt8(list); + final int x = doGetInt8(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint8ListViewBench extends BenchmarkBase { + var list = Uint8List.view(Uint8List(N).buffer); + Uint8ListViewBench() : super('TypedData.Uint8ListViewBench'); + @override + void run() { + doSetUint8(list); + final int x = doGetUint8(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint8ClampedListViewBench extends BenchmarkBase { + var list = Uint8ClampedList.view(Uint8ClampedList(N).buffer); + Uint8ClampedListViewBench() : super('TypedData.Uint8ClampedListViewBench'); + @override + void run() { + doSetUint8Clamped(list); + final int x = doGetUint8Clamped(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int16ListViewBench extends BenchmarkBase { + var list = Int16List.view(Int16List(N).buffer); + Int16ListViewBench() : super('TypedData.Int16ListViewBench'); + @override + void run() { + doSetInt16(list); + final int x = doGetInt16(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint16ListViewBench extends BenchmarkBase { + var list = Uint16List.view(Uint16List(N).buffer); + Uint16ListViewBench() : super('TypedData.Uint16ListViewBench'); + @override + void run() { + doSetUint16(list); + final int x = doGetUint16(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int32ListViewBench extends BenchmarkBase { + var list = Int32List.view(Int32List(N).buffer); + Int32ListViewBench() : super('TypedData.Int32ListViewBench'); + @override + void run() { + doSetInt32(list); + final int x = doGetInt32(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint32ListViewBench extends BenchmarkBase { + var list = Uint32List.view(Uint32List(N).buffer); + Uint32ListViewBench() : super('TypedData.Uint32ListViewBench'); + @override + void run() { + doSetUint32(list); + final int x = doGetUint32(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int64ListViewBench extends BenchmarkBase { + var list = Int64List.view(Int64List(N).buffer); + Int64ListViewBench() : super('TypedData.Int64ListViewBench'); + @override + void run() { + doSetInt64(list); + final int x = doGetInt64(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint64ListViewBench extends BenchmarkBase { + var list = Uint64List.view(Uint64List(N).buffer); + Uint64ListViewBench() : super('TypedData.Uint64ListViewBench'); + @override + void run() { + doSetUint64(list); + final int x = doGetUint64(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Float32ListViewBench extends BenchmarkBase { + var list = Float32List.view(Float32List(N).buffer); + Float32ListViewBench() : super('TypedData.Float32ListViewBench'); + @override + void run() { + doSetFloat32(list); + final double x = doGetFloat32(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Float64ListViewBench extends BenchmarkBase { + var list = Float64List.view(Float64List(N).buffer); + Float64ListViewBench() : super('TypedData.Float64ListViewBench'); + @override + void run() { + doSetFloat64(list); + final double x = doGetFloat64(list); + if (x != N) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int8ListVarBench extends BenchmarkBase { + var list = Int8List(N); + Int8ListVarBench() : super('TypedData.Int8ListVarBench'); + @override + void run() { + doSetInt8Var(list); + final int x = doGetInt8(list); + if (x != -212) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint8ListVarBench extends BenchmarkBase { + var list = Uint8List(N); + Uint8ListVarBench() : super('TypedData.Uint8ListVarBench'); + @override + void run() { + doSetUint8Var(list); + final int x = doGetUint8(list); + if (x != 124716) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint8ClampedListVarBench extends BenchmarkBase { + var list = Uint8ClampedList(N); + Uint8ClampedListVarBench() : super('TypedData.Uint8ClampedListVarBench'); + @override + void run() { + doSetUint8ClampedVar(list); + final int x = doGetUint8Clamped(list); + if (x != 222360) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int16ListVarBench extends BenchmarkBase { + var list = Int16List(N); + Int16ListVarBench() : super('TypedData.Int16ListVarBench'); + @override + void run() { + doSetInt16Var(list); + final int x = doGetInt16(list); + if (x != 499500) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint16ListVarBench extends BenchmarkBase { + var list = Uint16List(N); + Uint16ListVarBench() : super('TypedData.Uint16ListVarBench'); + @override + void run() { + doSetUint16Var(list); + final int x = doGetUint16(list); + if (x != 499500) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int32ListVarBench extends BenchmarkBase { + var list = Int32List(N); + Int32ListVarBench() : super('TypedData.Int32ListVarBench'); + @override + void run() { + doSetInt32Var(list); + final int x = doGetInt32(list); + if (x != 499500) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint32ListVarBench extends BenchmarkBase { + var list = Uint32List(N); + Uint32ListVarBench() : super('TypedData.Uint32ListVarBench'); + @override + void run() { + doSetUint32Var(list); + final int x = doGetUint32(list); + if (x != 499500) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int64ListVarBench extends BenchmarkBase { + var list = Int64List(N); + Int64ListVarBench() : super('TypedData.Int64ListVarBench'); + @override + void run() { + doSetInt64Var(list); + final int x = doGetInt64(list); + if (x != 499500) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint64ListVarBench extends BenchmarkBase { + var list = Uint64List(N); + Uint64ListVarBench() : super('TypedData.Uint64ListVarBench'); + @override + void run() { + doSetUint64Var(list); + final int x = doGetUint64(list); + if (x != 499500) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Float32ListVarBench extends BenchmarkBase { + var list = Float32List(N); + Float32ListVarBench() : super('TypedData.Float32ListVarBench'); + @override + void run() { + doSetFloat32Var(list); + final double x = doGetFloat32(list); + if (x != 499500.0) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Float64ListVarBench extends BenchmarkBase { + var list = Float64List(N); + Float64ListVarBench() : super('TypedData.Float64ListVarBench'); + @override + void run() { + doSetFloat64Var(list); + final double x = doGetFloat64(list); + if (x != 499500.0) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int8ListViewVarBench extends BenchmarkBase { + var list = Int8List.view(Int8List(N).buffer); + Int8ListViewVarBench() : super('TypedData.Int8ListViewVarBench'); + @override + void run() { + doSetInt8Var(list); + final int x = doGetInt8(list); + if (x != -212) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint8ListViewVarBench extends BenchmarkBase { + var list = Uint8List.view(Uint8List(N).buffer); + Uint8ListViewVarBench() : super('TypedData.Uint8ListViewVarBench'); + @override + void run() { + doSetUint8Var(list); + final int x = doGetUint8(list); + if (x != 124716) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint8ClampedListViewVarBench extends BenchmarkBase { + var list = Uint8ClampedList.view(Uint8ClampedList(N).buffer); + Uint8ClampedListViewVarBench() + : super('TypedData.Uint8ClampedListViewVarBench'); + @override + void run() { + doSetUint8ClampedVar(list); + final int x = doGetUint8Clamped(list); + if (x != 222360) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int16ListViewVarBench extends BenchmarkBase { + var list = Int16List.view(Int16List(N).buffer); + Int16ListViewVarBench() : super('TypedData.Int16ListViewVarBench'); + @override + void run() { + doSetInt16Var(list); + final int x = doGetInt16(list); + if (x != 499500) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint16ListViewVarBench extends BenchmarkBase { + var list = Uint16List.view(Uint16List(N).buffer); + Uint16ListViewVarBench() : super('TypedData.Uint16ListViewVarBench'); + @override + void run() { + doSetUint16Var(list); + final int x = doGetUint16(list); + if (x != 499500) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int32ListViewVarBench extends BenchmarkBase { + var list = Int32List.view(Int32List(N).buffer); + Int32ListViewVarBench() : super('TypedData.Int32ListViewVarBench'); + @override + void run() { + doSetInt32Var(list); + final int x = doGetInt32(list); + if (x != 499500) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint32ListViewVarBench extends BenchmarkBase { + var list = Uint32List.view(Uint32List(N).buffer); + Uint32ListViewVarBench() : super('TypedData.Uint32ListViewVarBench'); + @override + void run() { + doSetUint32Var(list); + final int x = doGetUint32(list); + if (x != 499500) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Int64ListViewVarBench extends BenchmarkBase { + var list = Int64List.view(Int64List(N).buffer); + Int64ListViewVarBench() : super('TypedData.Int64ListViewVarBench'); + @override + void run() { + doSetInt64Var(list); + final int x = doGetInt64(list); + if (x != 499500) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Uint64ListViewVarBench extends BenchmarkBase { + var list = Uint64List.view(Uint64List(N).buffer); + Uint64ListViewVarBench() : super('TypedData.Uint64ListViewVarBench'); + @override + void run() { + doSetUint64Var(list); + final int x = doGetUint64(list); + if (x != 499500) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Float32ListViewVarBench extends BenchmarkBase { + var list = Float32List.view(Float32List(N).buffer); + Float32ListViewVarBench() : super('TypedData.Float32ListViewVarBench'); + @override + void run() { + doSetFloat32Var(list); + final double x = doGetFloat32(list); + if (x != 499500.0) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +class Float64ListViewVarBench extends BenchmarkBase { + var list = Float64List.view(Float64List(N).buffer); + Float64ListViewVarBench() : super('TypedData.Float64ListViewVarBench'); + @override + void run() { + doSetFloat64Var(list); + final double x = doGetFloat64(list); + if (x != 499500.0) { + throw Exception('$name: Unexpected result: $x'); + } + } +} + +// +// Main driver. +// + +void main() { + final microBenchmarks = [ + () => Int8ListBench(), + () => Uint8ListBench(), + () => Uint8ClampedListBench(), + () => Int16ListBench(), + () => Uint16ListBench(), + () => Int32ListBench(), + () => Uint32ListBench(), + () => Int64ListBench(), + () => Uint64ListBench(), + () => Float32ListBench(), + () => Float64ListBench(), + () => Int8ListViewBench(), + () => Uint8ListViewBench(), + () => Uint8ClampedListViewBench(), + () => Int16ListViewBench(), + () => Uint16ListViewBench(), + () => Int32ListViewBench(), + () => Uint32ListViewBench(), + () => Int64ListViewBench(), + () => Uint64ListViewBench(), + () => Float32ListViewBench(), + () => Float64ListViewBench(), + () => Int8ListVarBench(), + () => Uint8ListVarBench(), + () => Uint8ClampedListVarBench(), + () => Int16ListVarBench(), + () => Uint16ListVarBench(), + () => Int32ListVarBench(), + () => Uint32ListVarBench(), + () => Int64ListVarBench(), + () => Uint64ListVarBench(), + () => Float32ListVarBench(), + () => Float64ListVarBench(), + () => Int8ListViewVarBench(), + () => Uint8ListViewVarBench(), + () => Uint8ClampedListViewVarBench(), + () => Int16ListViewVarBench(), + () => Uint16ListViewVarBench(), + () => Int32ListViewVarBench(), + () => Uint32ListViewVarBench(), + () => Int64ListViewVarBench(), + () => Uint64ListViewVarBench(), + () => Float32ListViewVarBench(), + () => Float64ListViewVarBench(), + ]; + for (var mbm in microBenchmarks) { + mbm().report(); + } +} diff --git a/benchmarks/TypedDataCopy/dart/TypedDataCopy.dart b/benchmarks/TypedDataCopy/dart/TypedDataCopy.dart new file mode 100644 index 00000000000..7e2de14d2ce --- /dev/null +++ b/benchmarks/TypedDataCopy/dart/TypedDataCopy.dart @@ -0,0 +1,27 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'TypedDataCopyLib.dart'; + +void main() { + final benchmarks = [ + () => Int8ViewToInt8View(), + () => Int8ToInt8(), + () => Int8ToUint8Clamped(), + () => Int8ViewToInt8(), + () => ByteSwap(), + ]; + + // Run all the code to ensure consistent polymorphism in shared code. + for (var bm in benchmarks) { + bm() + ..setup() + ..run() + ..run(); + } + + for (var bm in benchmarks) { + bm().report(); + } +} diff --git a/benchmarks/TypedDataCopy/dart/TypedDataCopyLib.dart b/benchmarks/TypedDataCopy/dart/TypedDataCopyLib.dart new file mode 100644 index 00000000000..6629e63fbd0 --- /dev/null +++ b/benchmarks/TypedDataCopy/dart/TypedDataCopyLib.dart @@ -0,0 +1,174 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'dart:typed_data'; + +import 'package:benchmark_harness/benchmark_harness.dart'; + +// Typed data block copy benchmark. + +const int size = 256 * 1024; + +class Int8ViewToInt8View extends BenchmarkBase { + Int8ViewToInt8View() : super('TypedDataCopy.Int8ViewToInt8View'); + + var a1; + var a2; + + @override + void setup() { + final storage = Int8List(size); + final buffer = storage.buffer; + a1 = Int8List.view(buffer, 0, buffer.lengthInBytes); + a2 = Int8List.view(buffer, 8, buffer.lengthInBytes - 8); + + for (int i = 0; i < a1.length; i++) { + a1[i] = i; + } + } + + @override + void run() { + // Shift all bytes 8 positions nearer to the front. + a1.setRange(0, a2.length, a2); + final check = a1[a1.length - 8 - 1]; + if (check != -1) throw 'Bad $check'; + } +} + +class Int8ViewToInt8 extends BenchmarkBase { + Int8ViewToInt8() : super('TypedDataCopy.Int8ViewToInt8'); + + var a1; + var a2; + + @override + void setup() { + a1 = Int8List(size); + final buffer = a1.buffer; + a2 = Int8List.view(buffer, 8, buffer.lengthInBytes - 8); + + for (int i = 0; i < a1.length; i++) { + a1[i] = i; + } + } + + @override + void run() { + // Shift all bytes 8 positions nearer to the front. + a1.setRange(0, a2.length, a2); + final check = a1[a1.length - 8 - 1]; + if (check != -1) { + throw 'Bad $check'; + } + } +} + +class Int8ToInt8 extends BenchmarkBase { + Int8ToInt8() : super('TypedDataCopy.Int8ToInt8'); + + var a1; + + @override + void setup() { + a1 = Int8List(size); + for (int i = 0; i < a1.length; i++) { + a1[i] = i; + } + } + + @override + void run() { + // Shift all bytes 8 positions nearer to the front. + a1.setRange(0, a1.length - 8, a1, 8); + final check = a1[a1.length - 8 - 1]; + if (check != -1) { + throw 'Bad $check'; + } + } +} + +class Int8ToUint8Clamped extends BenchmarkBase { + Int8ToUint8Clamped() : super('TypedDataCopy.Int8ToUint8Clamped'); + + var a1; + var a2; + + @override + void setup() { + a1 = Uint8ClampedList(size); + a2 = Int8List(size); + for (int i = 0; i < a2.length; i++) { + a2[i] = i; + } + } + + @override + void run() { + a1.setRange(0, a2.length, a2, 0); + var check = a1[100]; + if (check != 100) { + throw 'Bad $check'; + } + check = a1[200]; + if (check != 0) { + throw 'Bad $check'; + } + } +} + +class ByteSwap extends BenchmarkBase { + ByteSwap() : super('TypedDataCopy.ByteSwap'); + + final a8 = Int8List(size ~/ 16); + + @override + void setup() { + for (int i = 0; i < a8.length; i++) { + a8[i] = i; + } + } + + void check(e0, e1, e2, e3) { + final a0 = a8[0]; + final a1 = a8[1]; + final a2 = a8[2]; + final a3 = a8[3]; + if (a0 != e0 || a1 != e1 || a2 != e2 || a3 != e3) { + throw 'Bad: $a0 $a1 $a2 $a3, expected $e0 $e1 $e2 $e3'; + } + } + + @override + void run() { + final b = ByteData.view(a8.buffer); + + // Do several passes over the data, reading and writing in different widths + // with different endianes. + + for (int i = 0; i < b.lengthInBytes; i += 4) { + final e = b.getInt32(i); // Implicit big endian. + b.setInt32(i, e, Endian.little); + } + check(3, 2, 1, 0); + + for (int i = 0; i < b.lengthInBytes; i += 2) { + final e = b.getInt16(i, Endian.big); + b.setInt16(i, e, Endian.little); + } + check(2, 3, 0, 1); + + for (int i = 0; i < b.lengthInBytes; i += 4) { + final e = b.getUint32(i, Endian.little); + b.setUint32(i, e); // Implicit big endian. + } + check(1, 0, 3, 2); + + for (int i = 0; i < b.lengthInBytes; i += 2) { + final e = b.getUint16(i, Endian.little); + b.setUint16(i, e, Endian.big); + } + check(0, 1, 2, 3); // Back to normal for the next run(). + } +} diff --git a/benchmarks/TypedDataCopy/dart2/TypedDataCopy.dart b/benchmarks/TypedDataCopy/dart2/TypedDataCopy.dart new file mode 100644 index 00000000000..d804efa9391 --- /dev/null +++ b/benchmarks/TypedDataCopy/dart2/TypedDataCopy.dart @@ -0,0 +1,29 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// @dart=2.9 + +import 'TypedDataCopyLib.dart'; + +void main() { + final benchmarks = [ + () => Int8ViewToInt8View(), + () => Int8ToInt8(), + () => Int8ToUint8Clamped(), + () => Int8ViewToInt8(), + () => ByteSwap(), + ]; + + // Run all the code to ensure consistent polymorphism in shared code. + for (var bm in benchmarks) { + bm() + ..setup() + ..run() + ..run(); + } + + for (var bm in benchmarks) { + bm().report(); + } +} diff --git a/benchmarks/TypedDataCopy/dart2/TypedDataCopyLib.dart b/benchmarks/TypedDataCopy/dart2/TypedDataCopyLib.dart new file mode 100644 index 00000000000..975f9f6ff61 --- /dev/null +++ b/benchmarks/TypedDataCopy/dart2/TypedDataCopyLib.dart @@ -0,0 +1,176 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// @dart=2.9 + +import 'dart:typed_data'; + +import 'package:benchmark_harness/benchmark_harness.dart'; + +// Typed data block copy benchmark. + +const int size = 256 * 1024; + +class Int8ViewToInt8View extends BenchmarkBase { + Int8ViewToInt8View() : super('TypedDataCopy.Int8ViewToInt8View'); + + var a1; + var a2; + + @override + void setup() { + final storage = Int8List(size); + final buffer = storage.buffer; + a1 = Int8List.view(buffer, 0, buffer.lengthInBytes); + a2 = Int8List.view(buffer, 8, buffer.lengthInBytes - 8); + + for (int i = 0; i < a1.length; i++) { + a1[i] = i; + } + } + + @override + void run() { + // Shift all bytes 8 positions nearer to the front. + a1.setRange(0, a2.length, a2); + final check = a1[a1.length - 8 - 1]; + if (check != -1) throw 'Bad $check'; + } +} + +class Int8ViewToInt8 extends BenchmarkBase { + Int8ViewToInt8() : super('TypedDataCopy.Int8ViewToInt8'); + + var a1; + var a2; + + @override + void setup() { + a1 = Int8List(size); + final buffer = a1.buffer; + a2 = Int8List.view(buffer, 8, buffer.lengthInBytes - 8); + + for (int i = 0; i < a1.length; i++) { + a1[i] = i; + } + } + + @override + void run() { + // Shift all bytes 8 positions nearer to the front. + a1.setRange(0, a2.length, a2); + final check = a1[a1.length - 8 - 1]; + if (check != -1) { + throw 'Bad $check'; + } + } +} + +class Int8ToInt8 extends BenchmarkBase { + Int8ToInt8() : super('TypedDataCopy.Int8ToInt8'); + + var a1; + + @override + void setup() { + a1 = Int8List(size); + for (int i = 0; i < a1.length; i++) { + a1[i] = i; + } + } + + @override + void run() { + // Shift all bytes 8 positions nearer to the front. + a1.setRange(0, a1.length - 8, a1, 8); + final check = a1[a1.length - 8 - 1]; + if (check != -1) { + throw 'Bad $check'; + } + } +} + +class Int8ToUint8Clamped extends BenchmarkBase { + Int8ToUint8Clamped() : super('TypedDataCopy.Int8ToUint8Clamped'); + + var a1; + var a2; + + @override + void setup() { + a1 = Uint8ClampedList(size); + a2 = Int8List(size); + for (int i = 0; i < a2.length; i++) { + a2[i] = i; + } + } + + @override + void run() { + a1.setRange(0, a2.length, a2, 0); + var check = a1[100]; + if (check != 100) { + throw 'Bad $check'; + } + check = a1[200]; + if (check != 0) { + throw 'Bad $check'; + } + } +} + +class ByteSwap extends BenchmarkBase { + ByteSwap() : super('TypedDataCopy.ByteSwap'); + + final a8 = Int8List(size ~/ 16); + + @override + void setup() { + for (int i = 0; i < a8.length; i++) { + a8[i] = i; + } + } + + void check(e0, e1, e2, e3) { + final a0 = a8[0]; + final a1 = a8[1]; + final a2 = a8[2]; + final a3 = a8[3]; + if (a0 != e0 || a1 != e1 || a2 != e2 || a3 != e3) { + throw 'Bad: $a0 $a1 $a2 $a3, expected $e0 $e1 $e2 $e3'; + } + } + + @override + void run() { + final b = ByteData.view(a8.buffer); + + // Do several passes over the data, reading and writing in different widths + // with different endianes. + + for (int i = 0; i < b.lengthInBytes; i += 4) { + final e = b.getInt32(i); // Implicit big endian. + b.setInt32(i, e, Endian.little); + } + check(3, 2, 1, 0); + + for (int i = 0; i < b.lengthInBytes; i += 2) { + final e = b.getInt16(i, Endian.big); + b.setInt16(i, e, Endian.little); + } + check(2, 3, 0, 1); + + for (int i = 0; i < b.lengthInBytes; i += 4) { + final e = b.getUint32(i, Endian.little); + b.setUint32(i, e); // Implicit big endian. + } + check(1, 0, 3, 2); + + for (int i = 0; i < b.lengthInBytes; i += 2) { + final e = b.getUint16(i, Endian.little); + b.setUint16(i, e, Endian.big); + } + check(0, 1, 2, 3); // Back to normal for the next run(). + } +} diff --git a/benchmarks/TypedDataCopy/javascript/TypedDataCopy.js b/benchmarks/TypedDataCopy/javascript/TypedDataCopy.js new file mode 100644 index 00000000000..f81f4deca2c --- /dev/null +++ b/benchmarks/TypedDataCopy/javascript/TypedDataCopy.js @@ -0,0 +1,112 @@ +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +var K = 1024; +var SIZE = 256 * K; + +var runInt8ToInt8 = (function() { + var a1, a2; + + function setup() { + var storage = new Int8Array(SIZE); + var buffer = storage.buffer; + a1 = new Int8Array(buffer, 0, buffer.byteLength); + a2 = new Int8Array(buffer, 8, buffer.byteLength - 8); + + for (var i = 0; i < a1.length; i++) a1[i] = i; + } + + function run() { + a1.set(a2, 0); + var check = a1[a1.length - 8 - 1]; + if (check != -1) throw 'Bad ' + check; + } + + setup(); + return run; +})(); + +var runInt8ToUint8Clamped = (function() { + var a1, a2; + + function setup() { + a1 = new Uint8ClampedArray(SIZE); + a2 = new Int8Array(SIZE); + + for (var i = 0; i < a2.length; i++) a2[i] = i; + } + + function run() { + a1.set(a2, 0); + var check = a1[100]; + if (check != 100) throw 'Bad ' + check; + check = a1[200]; + if (check != 0) throw 'Bad ' + check; // 200 = -56 clipped to 0. + } + + setup(); + return run; +})(); + +var runByteSwap = (function() { + var a8 = new Int8Array((SIZE / 16) | 0); + + function setup() { + for (var i = 0; i < a8.length; i++) a8[i] = i; + } + + function check(e0, e1, e2, e3) { + var a0 = a8[0]; + var a1 = a8[1]; + var a2 = a8[2]; + var a3 = a8[3]; + if (a0 != e0 || a1 != e1 || a2 != e2 || a3 != e3) { + throw 'Bad: ' + [a0, a1, a2, a3] + ', expected ' + [e0, e1, e2, e3]; + } + } + + function run() { + var b = new DataView(a8.buffer); + var i, e; + + // Do several passes over the data, reading and writing in different widths + // with different endiannesses. + + for (i = 0; i < b.byteLength; i += 4) { + e = b.getInt32(i); // Implicit BIG_ENDIAN. + b.setInt32(i, e, true); // LITTLE_ENDIAN + } + check(3, 2, 1, 0); + + for (i = 0; i < b.byteLength; i += 2) { + e = b.getInt16(i, false); // BIG_ENDIAN + b.setInt16(i, e, true); // LITTLE_ENDIAN + } + check(2, 3, 0, 1); + + for (i = 0; i < b.byteLength; i += 4) { + e = b.getUint32(i, true); // LITTLE_ENDIAN + b.setUint32(i, e); // Implicit BIG_ENDIAN. + } + check(1, 0, 3, 2); + + for (i = 0; i < b.byteLength; i += 2) { + e = b.getUint16(i, true); // LITTLE_ENDIAN + b.setUint16(i, e, false); // BIG_ENDIAN + } + check(0, 1, 2, 3); // Back to normal for the next run(). + } + + setup(); + return run; +})(); + +Benchmark.report("TypedDataCopy.Int8ViewToInt8View", runInt8ToInt8); +// Yes, these are the same: +Benchmark.report("TypedDataCopy.Int8ToInt8", runInt8ToInt8); +Benchmark.report("TypedDataCopy.Int8ViewToInt8", runInt8ToInt8); + +Benchmark.report("TypedDataCopy.Int8ToUint8Clamped", runInt8ToUint8Clamped); + +Benchmark.report("TypedDataCopy.ByteSwap", runByteSwap);