perf(transformer/jsx): use memchr for parsing JSX pragma comments#11001
Conversation
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
CodSpeed Instrumentation Performance ReportMerging #11001 will create unknown performance changesComparing Summary
Benchmarks breakdown
|
Merge activity
|
…11001) Use `memchr` for finding `@` when parsing JSX pragmas from comments. This wins back most (but not all) of the perf loss of #10983 on `antd.js` benchmark, and preserves the perf gain of #10983 on `cal.com.tsx` benchmark. Interestingly, using `memchr` to search just for `@` and then checking next 3 bytes are `jsx` separately is measurably faster than using `memchr::memmem::Finder` to search for `@jsx`.
03772ba to
6540f44
Compare
861a6da to
1aed99b
Compare
| // Search for `@`. | ||
| // Note: Using `memchr::memmem::Finder` to search for `@jsx` is slower than only using `memchr` | ||
| // to find `@` characters, and then checking if `@` is followed by `jsx` separately. | ||
| let at_sign_index = memchr(b'@', comment_str.as_bytes())?; |
There was a problem hiding this comment.
it might also be faster to use memchr_iter rather than a loop and having the searcher re-compiled again. (it also might make no difference depending on how the compiler is optimizing it)
There was a problem hiding this comment.
I don't think memchr (unlike memchr::memmem::Finder) compiles a searcher type - it's a straight function call. If it's inlined, the "needle" (@) should be statically known to compiler, which might allow it to generate more optimal code than a more complex type.
I've taken some shortcuts here - there are some bounds checks which could be eliminated, and other stuff that could be a little tighter. But my assumption was that almost all the cost is in searching for @, so everything after that was not worthwhile optimizing.
But still, yes, memchr_iter might be faster, especially if the searcher was created outside of find_jsx_pragma and passed in so it can be re-used. Want to try it?

Use
memchrfor finding@when parsing JSX pragmas from comments.This wins back most (but not all) of the perf loss of #10983 on
antd.jsbenchmark, and preserves the perf gain of #10983 oncal.com.tsxbenchmark.Interestingly, using
memchrto search just for@and then checking next 3 bytes arejsxseparately is measurably faster than usingmemchr::memmem::Finderto search for@jsx.