From 5f150f1ad8c156f786c1a4051c8e0ba6e5aa0a3b Mon Sep 17 00:00:00 2001 From: Jake Macdonald Date: Mon, 4 Nov 2024 22:02:01 +0000 Subject: [PATCH 1/3] add a basic benchmark for DeepCollectionEquality --- .../benchmark/deep_collection_equality.dart | 65 +++++++++++++++++++ pkgs/collection/pubspec.yaml | 1 + 2 files changed, 66 insertions(+) create mode 100644 pkgs/collection/benchmark/deep_collection_equality.dart diff --git a/pkgs/collection/benchmark/deep_collection_equality.dart b/pkgs/collection/benchmark/deep_collection_equality.dart new file mode 100644 index 000000000..8582e834e --- /dev/null +++ b/pkgs/collection/benchmark/deep_collection_equality.dart @@ -0,0 +1,65 @@ +// Copyright (c) 2014, 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'; +import 'package:collection/collection.dart'; + +void main() { + for (var unordered in [true, false]) { + DeepCollectionEqualityEqualsBenchmark(unordered).report(); + DeepCollectionEqualityHashBenchmark(unordered).report(); + } +} + +class DeepCollectionEqualityBase extends BenchmarkBase { + final DeepCollectionEquality equality; + + DeepCollectionEqualityBase(bool unordered, String function) + : equality = unordered + ? const DeepCollectionEquality.unordered() + : const DeepCollectionEquality(), + super('DeepCollectionQuality${unordered ? 'Unordered' : ''}.$function'); +} + +class DeepCollectionEqualityEqualsBenchmark extends DeepCollectionEqualityBase { + DeepCollectionEqualityEqualsBenchmark(bool unordered) + : super(unordered, 'hash'); + + @override + void run() { + hash = equality.hash(mapA); + } + + static int hash = 0; +} + +class DeepCollectionEqualityHashBenchmark extends DeepCollectionEqualityBase { + DeepCollectionEqualityHashBenchmark(bool unordered) + : super(unordered, 'equals'); + + @override + void run() { + equals = equality.equals(mapA, mapB); + } + + static bool equals = false; +} + +final mapA = { + for (var i = 0; i < 100; i++) + { + [ + for (var j = i; j < i + 10; j++) j, + ]: i.isEven ? i : '$i', + } +}; + +final mapB = { + for (var i = 0; i < 100; i++) + { + [ + for (var j = i; j < i + 10; j++) j, + ]: i.isEven ? i : '$i', + } +}; diff --git a/pkgs/collection/pubspec.yaml b/pkgs/collection/pubspec.yaml index c1b163340..ac729ff60 100644 --- a/pkgs/collection/pubspec.yaml +++ b/pkgs/collection/pubspec.yaml @@ -12,5 +12,6 @@ environment: sdk: ^3.4.0 dev_dependencies: + benchmark_harness: ^2.3.1 dart_flutter_team_lints: ^3.0.0 test: ^1.16.6 From ab7d80972275a4dcade2ff4f96249bcec1b534d3 Mon Sep 17 00:00:00 2001 From: Jake Macdonald Date: Mon, 4 Nov 2024 22:31:26 +0000 Subject: [PATCH 2/3] ignore exe files under benchmark --- pkgs/collection/.gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/pkgs/collection/.gitignore b/pkgs/collection/.gitignore index 98d6d21f4..3320291ca 100644 --- a/pkgs/collection/.gitignore +++ b/pkgs/collection/.gitignore @@ -8,3 +8,4 @@ build/ packages .packages pubspec.lock +benchmark/*.exe From 281afedbc035f2e2ac8ac923261f2e43ebb37d5f Mon Sep 17 00:00:00 2001 From: Jake Macdonald Date: Mon, 4 Nov 2024 22:34:12 +0000 Subject: [PATCH 3/3] fix typo in names --- pkgs/collection/benchmark/deep_collection_equality.dart | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkgs/collection/benchmark/deep_collection_equality.dart b/pkgs/collection/benchmark/deep_collection_equality.dart index 8582e834e..182cf86d1 100644 --- a/pkgs/collection/benchmark/deep_collection_equality.dart +++ b/pkgs/collection/benchmark/deep_collection_equality.dart @@ -22,8 +22,8 @@ class DeepCollectionEqualityBase extends BenchmarkBase { super('DeepCollectionQuality${unordered ? 'Unordered' : ''}.$function'); } -class DeepCollectionEqualityEqualsBenchmark extends DeepCollectionEqualityBase { - DeepCollectionEqualityEqualsBenchmark(bool unordered) +class DeepCollectionEqualityHashBenchmark extends DeepCollectionEqualityBase { + DeepCollectionEqualityHashBenchmark(bool unordered) : super(unordered, 'hash'); @override @@ -34,8 +34,8 @@ class DeepCollectionEqualityEqualsBenchmark extends DeepCollectionEqualityBase { static int hash = 0; } -class DeepCollectionEqualityHashBenchmark extends DeepCollectionEqualityBase { - DeepCollectionEqualityHashBenchmark(bool unordered) +class DeepCollectionEqualityEqualsBenchmark extends DeepCollectionEqualityBase { + DeepCollectionEqualityEqualsBenchmark(bool unordered) : super(unordered, 'equals'); @override