This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 660
perf(rome_js_formatter): Reduce FormatElement
size
#2456
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Parser conformance results on ubuntu-latestjs/262
jsx/babel
ts/babel
ts/microsoft
|
MichaReiser
force-pushed
the
perf/concat-elements
branch
from
April 15, 2022 21:42
b36b7bb
to
3c88c8e
Compare
This PR reduces the size of a `FormatElement` from 56 to 32 bytes by: * Using a `Box<str>` to store a dynamic token's string as it doesn't need to be mutable (safes 8 bytes for the `capacity`) * Only storing the start position for a `DynamicToken` because the length isn't used by the printer * Change the semantics of `verbatim_ranges` to store the ranges in the **formatted** string. The ranges in the formatted output should be sufficient for debugging and it allows to resolve the `String` rather than having to store it on the `FormatElement` (range can be computed in the printer). This improves the formatter's performance by about 10%
MichaReiser
force-pushed
the
perf/concat-elements
branch
from
April 16, 2022 07:11
3c88c8e
to
384026a
Compare
MichaReiser
commented
Apr 16, 2022
ematipico
reviewed
Apr 19, 2022
Comment on lines
-266
to
-267
pub fn into_verbatim(self) -> Vec<(String, TextRange)> { | ||
self.verbatim_source |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any particular reason why we removed this function? It was there mainly to be in line with into_sourcemap
and into_code
. If it's been removed because not used, then we should do the same with into_sourcemap
too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed it because it wasn't used and the struct no longer stores the Vec
that it could just take and return. Having the iterator specific method makes it clear that taking the verbatim sources requires a vector allocation.
ematipico
approved these changes
Apr 20, 2022
leops
approved these changes
Apr 20, 2022
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR reduces the size of a
FormatElement
from 56 to 32 bytes by:Box<str>
to store a dynamic token's string as it doesn't need to be mutable (safes 8 bytes for thecapacity
)DynamicToken
because the length isn't used by the printerverbatim_ranges
to store the ranges in the formatted string. The ranges in the formatted output should be sufficient for debugging and it allows to resolve theString
rather than having to store it on theFormatElement
(range can be computed in the printer).It further improves the
get_lines_between
implementation to only check the leading trivia because the trailing trivia should never contain new lines except for the EOF token which isn't relevant for that function.This improves the formatter's performance by about 10%
Test Plan
cargo test
.I changed a node implementation to call
format_verbatim
and verified the printed text & ranges.Future improvements
It should be possible to bring the
FormatElement
size down to 24 bytes byMarker
Element that creates a source map entry from source position to formatted positionToken
variantsList
'sVec
with aVec
that uses u32 for indices. This should be sufficient, considering that Rome only supports files with less than u32::max characters