Skip to content

perf(markdown): flatten nested Element.textContent iteratively - #39

Merged
palmoni5 merged 1 commit into
mainfrom
palmoni5/markdown-iterative-ast-text-20261111
Nov 11, 2026
Merged

palmoni5 merged 1 commit into
mainfrom
palmoni5/markdown-iterative-ast-text-20261111

Conversation

@palmoni5

@palmoni5 palmoni5 commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Compute Element.textContent with an explicit iterator stack and a single StringBuffer instead of recursive children.map(...).join().
  • Plain Element children are flattened in place; any other Node (including Element subclasses) is asked for its own textContent, so overridden getters keep working.
  • Deeply nested ASTs (e.g. 10,000 levels, built programmatically) no longer throw StackOverflowError; nested text is no longer copied once per level.
  • Tests: nested/unparsed/empty text, subclass override, 10,000-deep AST (fails on main with a stack overflow).

Impact

Output is unchanged (differential over spec inputs, pub-cache READMEs/CHANGELOGs, Hebrew samples and fuzz: byte-identical). Direct textContent calls are ~30% faster on parsed READMEs and ~7x on deep trees; end-to-end markdownToHtml shows no measurable change, since only image alt text reaches this path. Heading IDs are unaffected (they read UnparsedContent).

Related

dart-lang#2737 (draft) plans the same change upstream.

@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

PR Health

Publish ✔️
Package Version Status Publish tag (post-merge)
package:markdown ⭐ 7.4.1-wip WIP (no publish necessary)
  • 16 already published.
  • 26 WIP (no publish necessary).

Documentation at https://github.com/dart-lang/ecosystem/wiki/Publishing-automation.

This check can be disabled by tagging the PR with skip-publish-check.

Changelog Entry ✔️
Package Changed Files

Changes to files need to be accounted for in their respective changelogs.

This check can be disabled by tagging the PR with skip-changelog-check (for example, for non-user-visible changes when the package version is already at -wip).

Breaking changes ✔️
Package Change Current Version New Version Needed Version Looking good?
markdown Non-Breaking 7.3.1 7.4.1-wip 7.4.1-wip ✔️

This check can be disabled by tagging the PR with skip-breaking-check.

API leaks ✔️

The following packages contain symbols visible in the public API, but not exported by the library. Export these symbols or remove them from your publicly visible API.

Package Leaked API symbol Leaking sources

This check can be disabled by tagging the PR with skip-leaking-check.

Unused Dependencies ✔️
Package Status
markdown ✔️ All dependencies utilized correctly.

For details on how to fix these, see dependency_validator.

This check can be disabled by tagging the PR with skip-unused-dependencies-check.

License Headers ✔️
// 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.

Files
no missing headers

All source files should start with a license header.

Unrelated files missing license headers
Files
pkgs/html/test/differential_test.dart
pkgs/html/test/dom_parsing_test.dart
pkgs/html/test/mxss_test.dart
pkgs/html/test/noah_ark_clause_limit_test.dart

This check can be disabled by tagging the PR with skip-license-check.

Coverage ✔️
File Coverage
pkgs/markdown/lib/src/ast.dart 💚 89 % ⬆️ 4 %

This check for test coverage is informational (issues shown here will not fail the PR).

This check can be disabled by tagging the PR with skip-coverage-check.

@palmoni5
palmoni5 force-pushed the palmoni5/markdown-iterative-ast-text-20261111 branch from 41869ce to 3be7fea Compare November 11, 2026 17:08
@palmoni5
palmoni5 merged commit ed94f55 into main Nov 11, 2026
17 checks passed
@palmoni5
palmoni5 deleted the palmoni5/markdown-iterative-ast-text-20261111 branch November 11, 2026 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant