Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions doc/10_metrics/ui/web_perf_round7b_2026-09-13.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Web-rendering perf round 7b: HTML tokenizer byte-array scan (2026-09-13)

Continuation of `doc/10_metrics/ui/web_perf_round6_2026-09-13.md`, which left
`pp_html_us` (HTML tokenize + tree build) as the largest parse-phase bucket
(1427-1544 ms summed over the 8-page catalog) and recorded a REVERTED
`.bytes()`-based candidate: it regressed because `_find_char`/`_find_substr`
called `s.bytes()` on the *whole remaining document* on every single
invocation, redecoding O(document length) repeatedly across the scan instead
of decoding once. The round-6 doc named the real fix: "threading a
document-wide byte array through the tokenizer's whole scan loop so it is
decoded once per document rather than once per `_find_char` call" — that is
exactly what this round implements.

## Fix

`src/lib/gc_async_mut/gpu/browser_engine/html_tokenizer.spl`,
`html_tokenizer_tokenize_with_limits` (main scan loop): decode
`val hb = text_to_bytes(html)` **once** at the top of the function, then
thread that same `[u8]` array through the whole scan instead of re-slicing
or re-decoding per character or per helper call:

- The outer loop's per-position dispatch (`ch == "<"`, `ch == "&"`, peeking
`next`) now reads `hb[pos]`/`hb[pos+1]` as integer bytes instead of
allocating a length-1 `text` via `html.slice(pos, pos+1)` on every position.
- New byte-array twins `_find_byte`, `_scan_char_data_b`, `_find_tag_close_b`,
`_is_alpha_byte` replace the text-based `_find_char(html, ">", ...)`,
`_scan_char_data(html, ...)`, `_find_tag_close(html, ...)`,
`_is_alpha_char(next)` call sites inside the hot outer loop — each now
indexes the single pre-decoded `hb` array (O(1) per probe) instead of
allocating a new `text` slice per character compared.
- The original text-based `_find_char`/`_find_tag_close`/`_scan_char_data`/
`_is_alpha_char` functions are kept unchanged and still used by the
bounded, per-tag-sized call sites (`_parse_attrs`'s quoted-value search,
raw-text `&`-scan on an already-sliced `raw_data`) where they were never
the O(document) bottleneck.
- `_find_substr` (comment-close `-->` search) and `_find_raw_end_tag`
(script/style/textarea end-tag search) are left as-is: comments and raw-text
elements are rare relative to the character-data/tag-open scan this fix
targets, and round-6 already showed `_find_raw_end_tag`-style multi-byte
compares are not the dominant cost.

Token output is unchanged — this is a pure scan-mechanism change, no token
semantics moved.

## A/B evidence (8-page catalog, `pipeline_bench.spl`, this host)

4 alternating pairs, each running `SIMPLE_WEB_PHASE_TRACE=1` and summing
`pp_html_us` across the 8 catalog pages; Draw-IR digest is the sha256 of the
8 concatenated `[bench] digest` lines:

| pair | pp_html_us before (ms) | pp_html_us after (ms) | delta | digest before | digest after |
|---|---|---|---|---|---|
| 1 | 1363.5 | 1288.2 | -5.5% | `793b167fdefd` | `793b167fdefd` |
| 2 | 1386.3 | 1322.2 | -4.6% | `793b167fdefd` | `793b167fdefd` |
| 3 | 1432.5 | 1440.7 | +0.6% | `793b167fdefd` | `793b167fdefd` |
| 4 | 1550.0 | 1391.8 | -10.2% | `793b167fdefd` | `793b167fdefd` |

3 of 4 pairs improved (4.6-10.2%); pair 3 is flat within host-load noise (this
box was shared, same noise band as round 6's ±100 ms swing on the unmodified
baseline itself). Net direction is a real, if modest, win — not a regression,
unlike round 6's two candidates. Draw-IR digest is byte-identical (12-hex
prefix shown; full sha256 matches) across all 8 pairs both sides — the token
stream is unchanged, only the scan mechanism.

## Correctness

- `test/01_unit/browser_engine/html_tokenizer_abrupt_comment_spec.spl`: 4/4
pass, identical both sides.
- `test/unit/browser_engine/html_tokenizer_spec.spl`: 17/17 pass, identical
both sides.
- `test/01_unit/browser_engine/html_tokenizer_spec.spl`: 23/53 pass, 30/53
fail on **both** the pre-change and post-change tree — confirmed
pre-existing (baseline failure, not introduced by this change) by diffing
the exact `✗` example list byte-for-byte between the two trees: identical.
- GPU boundary audit
(`SIMPLE_BIN=.../simple sh scripts/check/check-web-vulkan-gpu-boundary-audit.shs`):
`PASS — 2 frame(s) audited, host_pixel_iterations=0, readbacks_per_frame<=1,
submits_per_frame<=1`.

## Scope note

`sel_match`/selector matching is untouched — round 6 identified it as the
single largest style-stage leaf, but it belongs to a different lane per this
round's contract. This round is `pp_html_us` only.
84 changes: 68 additions & 16 deletions src/lib/gc_async_mut/gpu/browser_engine/html_tokenizer.spl
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,20 @@ pub fn html_tokenizer_tokenize_with_limits(
var pos = 0
val html = t.html
val n = html.len()
# Decode the WHOLE document to bytes exactly once. The main scan loop and
# its `_find_*_b`/`_scan_char_data_b` helpers below index into this same
# array by byte offset instead of each re-decoding `html.bytes()` (or
# allocating a length-1 `text` via `html.slice(i, i+1)`) per character —
# see doc/10_metrics/ui/web_perf_round6_2026-09-13.md for the regression
# this avoids (a prior attempt called `.bytes()` inside `_find_char`
# itself, redecoding the full remaining document on every invocation).
val hb = text_to_bytes(html)
var retained_attrs = 0
# Raw-text mode for script/style and RCDATA mode for textarea.
var raw_text_tag = ""
var raw_text_decodes_entities = false
while pos < n and not sink.truncated:
val ch = html.slice(pos, pos + 1)
val chb = hb[pos]
if raw_text_tag != "":
val current_raw_tag = raw_text_tag
val found_end = _find_raw_end_tag(html, current_raw_tag, pos)
Expand Down Expand Up @@ -213,23 +221,23 @@ pub fn html_tokenizer_tokenize_with_limits(
else:
pos = found_end
# Emit the end tag
val close_end = _find_char(html, ">", pos)
val close_end = _find_byte(hb, n, 62, pos)
if close_end < 0:
pos = n
else:
pos = close_end + 1
val _ = sink.push(_tok_end(current_raw_tag))
raw_text_tag = ""
raw_text_decodes_entities = false
elif ch == "<":
elif chb == 60:
# Peek at next char
if pos + 1 >= n:
# Lone < at end — emit as character
val _ = sink.push(_tok_char("<"))
pos = pos + 1
else:
val next = html.slice(pos + 1, pos + 2)
if next == "!":
val nextb = hb[pos + 1]
if nextb == 33:
# Comment or doctype
if pos + 3 < n and html.slice(pos + 2, pos + 4) == "--":
# Comment: <!-- ... -->
Expand Down Expand Up @@ -261,7 +269,7 @@ pub fn html_tokenizer_tokenize_with_limits(
pos = comment_end + 3
elif pos + 8 < n and html.slice(pos + 2, pos + 9).lower() == "doctype":
# Doctype
val gt = _find_char(html, ">", pos)
val gt = _find_byte(hb, n, 62, pos)
if gt < 0:
pos = n
else:
Expand All @@ -271,14 +279,14 @@ pub fn html_tokenizer_tokenize_with_limits(
pos = gt + 1
else:
# Unknown <! sequence — skip to >
val gt2 = _find_char(html, ">", pos)
val gt2 = _find_byte(hb, n, 62, pos)
if gt2 < 0:
pos = n
else:
pos = gt2 + 1
elif next == "/":
elif nextb == 47:
# End tag
val gt = _find_char(html, ">", pos + 2)
val gt = _find_byte(hb, n, 62, pos + 2)
if gt < 0:
val _ = sink.push(_tok_char("<"))
pos = pos + 1
Expand All @@ -287,22 +295,22 @@ pub fn html_tokenizer_tokenize_with_limits(
val tag_name = _strip_tag_name(tag_raw)
val _ = sink.push(_tok_end(tag_name))
pos = gt + 1
elif next == "?":
elif nextb == 63:
# Processing instruction — skip to >
val gt3 = _find_char(html, ">", pos)
val gt3 = _find_byte(hb, n, 62, pos)
if gt3 < 0:
pos = n
else:
pos = gt3 + 1
elif _is_alpha_char(next):
elif _is_alpha_byte(nextb):
# Start tag
val gt4 = _find_tag_close(html, pos + 1)
val gt4 = _find_tag_close_b(hb, n, pos + 1)
if gt4 < 0:
val _ = sink.push(_tok_char("<"))
pos = pos + 1
else:
val self_closing = (
gt4 > 0 and html.slice(gt4 - 1, gt4) == "/"
gt4 > 0 and hb[gt4 - 1] == 47
)
val inner = html.slice(pos + 1, if self_closing: gt4 - 1 else: gt4).trim()
val parsed = _parse_start_tag(
Expand All @@ -326,14 +334,14 @@ pub fn html_tokenizer_tokenize_with_limits(
# Stray < — emit as character
val _ = sink.push(_tok_char("<"))
pos = pos + 1
elif ch == "&":
elif chb == 38:
# Named entity reference
val entity_result = _parse_entity(html, pos, false)
val _ = sink.push(_tok_char(entity_result.decoded))
pos = entity_result.end_pos
else:
# Regular character data — accumulate until < or &
val end_pos = _scan_char_data(html, pos)
val end_pos = _scan_char_data_b(hb, n, pos)
val char_data = html.slice(pos, end_pos)
if char_data.len() > 0:
val _ = sink.push(_tok_char(char_data))
Expand Down Expand Up @@ -678,6 +686,50 @@ fn _decode_character_references(value: text) -> text:
# String scanning utilities
# =========================================================================

fn _find_byte(hb: [u8], n: i32, target: u8, start: i32) -> i32:
"""Find first occurrence of byte `target` in `hb` (already decoded once
by the caller) starting at `start`. Returns -1 if not found. Indexing an
already-decoded byte array is O(1) per probe, unlike re-slicing `text`
or re-decoding `.bytes()` per call — see the round-6 regression note at
`html_tokenizer_tokenize_with_limits` above."""
var i = start
while i < n:
if hb[i] == target:
return i
i = i + 1
-1

fn _scan_char_data_b(hb: [u8], n: i32, start: i32) -> i32:
"""Byte-array twin of `_scan_char_data`: scan from start until < or &."""
var i = start
while i < n:
val c = hb[i]
if c == 60 or c == 38:
return i
i = i + 1
n

fn _find_tag_close_b(hb: [u8], n: i32, start: i32) -> i32:
"""Byte-array twin of `_find_tag_close`: find the closing > of a tag,
respecting quoted attribute values."""
var i = start
var in_quote: u8 = 0
while i < n:
val c = hb[i]
if in_quote != 0:
if c == in_quote:
in_quote = 0
elif c == 34 or c == 39:
in_quote = c
elif c == 62:
return i
i = i + 1
-1

fn _is_alpha_byte(c: u8) -> bool:
"""True if byte c is an ASCII letter a-z or A-Z."""
(c >= 97 and c <= 122) or (c >= 65 and c <= 90)

fn _find_char(s: text, ch: text, start: i32) -> i32:
"""Find first occurrence of single char ch in s starting at start. Returns -1 if not found."""
val n = s.len()
Expand Down
Loading