From 3bef2af5ab5914dba151d0c17c9006b48066af6e Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 22:17:11 +0200 Subject: [PATCH 1/3] [csslib] Hash all Font fields compared by == --- pkgs/csslib/CHANGELOG.md | 37 ++++++++++++++ .../csslib/benchmark/font_hash_benchmark.dart | 45 +++++++++++++++++ pkgs/csslib/lib/src/property.dart | 3 +- pkgs/csslib/test/font_hash_test.dart | 48 +++++++++++++++++++ 4 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 pkgs/csslib/benchmark/font_hash_benchmark.dart create mode 100644 pkgs/csslib/test/font_hash_test.dart diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 2b5e3472da..1c51e98f48 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -1,5 +1,42 @@ ## 1.0.3-wip +- Correct `Font.hashCode` for partial/empty font styles and distribute hashes + across font families and other properties, avoiding hash-table collisions. +- Fix `Rgba.fromArgbValue`, `Rgba.argbValue` and `Hsla.color`, preserve alpha + for grayscale colors in `Hsla.fromRgba`, and keep the exact 8-bit alpha in + `Color.rgba`, `Color.hsla` and `Hsla.fromArgbValue` instead of rounding it to + two digits. `Rgba.fromString` and `Hsla.fromString` parse the CSS color once, + and `Color.css` throws a `FormatException` for unsupported colors. +- Correct CSS namespace URI serialization in pretty-printed stylesheets while + preserving compact `url()` representation. +- Fix fractional alpha in the numeric `Color` constructor and normalize CSS + short and eight-digit hex colors, with validation of malformed input and + unknown color names. + `Color.css` also accepts named colors and `transparent`, and + `Rgba.fromString`/`Hsla.fromString` now parse their argument only once. +- `Color.css` now reads the `hsl()`/`hsla()` hue as a CSS angle (degrees by + default, or `deg`, `grad`, `rad` and `turn`) instead of a 0..1 fraction, and + accepts percentages for saturation, lightness, RGB channels and alpha. + Functional colors with the wrong number of components or invalid or + non-finite values now throw a `FormatException` whose source is the color. +- Correctly parse unit-like type selectors (`rem`, `vw`, `dpi`, etc.) at the + start of `selector()` and `parseSelectorGroup()` input and of style rules. +- Use native integer hexadecimal formatting for named CSS colors, reducing + temporary allocations and avoiding hangs on negative conversion inputs. +- Speed up named CSS color lookups by indexing the color table. +- Recognize the `@left-bottom` page-margin directive, and stop parsing + `@right-bottom` as `@left-bottom`. +- Recognize the `rebeccapurple` color keyword and add `Color.rebeccaPurple`. +- Reduce repeated CSS unit, directive, and media keyword scans by indexing + keyword tables by length while retaining allocation-free token matching. +- Reduce allocations when tokenizing ordinary CSS identifiers; add a parsing + benchmark and regression coverage for Unicode and escaped identifiers. +- Flatten nested top-level selectors with a single ordered pass instead of + repeated linear searches and list insertions on large stylesheets. +- Avoid recursive tokenizer calls when skipping CSS comments and whitespace, + preventing stack overflow on comment-heavy stylesheets. +- End HTML comments at the first `-->` even when more dashes precede it, and + don't end comments at a NUL character. - Fix the values of the `aliceblue` color keyword and the `Color.aliceBlue`, `Color.antiqueWhite` and `Color.peru` constants. - Fix unhandled exceptions (`RangeError`, `FormatException`, `StateError`, diff --git a/pkgs/csslib/benchmark/font_hash_benchmark.dart b/pkgs/csslib/benchmark/font_hash_benchmark.dart new file mode 100644 index 0000000000..eff2de73fd --- /dev/null +++ b/pkgs/csslib/benchmark/font_hash_benchmark.dart @@ -0,0 +1,45 @@ +// Copyright (c) 2026, 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. + +// ignore_for_file: avoid_print + +import 'package:csslib/parser.dart'; + +/// Compare baseline and optimized branches: +/// dart run benchmark/font_hash_benchmark.dart +/// +/// Many font styles share the same size, as they do in document rendering. +/// The old hash function collapsed most of them into the same bucket. +void main() { + const count = 2000; + const passes = 3; + final fonts = List.generate( + count, + (i) => Font(size: 16, family: ['font-$i']), + ); + + // Warm up compilation and the hash-table implementation. + for (var i = 0; i < 2; i++) { + final warmup = {}; + for (var j = 0; j < fonts.length; j++) { + warmup[fonts[j]] = j; + } + } + + final stopwatch = Stopwatch()..start(); + var sum = 0; + for (var pass = 0; pass < passes; pass++) { + final map = {}; + for (var i = 0; i < fonts.length; i++) { + map[fonts[i]] = i; + } + for (final font in fonts) { + sum += map[font]!; + } + } + stopwatch.stop(); + final elapsed = stopwatch.elapsedMicroseconds; + print('Hash insert/lookup: $count fonts x $passes passes, ' + 'sum=$sum, elapsed=$elapsed us'); +} diff --git a/pkgs/csslib/lib/src/property.dart b/pkgs/csslib/lib/src/property.dart index 0188c2b8e2..ded283c7ef 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -1065,8 +1065,7 @@ class Font implements _StyleProperty { @override int get hashCode => - // TODO(jimhug): Lot's of potential collisions here. List of fonts, etc. - size!.toInt() % family![0].hashCode; + Object.hash(size, family, weight, lineHeight, style, variant); @override bool operator ==(Object other) { diff --git a/pkgs/csslib/test/font_hash_test.dart b/pkgs/csslib/test/font_hash_test.dart new file mode 100644 index 0000000000..46070d1599 --- /dev/null +++ b/pkgs/csslib/test/font_hash_test.dart @@ -0,0 +1,48 @@ +// Copyright (c) 2026, 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:csslib/parser.dart'; +import 'package:test/test.dart'; + +void main() { + test('hashCode is safe for partial and empty font properties', () { + final fonts = [ + const Font(), + const Font(size: 12), + const Font(family: []), + const Font(size: 12, family: []), + const Font(size: 12, family: ['serif']), + ]; + for (final font in fonts) { + expect(() => font.hashCode, returnsNormally); + expect(font.hashCode, isA()); + } + }); + + test('equal fonts have equal hashes using the existing equality policy', () { + final family = ['Alef', 'serif']; + final first = Font( + size: 16, + family: family, + weight: FontWeight.bold, + ); + final second = Font( + size: 16, + family: family, + weight: FontWeight.bold, + ); + expect(first, second); + expect(first.hashCode, second.hashCode); + expect({first: 'found'}[second], 'found'); + }); + + test('font hashes distinguish many equal-size distinct families', () { + final fonts = List.generate( + 256, + (i) => Font(size: 16, family: ['family-$i']), + ); + final distinctHashes = fonts.map((font) => font.hashCode).toSet(); + expect(distinctHashes.length, greaterThan(200)); + }); +} From 2ad26c006729b62288a6c3403146858ec08ab7ee Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 23:26:22 +0200 Subject: [PATCH 2/3] Keep only this change's changelog entry --- pkgs/csslib/CHANGELOG.md | 35 ----------------------------------- 1 file changed, 35 deletions(-) diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 1c51e98f48..015a5c718e 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -2,41 +2,6 @@ - Correct `Font.hashCode` for partial/empty font styles and distribute hashes across font families and other properties, avoiding hash-table collisions. -- Fix `Rgba.fromArgbValue`, `Rgba.argbValue` and `Hsla.color`, preserve alpha - for grayscale colors in `Hsla.fromRgba`, and keep the exact 8-bit alpha in - `Color.rgba`, `Color.hsla` and `Hsla.fromArgbValue` instead of rounding it to - two digits. `Rgba.fromString` and `Hsla.fromString` parse the CSS color once, - and `Color.css` throws a `FormatException` for unsupported colors. -- Correct CSS namespace URI serialization in pretty-printed stylesheets while - preserving compact `url()` representation. -- Fix fractional alpha in the numeric `Color` constructor and normalize CSS - short and eight-digit hex colors, with validation of malformed input and - unknown color names. - `Color.css` also accepts named colors and `transparent`, and - `Rgba.fromString`/`Hsla.fromString` now parse their argument only once. -- `Color.css` now reads the `hsl()`/`hsla()` hue as a CSS angle (degrees by - default, or `deg`, `grad`, `rad` and `turn`) instead of a 0..1 fraction, and - accepts percentages for saturation, lightness, RGB channels and alpha. - Functional colors with the wrong number of components or invalid or - non-finite values now throw a `FormatException` whose source is the color. -- Correctly parse unit-like type selectors (`rem`, `vw`, `dpi`, etc.) at the - start of `selector()` and `parseSelectorGroup()` input and of style rules. -- Use native integer hexadecimal formatting for named CSS colors, reducing - temporary allocations and avoiding hangs on negative conversion inputs. -- Speed up named CSS color lookups by indexing the color table. -- Recognize the `@left-bottom` page-margin directive, and stop parsing - `@right-bottom` as `@left-bottom`. -- Recognize the `rebeccapurple` color keyword and add `Color.rebeccaPurple`. -- Reduce repeated CSS unit, directive, and media keyword scans by indexing - keyword tables by length while retaining allocation-free token matching. -- Reduce allocations when tokenizing ordinary CSS identifiers; add a parsing - benchmark and regression coverage for Unicode and escaped identifiers. -- Flatten nested top-level selectors with a single ordered pass instead of - repeated linear searches and list insertions on large stylesheets. -- Avoid recursive tokenizer calls when skipping CSS comments and whitespace, - preventing stack overflow on comment-heavy stylesheets. -- End HTML comments at the first `-->` even when more dashes precede it, and - don't end comments at a NUL character. - Fix the values of the `aliceblue` color keyword and the `Color.aliceBlue`, `Color.antiqueWhite` and `Color.peru` constants. - Fix unhandled exceptions (`RangeError`, `FormatException`, `StateError`, From 601b88327082e07cf8021a35f9649df0a5818409 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Sat, 14 Nov 2026 18:39:47 +0200 Subject: [PATCH 3/3] Address review feedback --- pkgs/csslib/CHANGELOG.md | 3 +- .../csslib/benchmark/font_hash_benchmark.dart | 45 ------------------- pkgs/csslib/test/font_hash_test.dart | 9 ---- 3 files changed, 1 insertion(+), 56 deletions(-) delete mode 100644 pkgs/csslib/benchmark/font_hash_benchmark.dart diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 015a5c718e..025e0d3c2a 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -1,7 +1,6 @@ ## 1.0.3-wip -- Correct `Font.hashCode` for partial/empty font styles and distribute hashes - across font families and other properties, avoiding hash-table collisions. +- Correct `Font.hashCode` for partial/empty font styles. - Fix the values of the `aliceblue` color keyword and the `Color.aliceBlue`, `Color.antiqueWhite` and `Color.peru` constants. - Fix unhandled exceptions (`RangeError`, `FormatException`, `StateError`, diff --git a/pkgs/csslib/benchmark/font_hash_benchmark.dart b/pkgs/csslib/benchmark/font_hash_benchmark.dart deleted file mode 100644 index eff2de73fd..0000000000 --- a/pkgs/csslib/benchmark/font_hash_benchmark.dart +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) 2026, 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. - -// ignore_for_file: avoid_print - -import 'package:csslib/parser.dart'; - -/// Compare baseline and optimized branches: -/// dart run benchmark/font_hash_benchmark.dart -/// -/// Many font styles share the same size, as they do in document rendering. -/// The old hash function collapsed most of them into the same bucket. -void main() { - const count = 2000; - const passes = 3; - final fonts = List.generate( - count, - (i) => Font(size: 16, family: ['font-$i']), - ); - - // Warm up compilation and the hash-table implementation. - for (var i = 0; i < 2; i++) { - final warmup = {}; - for (var j = 0; j < fonts.length; j++) { - warmup[fonts[j]] = j; - } - } - - final stopwatch = Stopwatch()..start(); - var sum = 0; - for (var pass = 0; pass < passes; pass++) { - final map = {}; - for (var i = 0; i < fonts.length; i++) { - map[fonts[i]] = i; - } - for (final font in fonts) { - sum += map[font]!; - } - } - stopwatch.stop(); - final elapsed = stopwatch.elapsedMicroseconds; - print('Hash insert/lookup: $count fonts x $passes passes, ' - 'sum=$sum, elapsed=$elapsed us'); -} diff --git a/pkgs/csslib/test/font_hash_test.dart b/pkgs/csslib/test/font_hash_test.dart index 46070d1599..5648b8eb52 100644 --- a/pkgs/csslib/test/font_hash_test.dart +++ b/pkgs/csslib/test/font_hash_test.dart @@ -36,13 +36,4 @@ void main() { expect(first.hashCode, second.hashCode); expect({first: 'found'}[second], 'found'); }); - - test('font hashes distinguish many equal-size distinct families', () { - final fonts = List.generate( - 256, - (i) => Font(size: 16, family: ['family-$i']), - ); - final distinctHashes = fonts.map((font) => font.hashCode).toSet(); - expect(distinctHashes.length, greaterThan(200)); - }); }