Skip to content

fix(style): resolve rem units against the root element font size - #2771

Open
lanyue-llk wants to merge 1 commit into
mainfrom
fix/rem-root-font-size
Open

lanyue-llk wants to merge 1 commit into
mainfrom
fix/rem-root-font-size

Conversation

@lanyue-llk

@lanyue-llk lanyue-llk commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

PR #2771 at a glance

**Track:** Trajectory Replay (Browser Eval). Replaces part of #756.

Closes #2761.

Problem. rem (and rlh) always resolved against the 16px default, ignoring the root element's font size, so any page using html{font-size:62.5%} laid out at the wrong scale in computed style, geometry, screenshots and PDF.
moli fetch --layout 'data:text/html,<style>html{font-size:10px} %23s{width:2rem;height:1px}</style><div id=s></div>' --eval 'JSON.stringify({w:getComputedStyle(s).width,r:s.getBoundingClientRect().width})' printed {"w":"32px","r":32}; Chromium prints {"w":"20px","r":20}.

Reproduce (steps from #2761)
moli fetch --layout 'data:text/html,<style>html{font-size:10px} %23s{width:2rem;height:1px}</style><div id=s></div>' \
  --eval 'JSON.stringify({w:getComputedStyle(s).width,r:s.getBoundingClientRect().width})'

Expected (Chromium)

{"w":"20px","r":20}. After document.documentElement.style.fontSize = "20px", the width becomes 40px.

Actual

{"w":"32px","r":32}. After the root font-size change it is still 32px, even though getComputedStyle(document.documentElement).fontSize reports 20px.

Change. Stylo updates the Device's root font size, line height and root style only during a full traversal (matching.rs); Moli resolves elements one at a time with resolve_style, so the Device kept its defaults. publish_resolved_element_styles (style_engine/computed.rs), which every resolution path already goes through, now mirrors that update when it publishes the root element's style, using a small synchronize_root_style_device helper (style_engine/retained.rs, taken from the closed #756). The retained style system remembers the last root style (state.rs) and replays it onto a rebuilt Device after a viewport/environment change, so elements resolved afterwards do not fall back to 16px.

Verification.

  • Issue repro, v1.1.23 release binary: {"w":"32px","r":32}. This branch: {"w":"20px","r":20}.
  • Root size changed by a page script (<script>document.documentElement.style.fontSize="20px"</script>), then s.getBoundingClientRect().width: v1.1.23 [32], this branch [40].
  • New test computed_size_rem_lengths_follow_root_font_size_across_device_rebuilds covers the initial value, a later root change and a new element after a viewport-driven Device rebuild.
  • cargo fmt --all clean; cargo clippy -p moli-renderer-v8 --all-targets --all-features -- -D warnings clean (workspace clippy not run locally).
  • cargo nextest run -p moli-renderer-v8 -E 'test(computed_size) | test(registered_properties) | test(/style_engine::tests::lifecycle/)': 106 passed.
  • Not run locally: the full workspace nextest (the renderer test binary for the whole workspace does not link within local RAM); left to CI.
  • Note: in moli fetch --eval, changing the root size inside the --eval expression and then reading geometry still returns the previous layout. The same happens for s.style.width="50px", so it is a separate stale-layout behaviour of --eval and not part of this fix.

Cost. The added work is one ComputedValueFlags::IS_ROOT_ELEMENT_STYLE check per published element style. The Device sync and the retained Arc clone happen only when the root element's style is published, and the replay happens only on a Device rebuild. No new per-element state is kept: there is one retained Arc<ComputedValues> per Document.
Measured with /usr/bin/time -l on debug builds of origin/main (cbf4d01) and this branch, same profile. Each figure is the median of 5 runs, with the two binaries run in interleaved rounds:

case main wall / max RSS branch wall / max RSS
issue repro, --layout --eval (round 1) 0.08 s / 89.5 MB 0.08 s / 89.6 MB
issue repro, --layout --eval (round 2) 0.08 s / 89.5 MB 0.08 s / 89.7 MB
ordinary local article (60 sections), --layout --dump markdown (round 1) 0.13 s / 91.3 MB 0.13 s / 91.3 MB
ordinary local article, --layout --dump markdown (round 2) 0.13 s / 91.3 MB 0.13 s / 91.3 MB

@lanyue-llk lanyue-llk left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent review of 89b22cd

No blocking findings

What I checked, against origin/main:

  • Root cause. Moli has no full Stylo traversal. All three resolution paths (resolve_element_styles, prepare_style_observation_targets, materialize_ancestor_styles_for_resolution) are the only resolve_style/StyleResolverForElement callers, and they all go through publish_resolved_element_styles. So the Device update from matching.rs (set_root_style, root font size, root line height, root metrics when used) now runs on every route. The root's own rem comes from the cascade (initial size) and not from the Device, so the root resolving against the old Device value does no harm.
  • Child documents and iframes. world_for_document returns a per-Document world, and each world has its own RetainedStyleSystem with its own Stylist, Device and root_style. IS_ROOT_ELEMENT_STYLE is set only when is_root() holds (parent node is a Document, moli-selector/src/stylo/style_traversal.rs:2363). An iframe's <html> therefore updates only its own Device, and nothing leaks across documents.
  • Shadow roots. Children of a shadow root are not is_root(). Their traversal_parent chain reaches the document root, so they share the Document's Device, which matches the spec (rem uses the document root).
  • Stale descendants. A root style change makes the root a lazy invalidation root, so its whole subtree carries the new generation. Any later read re-resolves the root first, and force_descendants/inherited_change then re-resolves the descendants along the path. The test's s going from 20px to 40px exercises this. After a Device rebuild, set_device invalidation re-resolves the root wherever media rules change it. Otherwise the replayed root_style is still the root's current published style.
  • Stale or leaked root_style. The field is per-Document and is overwritten on every root publish. A full rebuild (build_retained_style_system) resets it to None together with clearing element data. A Device rebuilt in place replays it, and computed.rs publish_resolved_element_styles refreshes it the next time the root is resolved.
  • Cost. The added work is a few atomic stores and one line-height calculation (servo calc_line_height is arithmetic), and it runs only when the root element resolves. Font metrics are queried only when used_root_font_metrics() is set. Memory grows by one Arc clone of the root style per Document.
  • Test. Without the fix, s reads 32px and t reads 48px, so the test fails on main. The expected values match Chromium.
  • CI. Format, Clippy, Nextest, Build release (HEAD), CDP smoke (Chromium), Python/Node harness and Windows storage pass. The differential, PDF, WebDriver, Spider and benchmark jobs were still pending at review time.

Pending (others are adding these): the body's Cost section and its picture (only the <!-- picture --> placeholder is there now).

@lanyue-llk

Copy link
Copy Markdown
Contributor Author

CI note: the failing "Native PDF rendering and links" job is not caused by this PR. It is a regression on main (cbf4d0149f), where wrapped links get one PDF link annotation per glyph. It is filed with a reproduction as #2803: v1.1.23 produces 1 rect for the fixture, while a cbf4d0149f build produces 13. Any PR based on current main hits it.

@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

CI Regression Report

Source CI run · source state at render: in_progress · artifacts: 6/6

Check Status Signal
Release regression ✅ HEAD/base failures 0/0; raw binary +0.000291%
Frontend differential ✅ 1,020/1,020 cases matched; 0 issues
Agent episodes ✅ 8/8 Moli episodes passed; 0 failures
Runtime/CDP contracts ✅ 26 contract cases; 0 failures
CDP smoke ✅ 118/118 groups passed; 1,754 scenarios
WebMainBench ✅ 545/545 completed; 0 unexpected failures
Release regression — ✅ HEAD/base failures 0/0; raw binary +0.000291%

Package and image size

Metric Base HEAD Delta Delta %
Raw binary 154.52 MiB 154.52 MiB +472 B +0.000291%
Stripped binary 106.93 MiB 106.93 MiB 0 B 0.00%
gzip binary 53.41 MiB 53.42 MiB +166 B +0.000296%
Rootfs 161.43 MiB 161.43 MiB 0 B 0.00%
gzip rootfs 62.51 MiB 62.51 MiB +906 B +0.001382%

Startup latency and PSS

Case Base p50 HEAD p50 Delta Base PSS HEAD PSS Delta
Serve ready 26.98 ms 26.97 ms −0.01 ms 4.84 MiB 5.02 MiB +182.00 KiB
CDP first page 47.97 ms 51.23 ms +3.26 ms 4.07 MiB 5.35 MiB +1.27 MiB
CDP warm page 11.31 ms 12.08 ms +0.77 ms 53.00 MiB 53.23 MiB +235.00 KiB
CLI about:blank 12.13 ms 13.69 ms +1.56 ms 4.03 MiB 4.36 MiB +341.50 KiB
CLI local JS 27.15 ms 25.99 ms −1.16 ms 4.56 MiB 5.96 MiB +1.40 MiB

Concurrency matrix

Concurrency Base p50 HEAD p50 Delta Base PSS HEAD PSS Delta HEAD failures Unstable B/H
1 22.39 ms 24.44 ms +2.06 ms 1.21 MiB 916.00 KiB −319.00 KiB 0 0/0
5 27.68 ms 27.80 ms +0.12 ms 13.02 MiB 13.58 MiB +574.00 KiB 0 0/0
10 27.78 ms 28.58 ms +0.80 ms 13.00 MiB 12.84 MiB −169.00 KiB 0 0/0
25 28.24 ms 27.10 ms −1.14 ms 12.16 MiB 13.95 MiB +1.79 MiB 0 0/0
100 28.39 ms 29.77 ms +1.38 ms 13.16 MiB 13.06 MiB −102.00 KiB 0 0/0
Frontend differential — ✅ 1,020/1,020 cases matched; 0 issues
Match DOM mismatch Diagnostic mismatch Moli error Reference error Infrastructure error Mismatched frames Duration
1,020 0 0 0 0 0 0 7m 4.41s
Agent episodes · Moli vs Chromium — ✅ 8/8 Moli episodes passed; 0 failures
Fast contract Episodes Passed Assertions Ready p50 Episode p50
Moli 8 8 47/47 29.49 ms 31.62 ms
Chromium 8 8 47/47 984.37 ms 106.33 ms
Operation p50 Chromium Moli Delta Delta %
navigate 43.62 ms 6.08 ms −37.54 ms −86.05%
observe 1.58 ms 0.99 ms −0.60 ms −37.66%
fill 3.16 ms 3.73 ms +0.57 ms +17.95%
click 6.07 ms 4.89 ms −1.18 ms −19.44%
Idle-resource episode Peak RSS Peak PSS Average CPU Peak CPU Peak processes Sampler
Moli 70.42 MiB 66.61 MiB 0.76% 10.01% 1 ✅
Chromium 1310.71 MiB 434.34 MiB 1.25% 22.00% 13 ⚠️ PSS partial
Runtime and CDP session contracts — ✅ 26 contract cases; 0 failures
Suite Cases Failures Median case p50 Median case p95 Median peak PSS p50
Synthetic fetch 13 0 23.95 ms 25.19 ms 633.00 KiB
Long-lived CDP session 13 0 17.78 ms 18.88 ms —
CDP smoke — ✅ 118/118 groups passed; 1,754 scenarios

Workers: 4 · cumulative group time: 489.43 s · failed groups: 0

Slowest group Scenarios Duration Status
fetch-resource-dispatch 72 23.87 s ✅
fetch-resource-dispatch 72 23.65 s ✅
dom-input 62 23.47 s ✅
dom-input 62 23.21 s ✅
scrollbar-visibility 22 21.48 s ✅
WebMainBench · 545 pages — ✅ 545/545 completed; 0 unexpected failures
Completed Successful Expected DNS failure Unexpected failures Panics Crashes Timeouts Empty output
545/545 544 1 0 0 0 0 0

Frozen HTML · Linux · --wait done · 45 seconds per page · no retries. Content quality is evaluated separately.

Diagnostics: webmainbench-results — every page's Markdown, stderr, and exit status. Source run and artifacts

All artifact fields are parsed by the trusted default-branch renderer; missing or invalid inputs remain visible as unavailable.

@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Spider Bench A/B

✅ All benchmark browser service runs completed; results are informational.

Public HEAD: 184 / 240 rows; 38 / 48 sites produced rows; 10 unexpected empty sites.

✅ Deterministic fixture contract is clean.

Common ancestor 16861c2e39e8 → HEAD 1b89e15c8a56; benchmark order: head-first; workflow: success.

Public 48-site run · informational

Metric Base HEAD Δ
Run status success success —
Extracted rows / contract 184 / 240 184 / 240 0 (0.00%)
Contract fill 76.67% 76.67% 0.00 pp (0.00%)
Sites with rows 38 / 48 38 / 48 0 (0.00%)
Sites meeting row contract 36 / 48 36 / 48 0 (0.00%)
Unexpected empty sites 10 10 0 (0.00%)
Partial-row sites 2 2 0 (0.00%)
Site outcomes extracted: 37; HTTP error + rows: 1; empty: 8; HTTP error + empty: 1; snapshot error + empty: 1 extracted: 36; HTTP error + rows: 2; empty: 8; HTTP error + empty: 1; snapshot error + empty: 1 —
Observed duration 51.36 s 55.05 s +3.69 s (+7.19%)
Estimated CPU 41.17 s 39.43 s -1.75 s (-4.24%)
Peak PSS 231.08 MiB 241.83 MiB +10.76 MiB (+4.66%)
Peak RSS 234.59 MiB 245.39 MiB +10.80 MiB (+4.60%)
Peak threads 31 30 -1 (-3.23%)
Mismatched sites 0 0 0
Timeout with items 0 0 0
Category rows Base HEAD Δ
news 35 / 40 35 / 40 0
stocks 20 / 40 20 / 40 0
tech 35 / 40 35 / 40 0
sports 27 / 40 27 / 40 0
games 32 / 40 32 / 40 0
life 35 / 40 35 / 40 0

Public-site content, timing, and memory are noisy. This single A/B run reports evidence only; site outcome counts explain missing rows without treating them as a deterministic regression.

CPU and memory timelines

These are bounded 40-point views of the same complete process-tree samples used by the HTML report. CPU uses 100% per occupied logical core; memory lines are RSS first and PSS second when complete PSS samples are available.

Base

xychart-beta
    title "Base CPU"
    x-axis "Elapsed seconds" [1.037, 1.537, 3.039, 4.542, 5.544, 7.046, 8.547, 10.049, 11.551, 13.053, 14.554, 15.556, 17.057, 18.56, 20.062, 21.563, 23.065, 24.567, 26.069, 27.071, 28.572, 30.074, 31.576, 33.078, 34.581, 36.083, 36.583, 37.084, 38.586, 40.087, 41.587, 43.09, 44.591, 46.093, 47.094, 48.096, 48.596, 50.099]
    y-axis "CPU percent" 0 --> 200
    line [0, 4, 105.85, 139.7, 127.63, 99.76, 123.85, 107.83, 121.94, 87.94, 87.98, 3.99, 53.99, 83.97, 119.88, 121.85, 119.89, 39.96, 107.86, 93.75, 69.92, 29.98, 57.95, 123.85, 17.96, 129.66, 23.98, 25.98, 55.95, 29.97, 2, 29.96, 7.99, 13.99, 127.78, 163.69, 113.89, 141.57]
Loading
xychart-beta
    title "Base memory: RSS then PSS"
    x-axis "Elapsed seconds" [0.036, 1.037, 1.537, 3.039, 4.542, 5.544, 7.046, 8.547, 10.049, 11.551, 13.053, 14.554, 15.556, 17.057, 18.56, 20.062, 21.563, 23.065, 24.567, 26.069, 27.071, 28.572, 30.074, 31.576, 33.078, 34.581, 36.083, 36.583, 37.084, 38.586, 40.087, 41.587, 43.09, 44.591, 46.093, 47.094, 48.096, 48.596, 50.099, 51.364]
    y-axis "Memory MiB" 0 --> 500
    line [13.63, 57.1, 63.61, 121.55, 126.23, 140.96, 143.95, 160.04, 156.23, 159.84, 126.5, 152.75, 122.61, 125.47, 154.35, 160.46, 173.07, 194.64, 145.68, 161.68, 154.56, 147.09, 139.25, 156.21, 233.2, 225.37, 232.93, 234.59, 230.58, 234.35, 230.02, 230.08, 150.41, 148.21, 136.59, 155.11, 142.59, 152.63, 157.27, 0]
    line [10.31, 53.63, 60.15, 118.02, 121.46, 137.56, 139.77, 155.97, 152, 156.32, 122.98, 149.38, 119.1, 121.93, 150.85, 156.96, 169.55, 191.19, 140.77, 158.1, 151.04, 143.64, 135.75, 152.72, 229.69, 221.85, 229.42, 231.08, 227.06, 230.83, 226.5, 226.56, 146.89, 144.7, 131.97, 151.64, 139.07, 149.12, 153.84, 0]
Loading

HEAD

xychart-beta
    title "HEAD CPU"
    x-axis "Elapsed seconds" [1.037, 1.538, 3.04, 4.041, 4.541, 6.041, 7.542, 9.042, 10.544, 12.046, 14.049, 15.552, 17.055, 18.556, 20.056, 21.557, 23.059, 24.56, 26.062, 27.564, 29.066, 30.568, 32.07, 33.57, 35.072, 36.572, 38.073, 39.574, 41.576, 43.079, 44.581, 46.083, 47.584, 49.086, 50.589, 51.591, 52.09, 53.593]
    y-axis "CPU percent" 0 --> 200
    line [0, 25.94, 113.99, 91.85, 139.86, 127.99, 102.01, 128, 137.69, 73.97, 13.97, 5.99, 151.58, 0, 14, 85.82, 107.88, 102.02, 101.89, 69.9, 102.05, 95.9, 99.9, 15.99, 35.98, 117.96, 61.97, 99.81, 25.99, 1.99, 27.93, 40, 13.98, 0, 115.84, 153.82, 112.03, 75.95]
Loading
xychart-beta
    title "HEAD memory: RSS then PSS"
    x-axis "Elapsed seconds" [0.037, 1.037, 1.538, 3.04, 4.041, 4.541, 6.041, 7.542, 9.042, 10.544, 12.046, 14.049, 15.552, 17.055, 18.556, 20.056, 21.557, 23.059, 24.56, 26.062, 27.564, 29.066, 30.568, 32.07, 33.57, 35.072, 36.572, 38.073, 39.574, 41.576, 43.079, 44.581, 46.083, 47.584, 49.086, 50.589, 51.591, 52.09, 53.593, 55.055]
    y-axis "Memory MiB" 0 --> 500
    line [13.6, 57.71, 87.41, 121.53, 132.34, 139.08, 149.68, 135.78, 146.93, 137.2, 160.43, 118.38, 117.12, 146.12, 118.41, 118.12, 136.94, 129.87, 206.13, 184, 132.39, 166.36, 136.81, 141.02, 130.8, 134.89, 245.39, 226.86, 231.83, 229.02, 233.3, 231.73, 239.27, 133.75, 132.58, 167.11, 161.72, 146.18, 145.67, 0]
    line [10.24, 54.18, 83.88, 118.27, 128.82, 135.66, 146.11, 132.18, 143.38, 133.64, 157.96, 114.81, 113.55, 142.56, 114.85, 114.55, 133.43, 126.33, 202.58, 180.43, 128.82, 163.26, 133.22, 137.43, 127.23, 130.18, 241.83, 223.31, 228.33, 225.45, 229.74, 228.17, 235.54, 130.18, 129.01, 163.55, 158.22, 142.63, 141.98, 0]
Loading

Deterministic fixture · required contract

The fixture exercises 15 routes: 8 are expected to emit rows and 7 intentionally exercise empty, loading, or timeout behavior.

Metric Base HEAD Δ
Run status success success —
Extracted rows / contract 40 / 40 40 / 40 0 (0.00%)
Contract fill 100.00% 100.00% 0.00 pp (0.00%)
Sites with rows 8 / 15 8 / 15 0 (0.00%)
Sites meeting row contract 15 / 15 15 / 15 0 (0.00%)
Unexpected empty sites 0 0 0
Partial-row sites 0 0 0
Site outcomes extracted: 7; HTTP error + rows: 1; empty: 3; timeout + empty: 4 extracted: 7; HTTP error + rows: 1; empty: 3; timeout + empty: 4 —
Observed duration 19.88 s 19.87 s -0.01 s (-0.05%)
Estimated CPU 0.45 s 0.42 s -0.03 s (-6.21%)
Peak PSS 70.00 MiB 70.05 MiB +0.05 MiB (+0.07%)
Peak RSS 73.42 MiB 73.48 MiB +0.06 MiB (+0.08%)
Peak threads 21 21 0 (0.00%)
Mismatched sites 0 0 0
Timeout with items 0 0 0

Full HTML, JSON, CSV, logs, and page snapshots: workflow run and full spider-bench-results artifact.

The public-web A/B is informational. The exact HEAD fixture contract runs as a separate required CI check; timing and public-site content deltas remain non-blocking.

@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Sequential Navigation Soak A/B

✅ HEAD completed the 200-navigation resilience and memory observation.

One browser process, one target, and one CDP session navigate CSDN → SegmentFault → Huaban → example.com repeatedly. This run issued 200 Page.navigate commands per binary.

Common ancestor 16861c2e39 → HEAD 1b89e15c8a; benchmark order: head-first; workflow: success.

Session resilience

Metric Base HEAD
Attempted / planned 200 / 200 200 / 200
Direct observable pass 198 / 200 193 / 200
Failures / recovered 2 / 2 7 / 7
Recovery failures 0 0
Lifecycle order violations 0 0
Network order violations 0 0
Wall time 797.7 s 788.8 s

A public-page failure is reported separately from an unrecoverable session. The soak requires all navigation attempts, zero failed recovery, zero lifecycle/network ordering violations, and complete resource evidence.

Process-tree memory

Metric Base HEAD Δ
Boundary samples 200 200 0
Boundary RSS samples 200 200 0
Boundary PSS samples 200 200 0
Periodic peak RSS 253.7 MiB 250.5 MiB -3.3 MiB
Periodic peak PSS 251.1 MiB 248.3 MiB -2.8 MiB
RSS · first 10 avg 181.2 MiB 174.3 MiB -6.9 MiB
RSS · last 10 avg 191.2 MiB 189.7 MiB -1.5 MiB
RSS · last − first window +10.0 MiB +15.3 MiB +5.3 MiB
RSS · warm slope / 100 nav +7.2 MiB -0.5 MiB -7.6 MiB
PSS · first 10 avg 178.3 MiB 171.2 MiB -7.1 MiB
PSS · last 10 avg 188.3 MiB 186.7 MiB -1.5 MiB
PSS · last − first window +10.0 MiB +15.5 MiB +5.5 MiB
PSS · warm slope / 100 nav +7.3 MiB -0.5 MiB -7.7 MiB
Peak file descriptors 65 65 0
Peak threads 32 32 0

HEAD memory by 50-navigation quarter

HEAD range Avg RSS Final RSS Peak RSS Avg PSS Final PSS Peak FD
1–50 177.4 MiB 158.0 MiB 219.4 MiB 174.5 MiB 155.8 MiB 58
51–100 179.9 MiB 131.1 MiB 230.0 MiB 177.2 MiB 128.1 MiB 59
101–150 175.4 MiB 160.8 MiB 207.5 MiB 173.0 MiB 158.0 MiB 61
151–200 182.5 MiB 136.6 MiB 211.9 MiB 179.7 MiB 133.9 MiB 59

Raw reports and per-navigation boundary samples: workflow run and full sequential-navigation-soak-results artifact.

Public-site timing and memory are observational. Use the A/B deltas and trend shape as evidence, not as a deterministic performance threshold.

@lanyue-llk
lanyue-llk force-pushed the fix/rem-root-font-size branch from 471c1ed to 1b89e15 Compare November 13, 2026 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rem units ignore the root element font-size and always resolve against 16px

1 participant