From 676539de96720f466c18757bdb5e9d4e0a830df3 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 20:40:41 +0200 Subject: [PATCH 1/5] perf(html): reuse unchanged decoded input and avoid span sublists --- pkgs/html/lib/src/html_input_stream.dart | 31 +++++++++++++++--------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/pkgs/html/lib/src/html_input_stream.dart b/pkgs/html/lib/src/html_input_stream.dart index 8c5335147c..d88c969449 100644 --- a/pkgs/html/lib/src/html_input_stream.dart +++ b/pkgs/html/lib/src/html_input_stream.dart @@ -93,13 +93,15 @@ class HtmlInputStream { final rawChars = _rawChars ??= _decodeBytes(charEncodingName!, _rawBytes!); - // Optimistically allocate array, trim it later if there are changes - _chars = List.filled(rawChars.length, 0, growable: true); + // The common case has no CR/CRLF or malformed surrogates. Keep the + // decoded code units directly and copy only if normalization changes them. + // This avoids a second full-size allocation for ordinary HTML (including + // Hebrew text). Never modify rawChars: reset() must remain repeatable. + var chars = rawChars; var skipNewline = false; var wasSurrogatePair = false; var deletedChars = 0; - /// CodeUnits.length is not free final charsLength = rawChars.length; for (var i = 0; i < charsLength; i++) { var c = rawChars[i]; @@ -130,12 +132,17 @@ class HtmlInputStream { c = Charcode.lineFeed; } - _chars[i - deletedChars] = c; + if (c != rawChars[i] && identical(chars, rawChars)) { + chars = List.of(rawChars, growable: true); + } + if (!identical(chars, rawChars)) { + chars[i - deletedChars] = c; + } } if (deletedChars > 0) { - // Remove the null bytes from the end - _chars.removeRange(_chars.length - deletedChars, _chars.length); + chars.length -= deletedChars; } + _chars = chars; // Free decoded characters if they aren't needed anymore. if (_rawBytes != null) _rawChars = null; @@ -279,7 +286,7 @@ class HtmlInputStream { _offset += 1; } - return String.fromCharCodes(_chars.sublist(start, _offset)); + return String.fromCharCodes(_chars, start, _offset); } String charsUntil1(int charCode, [bool opposite = false]) { @@ -289,7 +296,7 @@ class HtmlInputStream { _offset += 1; } - return String.fromCharCodes(_chars.sublist(start, _offset)); + return String.fromCharCodes(_chars, start, _offset); } String charsUntil2(int charCode1, int charCode2, [bool opposite = false]) { @@ -300,7 +307,7 @@ class HtmlInputStream { _offset += 1; } - return String.fromCharCodes(_chars.sublist(start, _offset)); + return String.fromCharCodes(_chars, start, _offset); } String charsUntil3(int charCode1, int charCode2, int charCode3, @@ -312,7 +319,7 @@ class HtmlInputStream { _offset += 1; } - return String.fromCharCodes(_chars.sublist(start, _offset)); + return String.fromCharCodes(_chars, start, _offset); } String charsUntilAsciiLetter([bool opposite = false]) { @@ -324,7 +331,7 @@ class HtmlInputStream { opposite) { _offset += 1; } - return String.fromCharCodes(_chars.sublist(start, _offset)); + return String.fromCharCodes(_chars, start, _offset); } static bool _isSpaceCharacter(int c) => @@ -341,7 +348,7 @@ class HtmlInputStream { _offset += 1; } - return String.fromCharCodes(_chars.sublist(start, _offset)); + return String.fromCharCodes(_chars, start, _offset); } void unget(String? ch) { From 5152437fa1568cb7423785b3210622b9e24b1258 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 20:40:43 +0200 Subject: [PATCH 2/5] perf(html): serialize text escapes by runs without per-char allocations --- pkgs/html/lib/html_escape.dart | 50 ++++++++++++++++------------------ 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/pkgs/html/lib/html_escape.dart b/pkgs/html/lib/html_escape.dart index 93fbf3a350..66cdf44b54 100644 --- a/pkgs/html/lib/html_escape.dart +++ b/pkgs/html/lib/html_escape.dart @@ -19,36 +19,34 @@ /// [1]: https://html.spec.whatwg.org/multipage/parsing.html#serializing-html-fragments /// [2]: https://html.spec.whatwg.org/multipage/parsing.html#escapingString String htmlSerializeEscape(String text, {bool attributeMode = false}) { - // TODO(jmesserly): is it faster to build up a list of codepoints? - // StringBuffer seems cleaner assuming Dart can unbox 1-char strings. + // Scan UTF-16 code units without allocating one-character strings. + // Copy unchanged runs at once; return the original String when no escaping + // is needed, which is common for text-heavy HTML. StringBuffer? result; + var last = 0; for (var i = 0; i < text.length; i++) { - final ch = text[i]; - String? replace; - switch (ch) { - case '&': - replace = '&'; - break; - case '\u00A0' /*NO-BREAK SPACE*/ : - replace = ' '; - break; - case '"': - if (attributeMode) replace = '"'; - break; - case '<': - replace = '<'; - break; - case '>': - replace = '>'; - break; + String? replacement; + switch (text.codeUnitAt(i)) { + case 0x26: // & + replacement = '&'; + case 0xA0: // NO-BREAK SPACE + replacement = ' '; + case 0x22: // " + if (attributeMode) replacement = '"'; + case 0x3C: // < + replacement = '<'; + case 0x3E: // > + replacement = '>'; } - if (replace != null) { - result ??= StringBuffer(text.substring(0, i)); - result.write(replace); - } else if (result != null) { - result.write(ch); + if (replacement != null) { + result ??= StringBuffer(); + result.write(text.substring(last, i)); + result.write(replacement); + last = i + 1; } } - return result != null ? result.toString() : text; + if (result == null) return text; + result.write(text.substring(last)); + return result.toString(); } From 43a5f548732eedff3f88c33a928aadec296a8848 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 20:40:45 +0200 Subject: [PATCH 3/5] test(html): cover input normalization, spans and serializer escaping --- ...ml_stream_performance_regression_test.dart | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 pkgs/html/test/html_stream_performance_regression_test.dart diff --git a/pkgs/html/test/html_stream_performance_regression_test.dart b/pkgs/html/test/html_stream_performance_regression_test.dart new file mode 100644 index 0000000000..da2487c312 --- /dev/null +++ b/pkgs/html/test/html_stream_performance_regression_test.dart @@ -0,0 +1,66 @@ +// Copyright (c) project authors. All rights reserved. +// Licensed under the MIT license. +// See LICENSE file in the project root for details. + +import 'package:html/dom_parsing.dart'; +import 'package:html/src/html_input_stream.dart'; +import 'package:test/test.dart'; + +void main() { + group('HTML input stream normalization', () { + test('unchanged Hebrew and ASCII content can be read in chunks', () { + final stream = HtmlInputStream('שלום עברית'); + expect(stream.charsUntil1(0x3C), 'שלום '); + expect(stream.char(), '<'); + expect(stream.charsUntil2(0x3E, 0x21), 'b'); + expect(stream.char(), '>'); + expect(stream.charsUntil3(0x3C, 0x26, 0x21), 'עברית'); + expect(stream.char(), '<'); + }); + + test('normalizes CRLF and lone CR identically after reset', () { + final stream = HtmlInputStream('first\r\nsecond\rthird\n'); + expect(stream.charsUntil1(0x0), 'first\nsecond\nthird\n'); + stream.reset(); + expect(stream.charsUntil1(0x0), 'first\nsecond\nthird\n'); + }); + + test('preserves surrogate pairs and replaces malformed surrogates', () { + final stream = HtmlInputStream('א\uD83D\uDE00\uD800ב'); + expect(stream.charsUntil1(0x0), 'א\uD83D\uDE00\uFFFDב'); + expect(stream.errors, contains('invalid-codepoint')); + stream.reset(); + expect(stream.charsUntil1(0x0), 'א\uD83D\uDE00\uFFFDב'); + expect(stream.errors, contains('invalid-codepoint')); + }); + + test('source spans keep the normalized source text', () { + final stream = HtmlInputStream('אב\r\nגד', null, true, true); + expect(stream.fileInfo!.getText(0, 5), 'אב\nגד'); + }); + }); + + group('HTML serializer escaping', () { + test('returns text unchanged without escapable characters', () { + final plain = 'שלום עולם 😀'; + expect(htmlSerializeEscape(plain), same(plain)); + }); + + test('escapes markup, ampersands and nonbreaking spaces', () { + expect(htmlSerializeEscape('אב&< >\u00A0גד'), + 'אב&< > גד'); + }); + + test('quotes are escaped only in attributes', () { + expect(htmlSerializeEscape('a"b&c'), 'a"b&c'); + expect(htmlSerializeEscape('a"b&c', attributeMode: true), + 'a"b&c'); + }); + + test('escapes adjacent and trailing characters', () { + expect(htmlSerializeEscape('&&<>'), '&&<>'); + expect(htmlSerializeEscape('<'), '<'); + expect(htmlSerializeEscape(''), ''); + }); + }); +} From 628efe1f991d5509509dbd061fc71e9847c736f6 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 21:48:04 +0200 Subject: [PATCH 4/5] fix(html): slice the source String for unchanged input spans String.fromCharCodes iterates a non-builtin list from its first element on the VM, so reading spans from String.codeUnits made parsing quadratic in the input length. Keep the decoded String and use substring when normalization did not copy the code units. --- pkgs/html/lib/src/html_input_stream.dart | 46 ++++++++++++++---------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/pkgs/html/lib/src/html_input_stream.dart b/pkgs/html/lib/src/html_input_stream.dart index d88c969449..6cb34b1de8 100644 --- a/pkgs/html/lib/src/html_input_stream.dart +++ b/pkgs/html/lib/src/html_input_stream.dart @@ -36,8 +36,8 @@ class HtmlInputStream { List? _rawBytes; - /// Raw UTF-16 codes, used if a Dart String is passed in. - List? _rawChars; + /// Decoded text, or the Dart String passed in. + String? _rawText; Queue errors = Queue(); @@ -45,6 +45,9 @@ class HtmlInputStream { var _chars = []; + /// The String whose code units are [_chars], if normalization kept them. + String? _text; + var _offset = 0; /// Initialise an HtmlInputStream. @@ -68,7 +71,7 @@ class HtmlInputStream { this.sourceUrl]) : charEncodingName = codecName(encoding) { if (source is String) { - _rawChars = source.codeUnits; + _rawText = source; charEncodingName = 'utf-8'; charEncodingCertain = true; } else if (source is List) { @@ -91,12 +94,11 @@ class HtmlInputStream { _offset = 0; - final rawChars = _rawChars ??= _decodeBytes(charEncodingName!, _rawBytes!); + final rawText = _rawText ??= _decodeBytes(charEncodingName!, _rawBytes!); + final rawChars = rawText.codeUnits; - // The common case has no CR/CRLF or malformed surrogates. Keep the - // decoded code units directly and copy only if normalization changes them. - // This avoids a second full-size allocation for ordinary HTML (including - // Hebrew text). Never modify rawChars: reset() must remain repeatable. + // Most input needs no CR/CRLF or surrogate normalization, so keep the + // decoded String and copy its code units only when one of them changes. var chars = rawChars; var skipNewline = false; var wasSurrogatePair = false; @@ -143,9 +145,10 @@ class HtmlInputStream { chars.length -= deletedChars; } _chars = chars; + _text = identical(chars, rawChars) ? rawText : null; // Free decoded characters if they aren't needed anymore. - if (_rawBytes != null) _rawChars = null; + if (_rawBytes != null) _rawText = null; if (generateSpans) { fileInfo = SourceFile.decoded(_chars, url: sourceUrl); @@ -194,7 +197,7 @@ class HtmlInputStream { } else { charEncodingName = newEncoding; charEncodingCertain = true; - _rawChars = null; + _rawText = null; reset(); throw ReparseException( 'Encoding changed from $charEncodingName to $newEncoding'); @@ -286,7 +289,7 @@ class HtmlInputStream { _offset += 1; } - return String.fromCharCodes(_chars, start, _offset); + return _substring(start, _offset); } String charsUntil1(int charCode, [bool opposite = false]) { @@ -296,7 +299,7 @@ class HtmlInputStream { _offset += 1; } - return String.fromCharCodes(_chars, start, _offset); + return _substring(start, _offset); } String charsUntil2(int charCode1, int charCode2, [bool opposite = false]) { @@ -307,7 +310,7 @@ class HtmlInputStream { _offset += 1; } - return String.fromCharCodes(_chars, start, _offset); + return _substring(start, _offset); } String charsUntil3(int charCode1, int charCode2, int charCode3, @@ -319,7 +322,7 @@ class HtmlInputStream { _offset += 1; } - return String.fromCharCodes(_chars, start, _offset); + return _substring(start, _offset); } String charsUntilAsciiLetter([bool opposite = false]) { @@ -331,9 +334,14 @@ class HtmlInputStream { opposite) { _offset += 1; } - return String.fromCharCodes(_chars, start, _offset); + return _substring(start, _offset); } + // String.fromCharCodes copies a String's code units by iterating from the + // first one on the VM, so slice the String itself when it is available. + String _substring(int start, int end) => + _text?.substring(start, end) ?? String.fromCharCodes(_chars, start, end); + static bool _isSpaceCharacter(int c) => c == Charcode.space || c == Charcode.lineFeed || @@ -348,7 +356,7 @@ class HtmlInputStream { _offset += 1; } - return String.fromCharCodes(_chars, start, _offset); + return _substring(start, _offset); } void unget(String? ch) { @@ -439,13 +447,13 @@ bool _isSupportedEncoding(String? encoding) => /// Decodes the [bytes] with the provided [encoding] and returns a list for /// the codepoints. Supports ascii and utf-8 encodings. -List _decodeBytes(String encoding, List bytes) { +String _decodeBytes(String encoding, List bytes) { return switch (encoding) { - 'ascii' => ascii.decode(bytes, allowInvalid: true).codeUnits, + 'ascii' => ascii.decode(bytes, allowInvalid: true), 'utf-8' => // NOTE: To match the behavior of the other decode functions, we eat the // UTF-8 BOM here. This is the default behavior of `utf8.decode`. - utf8.decode(bytes, allowMalformed: true).codeUnits, + utf8.decode(bytes, allowMalformed: true), _ => throw ArgumentError('Encoding $encoding not supported') }; } From 1813200337c6f12699faf88feb2381779fcf25e4 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 21:48:05 +0200 Subject: [PATCH 5/5] test(html): cover decoded byte input; add changelog entry --- pkgs/html/CHANGELOG.md | 1 + pkgs/html/lib/html_escape.dart | 4 +--- ...tml_stream_performance_regression_test.dart | 18 ++++++++++++++---- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/pkgs/html/CHANGELOG.md b/pkgs/html/CHANGELOG.md index ae09a646d8..c7caf3f444 100644 --- a/pkgs/html/CHANGELOG.md +++ b/pkgs/html/CHANGELOG.md @@ -1,5 +1,6 @@ ## 0.15.8-wip +- Reduce allocations when parsing and serializing: `HtmlInputStream` no longer copies the input when it needs no newline or surrogate normalization, and `htmlSerializeEscape` copies unescaped runs at once and returns the input unchanged when nothing needs escaping. - `
` and `

` end tags in foreign content (``, ``) now exit foreign content before reprocessing in HTML mode, matching the WHATWG HTML standard. - `<` and `>` are now escaped as `<` and `>` when serializing attribute values in `htmlSerializeEscape`, `innerHtml`, and `outerHtml`, matching the WHATWG HTML standard. - Fix `Node.attributeSpans` and `Node.attributeValueSpans` throwing a `TypeError` when a start tag emits tokenizer parse errors, and preserve the first occurrence's span on duplicate attributes. diff --git a/pkgs/html/lib/html_escape.dart b/pkgs/html/lib/html_escape.dart index 66cdf44b54..e186b3e331 100644 --- a/pkgs/html/lib/html_escape.dart +++ b/pkgs/html/lib/html_escape.dart @@ -19,9 +19,7 @@ /// [1]: https://html.spec.whatwg.org/multipage/parsing.html#serializing-html-fragments /// [2]: https://html.spec.whatwg.org/multipage/parsing.html#escapingString String htmlSerializeEscape(String text, {bool attributeMode = false}) { - // Scan UTF-16 code units without allocating one-character strings. - // Copy unchanged runs at once; return the original String when no escaping - // is needed, which is common for text-heavy HTML. + // Copy unchanged runs at once instead of one-character strings. StringBuffer? result; var last = 0; for (var i = 0; i < text.length; i++) { diff --git a/pkgs/html/test/html_stream_performance_regression_test.dart b/pkgs/html/test/html_stream_performance_regression_test.dart index da2487c312..7246fa9a0f 100644 --- a/pkgs/html/test/html_stream_performance_regression_test.dart +++ b/pkgs/html/test/html_stream_performance_regression_test.dart @@ -2,6 +2,8 @@ // Licensed under the MIT license. // See LICENSE file in the project root for details. +import 'dart:convert'; + import 'package:html/dom_parsing.dart'; import 'package:html/src/html_input_stream.dart'; import 'package:test/test.dart'; @@ -18,6 +20,15 @@ void main() { expect(stream.char(), '<'); }); + test('decoded bytes can be read in chunks after reset', () { + final stream = HtmlInputStream(utf8.encode('שלום עברית')); + expect(stream.charsUntil1(0x3C), 'שלום '); + stream.reset(); + expect(stream.charsUntil1(0x3C), 'שלום '); + expect(stream.char(), '<'); + expect(stream.charsUntilAsciiLetter(true), 'b'); + }); + test('normalizes CRLF and lone CR identically after reset', () { final stream = HtmlInputStream('first\r\nsecond\rthird\n'); expect(stream.charsUntil1(0x0), 'first\nsecond\nthird\n'); @@ -47,14 +58,13 @@ void main() { }); test('escapes markup, ampersands and nonbreaking spaces', () { - expect(htmlSerializeEscape('אב&< >\u00A0גד'), - 'אב&< > גד'); + expect(htmlSerializeEscape('אב&< >\u00A0גד'), 'אב&< > גד'); }); test('quotes are escaped only in attributes', () { expect(htmlSerializeEscape('a"b&c'), 'a"b&c'); - expect(htmlSerializeEscape('a"b&c', attributeMode: true), - 'a"b&c'); + expect( + htmlSerializeEscape('a"b&c', attributeMode: true), 'a"b&c'); }); test('escapes adjacent and trailing characters', () {