Skip to content

Commit 3960d63

Browse files
Refactor: Improve clarity and correctness of benchmark data generators
- Fix a bug in the `_pathologicalBase` creation logic for odd sizes. - Make `_reversedBase` generation more declarative using `.reversed.toList()`. - Add clarifying comments for complex calculations.
1 parent 9ebe846 commit 3960d63

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

pkgs/collection/benchmark/dataset_generator.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ const count = 128; // Number of lists to cycle through.
1515
final List<int> _sortedBase = List.generate(size, (i) => i, growable: false);
1616
final List<int> _reversedBase = _sortedBase.reversed.toList(growable: false);
1717

18-
final _pathologicalBase = <int>[
18+
// Ensure the reverse loop for the pathological list starts on the highest
19+
// odd index, regardless of whether `size` is even or odd.
20+
final int secondLoopStart = (size - 1).isOdd ? size - 1 : size - 2;
21+
22+
/// It consists of all even-indexed elements followed by all odd-indexed
23+
/// elements in reverse order.
24+
final List<int> _pathologicalBase = [
1925
for (var i = 0; i < size; i += 2) _sortedBase[i],
20-
for (var i = size - 1; i > 0; i -= 2) _sortedBase[i],
26+
for (var i = secondLoopStart; i > -1; i -= 2) _sortedBase[i],
2127
];
2228

2329
final List<List<int>> random = _generateRandom();

0 commit comments

Comments
 (0)