Skip to content

Add dom-selector CSS engine once again - #3854

Merged
domenic merged 16 commits into
jsdom:mainfrom
asamuzaK:selector
Apr 19, 2025
Merged

domenic merged 16 commits into
jsdom:mainfrom
asamuzaK:selector

Conversation

@asamuzaK

@asamuzaK asamuzaK commented Apr 3, 2025 •

Copy link
Copy Markdown
Contributor

Ref #3667, #3644
This is another attempt to replace the CSS selector engine.
I believe there will be few performance issues this time.
However, do not merge until we can confirm there is no issue in a real project.
Until then, I would like you to publish a development release.

Issues fixed by this PR:
fix #2159, fix #3321, fix #3370, fix #3432, fix #3466,
fix #3469, fix #3544, fix #3603, fix #3610, fix #3612,
fix #3618, fix #3620, fix #3686, fix #3692, fix #3750,
fix #3780, fix #3792, fix #3802, fix #3818

@asamuzaK
asamuzaK marked this pull request as draft April 3, 2025 15:17
@asamuzaK

asamuzaK commented Apr 3, 2025 •

Copy link
Copy Markdown
Contributor Author

Benchmark results:

Selector A: main (nwsapi) B: PR (dom-selector) A/B
closest 19,367 ops/sec ±9.02% (66 runs sampled) 18,060 ops/sec ±4.97% (67 runs sampled) 1.07
matches 14,894 ops/sec ±5.70% (62 runs sampled) 13,673 ops/sec ±5.66% (63 runs sampled) 1.09
querySelector 1.51 ops/sec ±1.76% (8 runs sampled) 907 ops/sec ±6.19% (54 runs sampled) 0.002 (dom-selector is faster)
querySelectorAll 1.46 ops/sec ±1.69% (8 runs sampled) 1.22 ops/sec ±1.00% (7 runs sampled) 1.20
sizzle querySelector 18/273 fails. 0.46 ops/sec ±5.96% (6 runs sampled) 7/273 fails. 0.37 ops/sec ±4.76% (5 runs sampled) 1.35
sizzle querySelectorAll 18/273 fails. 0.49 ops/sec ±0.91% (6 runs sampled) 7/273 fails. 0.32 ops/sec ±4.96% (5 runs sampled) 1.53

@asamuzaK

asamuzaK commented Apr 3, 2025

Copy link
Copy Markdown
Contributor Author

The test failures appears to be related to #3154, not this PR.

@asamuzaK

asamuzaK commented Apr 3, 2025

Copy link
Copy Markdown
Contributor Author

Ref #3659
Tested locally with ariakit

jsdom + nwsapi

 Test Files  132 passed (132)
      Tests  665 passed (665)
   Start at  22:54:55
   Duration  545.00s (transform 8.73s, setup 124.53s, collect 2.44s, tests 1112.49s, environment 277.38s, prepare 39.44s)

jsdom + dom-selector

 Test Files  132 passed (132)
      Tests  665 passed (665)
   Start at  23:11:20
   Duration  595.00s (transform 8.69s, setup 128.84s, collect 2.57s, tests 1209.64s, environment 321.15s, prepare 39.84s)

@asamuzaK

asamuzaK commented Apr 5, 2025

Copy link
Copy Markdown
Contributor Author

Re: https://github.com/jsdom/jsdom/actions/runs/14283858797/job/40036577613#step:4:139

Err:58 http://security.ubuntu.com/ubuntu noble-updates/main amd64 liblzma-dev amd64 5.6.1+really5.4.5-1build0.1
  404  Not Found [IP: 52.252.75.106 80]
E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/x/xz-utils/liblzma-dev_5.6.1%2breally5.4.5-1build0.1_amd64.deb  404  Not Found [IP: 52.252.75.106 80]
Fetched 13.3 MB in 3s (4013 kB/s)
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
Error: Process completed with exit code 100.

It appears to have been changed to liblzma5_5.6.1+really5.4.5-1ubuntu0.2_amd64.deb

Not sure what to do here.

@asamuzaK

asamuzaK commented Apr 6, 2025

Copy link
Copy Markdown
Contributor Author

@domenic
I think I've done everything I can for now.
If there are no problems with the implementation, I'd like you to publish a pre-release package and have users to test with it.
What do you think?

@domenic

domenic commented Apr 8, 2025

Copy link
Copy Markdown
Member

I will try to work on this on my next jsdom weekend, although I have a pretty packed schedule for the next couple of weeks so it might be some time.

I will not be able to merge until we get CI passing, one way or another. I guess the problem is that Range-mutations-dataChange.html is failing, which is disappointing because I thought I made it significantly faster in 8da5ea8...f54e8e7 . I will want to do some local profiling to see if there is any major change between dom-selector and nwsapi for that test, I guess.

@@ -0,0 +1,377 @@
<!DOCTYPE html>

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.

Are these all from-scratch tests that we should work to upstream?? Or are derived from existing tests?

@asamuzaK asamuzaK Apr 8, 2025 •

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.

They are copied from /dom/nodes/
Should we rename them too?

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.

Why can't we just use the original dom/nodes ones?

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.

Both files are called within to-upstream/dom/nodes/ParentNode-querySelector-All-dont-upstream.html, to-upstream/dom/nodes/ParentNode-querySelector-All-xht-dont-upstream.xht.

<script src="selectors.js"></script>
<script src="ParentNode-querySelector-All.js"></script>

Is the following what you are suggesting?:

  • Change those two paths to absolute paths like
    <script src="/dom/nodes/selectors.js"></script>
    <script src="/dom/nodes/ParentNode-querySelector-All.js"></script>
    
  • In run-single-wpt.js, make modifications in the same way as /resources/testharness.js so that they are loaded from the ./tests folder

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.

The file I am commenting on here is ParentNode-querySelector-All-content.html, not selectors.js or ParentNode-querySelector-All.js. I am asking whether ParentNode-querySelector-All-content.html (and ParentNode-querySelector-All-content.xht) are completely new tests, or are derived from existing tests.


Separately, if we can reduce duplication for selectors.js and ParentNode-querySelector-All.js using a technique like what you describe, I think that would be good.

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.

Ah, they are loaded in iframe.

ParentNode-querySelector-All-dont-upstream.html
L19:

frame.src = "ParentNode-querySelector-All-content.html#target";

ParentNode-querySelector-All-xht-dont-upstream.xht
L21:

frame.src = "ParentNode-querySelector-All-content.xht#target";

I think we can remove them and load from ./tests too.

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.

Loading xht (xhtml) files from ./tests didn't work well.
It seemed to be a content-type issue, but maybe I'm just doing things wrong.

@asamuzaK
asamuzaK force-pushed the selector branch 6 times, most recently from 18e4f10 to 557429d Compare April 12, 2025 16:39
@asamuzaK

Copy link
Copy Markdown
Contributor Author

I will not be able to merge until we get CI passing, one way or another. I guess the problem is that Range-mutations-dataChange.html is failing, which is disappointing because I thought I made it significantly faster in 8da5ea8...f54e8e7 . I will want to do some local profiling to see if there is any major change between dom-selector and nwsapi for that test, I guess.

All green now.

The timeout is likely due to memory swap.

Ref: https://github.com/web-platform-tests/wpt/blob/9574a65b8b265c971aa15fa54500eb8f4a763119/dom/common.js#L18

This function creates and adds a lot of element nodes, many times.

@domenic domenic left a comment

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.

Overall this looks very nice. I really appreciate the attention to test coverage.

I left a few minor comments, and we should separate out the test-splitting work into #3860 which I would appreciate your review on. But I think we should be able to merge this soon.

I guess you are suggesting we do a prerelease test first? I am a bit worried we won't get enough testing coverage because a lot of people use jsdom as a transitive dependency (via Jest or Vitest or something, from what I understand). But maybe we can make it work if we:

  • Make a list of all issues where people are discussing either selector functionality or selector performance. Probably, that list = your list of fixed issues in the OP, plus #3667, #3644, and #3659
  • For each issue we add a small message asking people to test and giving them instructions on how to use package.json overrides to upgrade to a prerelease version.

"/dom/nodes/selectors.js"
];
if (extraResources.includes(url.pathname)) {
const filePath = path.resolve(__dirname, "tests" + url.pathname + (url.hash ? url.hash : ""));

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.

Why not just + url.hash?

Although, I don't think we have any cases where the hash is used, so maybe it's simpler to just omit that whole thing?

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'll fix url.hash part.
hash is needed in, for example, test/web-platform-tests/to-upstream/dom/nodes/ParentNode-querySelector-All-dont-upstream.html L19
It loads iframe content with hash.

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.

It's surprising that this makes a difference to the super.fetch() call. The file on disk does not have a hash in its name...

Comment thread lib/jsdom/living/helpers/style-rules.js Resolved
Comment thread lib/jsdom/living/helpers/style-rules.js Resolved
Comment thread lib/jsdom/living/nodes/Document-impl.js Resolved
@asamuzaK

Copy link
Copy Markdown
Contributor Author

What I have in mind is:

  1. Leave this PR open
  2. Prerelease jsdom@27.0.0-b.1
  3. Contact jest-dom (@testing-library) and vitest developers to test with them
  4. Add fixes on both jsdom and dom-selector if needed
  5. Release dom-selector@6.5.0
  6. Close this PR
  7. Release jsdom@27.0.0

I think I can take care of contacting jest-dom, vitest.
Thoughts?

@domenic

domenic commented Apr 13, 2025

Copy link
Copy Markdown
Member

I find leaving PRs open, so that releases on npm correspond to nothing clear on GitHub, to be a bit confusing. So I would prefer a slightly modified flow for that part. Let's merge to main and do a release from main, just with a prerelease version number.

I am unsure whether it should be a new major version or a new minor version. It is theoretically not a breaking change so a new minor version is more appropriate. But maybe it would be more cautious to do a major version?

I like the idea of contacting jest-dom and vitest to have them test. I think asking for feedback from people in #3659 by commenting on that thread would also be good.

@asamuzaK

asamuzaK commented Apr 13, 2025 •

Copy link
Copy Markdown
Contributor Author

TODO:

@asamuzaK
asamuzaK marked this pull request as draft April 14, 2025 17:08
@asamuzaK
asamuzaK marked this pull request as ready for review April 15, 2025 07:58
@asamuzaK
asamuzaK requested a review from domenic April 15, 2025 07:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment