Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: use @jridgewell/sourcemap-codec #215

Merged
merged 1 commit into from Dec 3, 2022

Conversation

sapphi-red
Copy link
Contributor

@sapphi-red sapphi-red commented Jun 3, 2022

@jridgewell/sourcemap-codec has better performance and memory footprint than sourcemap-codec.

This PR replaces sourcemap-codec with @jridgewell/sourcemap-codec.

before

node v16.16.0

generateMap (no edit) x 1,664 ops/sec ±0.62% (92 runs sampled)
generateMap (edit) x 1,651 ops/sec ±0.74% (92 runs sampled)

after

node v16.16.0

generateMap (no edit) x 1,981 ops/sec ±0.52% (93 runs sampled)
generateMap (edit) x 1,901 ops/sec ±0.56% (93 runs sampled)

.eslintrc Outdated
@@ -31,7 +31,7 @@
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 6,
"ecmaVersion": 2020,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

node12 supports up to ES2019. But ES2020 is needed to parse import.meta.url.

@sapphi-red sapphi-red marked this pull request as draft June 5, 2022 04:27
Copy link

@aleclarson aleclarson left a comment

Choose a reason for hiding this comment

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

Looks good to me 👍

For context, @jridgewell is the author of @ampproject/remapping (the best-in-class sourcemap chain flattener), so I trust in this fork's quality of maintenance.

@sapphi-red
Copy link
Contributor Author

@aleclarson FYI this pull request is waiting to see what happens with #216 (comment).

@sapphi-red sapphi-red marked this pull request as ready for review August 2, 2022 04:02
@jdalton
Copy link

jdalton commented Aug 12, 2022

@jridgewell it looks like @jridgewell/sourcemap-codec is a fork of Rich's. Do you have a summary of changed done to it?

To others: Should the original sourcemap-codec be deprecated?

@jridgewell
Copy link
Contributor

jridgewell commented Aug 12, 2022

You're only using the encode methods, so the relevant change is the use of a TextDecoder (or Buffer if your node version is old) to convert from the base64 bytes into a the string.

Sourcemaps are essentially a Line[], where each Line is a Segment[], and each Segment is a tuple of ints. Rich's implementations allocates an array per line, encodes each segment into a base64 string (using small concatenations, which is too small for v8's rope concatenations to kick in), and pushes the encoding into the line (array expansion costs). It then joins the array together, and pushes that onto the overall mappings string (this uses rope optimizations once the mappings is large enough).

My implementation uses a 16kb Uint8Array buffer. The individual base64 char is placed into the buffer, and when it fills I use a TextDecoder to covert them into a string in one shot. I then start filling the buffer again. This avoids small string concatenations, array expansions, and guarantees the rope optimizations kick in immediately (because 16kb is large enough).

@Rich-Harris Rich-Harris merged commit 847502c into Rich-Harris:master Dec 3, 2022
@Rich-Harris
Copy link
Owner

To others: Should the original sourcemap-codec be deprecated?

Yes, I think it should (sorry, dropped the ball on this whole convo, doing some inbox spring-cleaning). The fork has more downloads than the original, and I would wager the vast majority of sourcemap-codec's downloads are via magic-string, so deprecating it in favour of @jridgewell/sourcemap-codec would be the least disruptive move. Thanks @jridgewell for taking the initiative, I appreciate it 🍻

@Rich-Harris
Copy link
Owner

Released 0.27 with this change

@sapphi-red sapphi-red deleted the perf/encode-decode branch December 4, 2022 07:22
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.

None yet

5 participants