Cache hash codes for Sass strings and numbers

These values are often stored in maps, so caching their hash codes
seems to be net valuable.
This commit is contained in:
Natalie Weizenbaum 2021-10-15 16:10:52 -07:00
parent fa2576449e
commit 6069708a83
8 changed files with 25 additions and 7 deletions

View File

@ -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

View File

@ -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;
@ -826,7 +832,7 @@ abstract class SassNumber extends Value {
}
}
int get hashCode => fuzzyHashCode(value *
int get hashCode => hashCache ??= fuzzyHashCode(value *
_canonicalMultiplier(numeratorUnits) /
_canonicalMultiplier(denominatorUnits));

View File

@ -160,5 +160,6 @@ class SingleUnitSassNumber extends SassNumber {
}
}
int get hashCode => fuzzyHashCode(value * canonicalMultiplierForUnit(_unit));
int get hashCode =>
hashCache ??= fuzzyHashCode(value * canonicalMultiplierForUnit(_unit));
}

View File

@ -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);
}

View File

@ -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 {
@ -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]) =>

View File

@ -1,3 +1,7 @@
## 1.0.0-beta.17
* No user-visible changes.
## 1.0.0-beta.16
* No user-visible changes.

View File

@ -2,7 +2,7 @@ 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
@ -10,7 +10,7 @@ environment:
sdk: '>=2.12.0 <3.0.0'
dependencies:
sass: 1.43.2
sass: 1.43.3
dependency_overrides:
sass: {path: ../..}

View File

@ -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