Skip to content

Commit 02957e6

Browse files
authored
Use FastHash on Piece. (#1598)
This mixin overrides the default identity-based hash code with a stored integer hash code. That made a big difference in the old formatter's performance but for most of the times I tried it with the new one, it didn't help. But it seems that the new formatter is storing pieces in maps enough for it to make a difference. On the microbenchmarks: ``` Benchmark (tall) fastest median slowest average baseline ----------------------------- -------- ------- ------- ------- -------- block 0.057 0.061 0.121 0.065 107.0% chain 0.524 0.539 0.579 0.541 117.1% collection 0.247 0.256 0.273 0.257 107.1% collection_large 1.539 1.559 1.621 1.562 106.0% conditional 0.060 0.061 0.079 0.062 107.0% curry 0.455 0.462 0.477 0.463 118.0% ffi 0.143 0.149 0.159 0.148 105.4% flutter_popup_menu_test 0.503 0.514 0.541 0.516 115.0% flutter_scrollbar_test 0.399 0.406 0.450 0.410 113.2% function_call 1.658 1.709 1.849 1.716 106.0% infix_large 0.552 0.567 0.600 0.570 108.9% infix_small 0.145 0.148 0.162 0.149 105.3% interpolation 0.087 0.089 0.101 0.090 102.5% interpolation_1516 0.070 0.071 0.085 0.071 100.7% large 3.263 3.280 3.573 3.298 109.0% top_level 0.127 0.128 0.147 0.130 107.9% ``` And when formatting the Flutter repo: ``` Current formatter 10.138 ======================================== Optimized 9.490 ===================================== Old formatter 4.812 ================== The current formatter is 52.54% slower than the old formatter. The optimized is 6.83% faster than the current formatter. The optimized is 49.29% slower than the old formatter. The optimization gets the formatter 12.17% of the way to the old one. ``` That's pretty good for a one line change. Also, this makes debugging the new formatter easier because now each Piece has a different debug string. Trying to figure out which of a hundred `Infix` pieces is the one you want gets pretty tedious...
1 parent ecfc0cb commit 02957e6

File tree

5 files changed

+6
-5
lines changed

5 files changed

+6
-5
lines changed
File renamed without changes.

lib/src/piece/piece.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import '../back_end/code_writer.dart';
6+
import '../fast_hash.dart';
67
import '../profile.dart';
78

89
typedef Constrain = void Function(Piece other, State constrainedState);
@@ -14,7 +15,7 @@ typedef Constrain = void Function(Piece other, State constrainedState);
1415
/// roughly follows the AST but includes comments and is optimized for
1516
/// formatting and line splitting. The final output is then determined by
1617
/// deciding which pieces split and how.
17-
abstract base class Piece {
18+
abstract base class Piece with FastHash {
1819
/// The ordered list of all possible ways this piece could split.
1920
///
2021
/// Piece subclasses should override this if they support being split in
@@ -190,7 +191,7 @@ abstract base class Piece {
190191
String get debugName => runtimeType.toString().replaceAll('Piece', '');
191192

192193
@override
193-
String toString() => '$debugName${_pinnedState ?? ''}';
194+
String toString() => '$debugName$id${_pinnedState ?? ''}';
194195
}
195196

196197
/// A state that a piece can be in.

lib/src/short/chunk.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
4+
import '../fast_hash.dart';
45
import '../profile.dart';
5-
import 'fast_hash.dart';
66
import 'marking_scheme.dart';
77
import 'nesting_level.dart';
88
import 'rule/rule.dart';

lib/src/short/nesting_level.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'fast_hash.dart';
5+
import '../fast_hash.dart';
66
import 'marking_scheme.dart';
77

88
/// A single level of expression nesting.

lib/src/short/rule/rule.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import '../../constants.dart';
6+
import '../../fast_hash.dart';
67
import '../../profile.dart';
78
import '../chunk.dart';
8-
import '../fast_hash.dart';
99

1010
/// A constraint that determines the different ways a related set of chunks may
1111
/// be split.

0 commit comments

Comments
 (0)