+
Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.

refactor(rome_formatter): Return reference from context.comments() #3120

Merged
merged 6 commits into from
Aug 29, 2022

Conversation

MichaReiser
Copy link
Contributor

@MichaReiser MichaReiser commented Aug 28, 2022

context.comments used to return a Rc<Comments> so that the lifetime of comments isn't bound to the lifetime of the FormatContext. This is necessary to support writing to the formatter while holding to a reference of comments:

(Pseudo code):

let comments = f.context().comments();

for comment in comments.leading_comments(node) {
  write!(f, [comment])?;
}

The downside of returning an Rc is that every code path using comments pays for the overhead of incrementing the Rc counter and using an Rc in the signature leaks the abstraction.

This PR changes Comments to use an Rc internally to enable cheap cloning, similar to how SyntaxNode works. This allows context.comments() to return a reference to Comments instead and only code paths that need to write to the formatter have to pay the overhead of cloning the comments.

Tests

cargo test as this PR isn't adding any new functionality

@MichaReiser MichaReiser added the A-Formatter Area: formatter label Aug 28, 2022
@MichaReiser MichaReiser requested a review from ematipico as a code owner August 28, 2022 10:37
@MichaReiser MichaReiser temporarily deployed to aws August 28, 2022 10:37 Inactive
@MichaReiser
Copy link
Contributor Author

!bench_formatter

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Aug 28, 2022

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: e1cd151
Status: ✅  Deploy successful!
Preview URL: https://f12541df.tools-8rn.pages.dev
Branch Preview URL: https://refactor-comments-internal-r.tools-8rn.pages.dev

View logs

@MichaReiser MichaReiser temporarily deployed to aws August 28, 2022 10:42 Inactive
@github-actions
Copy link

github-actions bot commented Aug 28, 2022

@github-actions
Copy link

Formatter Benchmark Results

group                                    main                                   pr
-----                                    ----                                   --
formatter/checker.ts                     1.00    409.5±2.75ms     6.3 MB/sec    1.00    409.2±2.19ms     6.4 MB/sec
formatter/compiler.js                    1.00    250.7±1.30ms     4.2 MB/sec    1.01    254.0±1.31ms     4.1 MB/sec
formatter/d3.min.js                      1.00    204.0±1.41ms  1315.8 KB/sec    1.02    208.7±2.26ms  1285.8 KB/sec
formatter/dojo.js                        1.00     13.1±0.06ms     5.2 MB/sec    1.00     13.1±0.05ms     5.2 MB/sec
formatter/ios.d.ts                       1.01    263.3±0.55ms     7.1 MB/sec    1.00    259.8±0.76ms     7.2 MB/sec
formatter/jquery.min.js                  1.01     56.6±0.64ms  1495.8 KB/sec    1.00     55.8±0.66ms  1515.5 KB/sec
formatter/math.js                        1.00    429.9±3.21ms  1542.3 KB/sec    1.00    431.7±2.81ms  1535.8 KB/sec
formatter/parser.ts                      1.01      8.8±0.04ms     5.6 MB/sec    1.00      8.7±0.02ms     5.6 MB/sec
formatter/pixi.min.js                    1.00    240.5±1.83ms  1868.6 KB/sec    1.02    244.4±1.67ms  1838.6 KB/sec
formatter/react-dom.production.min.js    1.00     67.3±0.63ms  1750.0 KB/sec    1.02     68.7±0.66ms  1716.4 KB/sec
formatter/react.production.min.js        1.00      3.3±0.02ms  1929.1 KB/sec    1.00      3.3±0.01ms  1932.3 KB/sec
formatter/router.ts                      1.01      6.8±0.01ms     9.1 MB/sec    1.00      6.7±0.01ms     9.1 MB/sec
formatter/tex-chtml-full.js              1.00    551.4±3.56ms  1692.4 KB/sec    1.00    552.8±2.90ms  1688.0 KB/sec
formatter/three.min.js                   1.00    268.0±1.95ms     2.2 MB/sec    1.02    273.0±2.03ms     2.2 MB/sec
formatter/typescript.js                  1.00   1666.4±5.59ms     5.7 MB/sec    1.00   1660.6±4.53ms     5.7 MB/sec
formatter/vue.global.prod.js             1.01     91.1±0.80ms  1354.2 KB/sec    1.00     90.6±0.65ms  1362.1 KB/sec

Micha Reiser and others added 3 commits August 29, 2022 08:10
`context.comments` used to return a `Rc<Comments>` so that the lifetime of `comments` isn't bound to the lifetime of the `FormatContext`. This is necessary to support writing to the formatter while holding to a reference of comments:

(Pseudo code):

```rust
let comments = f.context().comments();

for comment in comments.leading_comments(node) {
  write!(f, [comment])?;
}
```

The downside of returning an `Rc` is that every code path using `comments` pays for the overhead of incrementing the `Rc` counter.

This PR changes `Comments` to use an `Rc` internally to enable cheap cloning, similar to how `SyntaxNode` works. This allows `context.comments()` to return a reference to `Comments` instead and only code paths that need to write to the formatter have to pay the overhead of cloning the comments.

## Tests

`cargo test` as this PR isn't adding any new functionality
@MichaReiser MichaReiser force-pushed the refactor/comments-internal-rc branch from 8e32a0c to de7cb04 Compare August 29, 2022 06:14
@MichaReiser MichaReiser temporarily deployed to aws August 29, 2022 06:14 Inactive
@MichaReiser MichaReiser temporarily deployed to aws August 29, 2022 06:39 Inactive
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
@MichaReiser MichaReiser temporarily deployed to aws August 29, 2022 09:44 Inactive
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
@MichaReiser MichaReiser temporarily deployed to aws August 29, 2022 09:44 Inactive
@MichaReiser MichaReiser merged commit 8d81f9a into main Aug 29, 2022
@MichaReiser MichaReiser deleted the refactor/comments-internal-rc branch August 29, 2022 10:07
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-Formatter Area: formatter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载