Skip to content

Implement a nesting limit - #811

Merged
mrobinson merged 1 commit into
servo:mainfrom
notriddle:nesting-limit
Nov 3, 2026
Merged

mrobinson merged 1 commit into
servo:mainfrom
notriddle:nesting-limit

Conversation

@notriddle

@notriddle notriddle commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Opening a document with 512 <i> tags in Firefox, as found in the included test case, shows the same DOM, including the correct text nesting and two <i> tags at the tip of the pyramid.

Fixes #788

Results of running the test are given below.

Before

<div>     n=  4000  bytes=  44001    51.818ms
<div>     n= 16000  bytes= 176001   459.037ms
<div>     n= 64000  bytes= 704001      7.551s
<ul><li>  n=  4000  bytes=  32001   115.165ms
<ul><li>  n= 16000  bytes= 128001      1.845s
<ul><li>  n= 64000  bytes= 512001     35.355s
<b>       n=  4000  bytes=  12001   814.333µs
<b>       n= 16000  bytes=  48001     2.981ms
<b>       n= 64000  bytes= 192001    12.490ms

After

<div>     n=  4000  bytes=  44001     8.136ms
<div>     n= 16000  bytes= 176001    33.434ms
<div>     n= 64000  bytes= 704001   134.578ms
<ul><li>  n=  4000  bytes=  32001    15.342ms
<ul><li>  n= 16000  bytes= 128001    62.561ms
<ul><li>  n= 64000  bytes= 512001   253.616ms
<b>       n=  4000  bytes=  12001   727.458µs
<b>       n= 16000  bytes=  48001     2.664ms
<b>       n= 64000  bytes= 192001    10.497ms

@github-actions github-actions Bot added the V-non-breaking A non-breaking change label Oct 31, 2026
Comment thread html5ever/src/tree_builder/mod.rs Outdated
///
/// Following the example set by Gecko and Blink,
/// html5ever limits the tree depth.
fn nesting_limit(&self) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: It might make sense to describe how the nesting limit is enforced in the rustdoc. Do other browsers take the same approach other than sharing a nesting limit? Maybe it makes sense to call this something like "pop_elements_until_nesting_limit_reached" as well, just to make it very clear what it does.

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.

I did some more investigation and found a spot where this disagrees with Firefox.

Instead of popping elements until the nesting limit is reached, I've copied Gecko implementation more closely, as described in whatwg/html#3732 (comment).

@github-actions github-actions Bot added V-non-breaking A non-breaking change and removed V-non-breaking A non-breaking change labels Nov 3, 2026
Opening a document with 512 `<i>` tags in Firefox,
as found in the included test case,
shows the same DOM, including the correct text nesting
and two `<i>` tags at the tip of the pyramid.

Fixes servo#788
Results of running the test are given below.

Before
------

    <div>     n=  4000  bytes=  44001    51.818ms
    <div>     n= 16000  bytes= 176001   459.037ms
    <div>     n= 64000  bytes= 704001      7.551s
    <ul><li>  n=  4000  bytes=  32001   115.165ms
    <ul><li>  n= 16000  bytes= 128001      1.845s
    <ul><li>  n= 64000  bytes= 512001     35.355s
    <b>       n=  4000  bytes=  12001   814.333µs
    <b>       n= 16000  bytes=  48001     2.981ms
    <b>       n= 64000  bytes= 192001    12.490ms

After
-----

    <div>     n=  4000  bytes=  44001     8.136ms
    <div>     n= 16000  bytes= 176001    33.434ms
    <div>     n= 64000  bytes= 704001   134.578ms
    <ul><li>  n=  4000  bytes=  32001    15.342ms
    <ul><li>  n= 16000  bytes= 128001    62.561ms
    <ul><li>  n= 64000  bytes= 512001   253.616ms
    <b>       n=  4000  bytes=  12001   727.458µs
    <b>       n= 16000  bytes=  48001     2.664ms
    <b>       n= 64000  bytes= 192001    10.497ms
@github-actions github-actions Bot added V-non-breaking A non-breaking change and removed V-non-breaking A non-breaking change labels Nov 3, 2026
@mrobinson
mrobinson added this pull request to the merge queue Nov 3, 2026
Merged via the queue into servo:main with commit 805ac27 Nov 3, 2026
11 checks passed
@notriddle
notriddle deleted the nesting-limit branch November 4, 2026 03:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

V-non-breaking A non-breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tree builder is O(n²) in nesting depth with no depth cap: 512 KB of <ul><li> takes 94 s

2 participants