Skip to content

Commit

Permalink
Cache hash codes for Sass strings and numbers
Browse files Browse the repository at this point in the history
These values are often stored in maps, so caching their hash codes
seems to be net valuable.
  • Loading branch information
nex3 committed Oct 20, 2021
1 parent fa25764 commit 6069708
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.43.3

* Improve performance.

## 1.43.2

* Improve the error message when the default namespace of a `@use` rule is not
Expand Down
8 changes: 7 additions & 1 deletion lib/src/value/number.dart
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ abstract class SassNumber extends Value {
/// integer value, or [assertInt] to do both at once.
final num value;

/// The cached hash code for this number, if it's been computed.
///
/// @nodoc
@protected
int? hashCache;

/// This number's numerator units.
List<String> get numeratorUnits;

Expand Down Expand Up @@ -826,7 +832,7 @@ abstract class SassNumber extends Value {
}
}

int get hashCode => fuzzyHashCode(value *
int get hashCode => hashCache ??= fuzzyHashCode(value *
_canonicalMultiplier(numeratorUnits) /
_canonicalMultiplier(denominatorUnits));

Expand Down
3 changes: 2 additions & 1 deletion lib/src/value/number/single_unit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,6 @@ class SingleUnitSassNumber extends SassNumber {
}
}

int get hashCode => fuzzyHashCode(value * canonicalMultiplierForUnit(_unit));
int get hashCode =>
hashCache ??= fuzzyHashCode(value * canonicalMultiplierForUnit(_unit));
}
2 changes: 1 addition & 1 deletion lib/src/value/number/unitless.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,5 @@ class UnitlessSassNumber extends SassNumber {
bool operator ==(Object other) =>
other is UnitlessSassNumber && fuzzyEquals(value, other.value);

int get hashCode => fuzzyHashCode(value);
int get hashCode => hashCache ??= fuzzyHashCode(value);
}
5 changes: 4 additions & 1 deletion lib/src/value/string.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class SassString extends Value {
/// efficient.
late final int sassLength = text.runes.length;

/// The cached hash code for this number, if it's been computed.
int? _hashCache;

/// @nodoc
@internal
bool get isSpecialNumber {
Expand Down Expand Up @@ -189,7 +192,7 @@ class SassString extends Value {

bool operator ==(Object other) => other is SassString && text == other.text;

int get hashCode => text.hashCode;
int get hashCode => _hashCache ??= text.hashCode;

/// Throws a [SassScriptException] with the given [message].
SassScriptException _exception(String message, [String? name]) =>
Expand Down
4 changes: 4 additions & 0 deletions pkg/sass_api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.0-beta.17

* No user-visible changes.

## 1.0.0-beta.16

* No user-visible changes.
Expand Down
4 changes: 2 additions & 2 deletions pkg/sass_api/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ name: sass_api
# Note: Every time we add a new Sass AST node, we need to bump the *major*
# version because it's a breaking change for anyone who's implementing the
# visitor interface(s).
version: 1.0.0-beta.16
version: 1.0.0-beta.17
description: Additional APIs for Dart Sass.
homepage: https://github.com/sass/dart-sass

environment:
sdk: '>=2.12.0 <3.0.0'

dependencies:
sass: 1.43.2
sass: 1.43.3

dependency_overrides:
sass: {path: ../..}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: sass
version: 1.43.2
version: 1.43.3
description: A Sass implementation in Dart.
homepage: https://github.com/sass/dart-sass

Expand Down

0 comments on commit 6069708

Please sign in to comment.