Skip to content

script: Use a u16 for prototype id to speed up Castable::is<T>()#44364

Merged
jdm merged 1 commit into
servo:mainfrom
webbeef:faster_get_dom_class
Apr 25, 2026
Merged

script: Use a u16 for prototype id to speed up Castable::is<T>()#44364
jdm merged 1 commit into
servo:mainfrom
webbeef:faster_get_dom_class

Conversation

@webbeef

@webbeef webbeef commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

Castable::is<T>() calls get_dom_class() in a way that incurs a roundtrip to the JS engine and
multiple pointer indirections:

self.reflector().get_jsobject().get() -> JSObject
Object.shape -> BaseShape.clasp -> JSClass
DOMJSClass.dom_class -> DOMClass
interface_chain[depth] == ID -> bool

Since the ID doesn't change after creation of reflectors, we can instead store the ID on the reflector
and avoid most of that cost. The trick is to use a Depth First Search to generate IDs that allow a
range check for Castable::is<T>. An example of IDs in the DFS looks like:

EventTarget = 0
Node = 1
CharacterData = 2
Text = 3
CDATASection = 4
Comment = 5
ProcessingInstruction = 6
Document = 7
HTMLDocument = 8
XMLDocument = 9

Testing: Green try run at https://github.com/webbeef/servo/actions/runs/24640508040 and manual testing with general browsing.

@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Apr 20, 2026
@webbeef

webbeef commented Apr 20, 2026

Copy link
Copy Markdown
Contributor Author

Local testing shows the parse_from_string test down from 250ms to 200ms with that change.

@webbeef webbeef force-pushed the faster_get_dom_class branch 3 times, most recently from 01c662e to 965e6c7 Compare April 20, 2026 19:15
@webbeef webbeef force-pushed the faster_get_dom_class branch from 0e846a0 to dcf1b8e Compare April 20, 2026 19:44
@jdm

jdm commented Apr 25, 2026

Copy link
Copy Markdown
Member

It kills me a little bit to take the memory hit for every reflected object, but the speed tradeoff is undeniable.

@servo-highfive servo-highfive removed the S-awaiting-review There is new code that needs to be reviewed. label Apr 25, 2026
@jdm jdm added this pull request to the merge queue Apr 25, 2026
@servo-highfive servo-highfive added the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Apr 25, 2026
Merged via the queue into servo:main with commit 87daffa Apr 25, 2026
30 checks passed
@servo-highfive servo-highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Apr 25, 2026
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.

3 participants