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

refactor(common): Debug-print spans more compactly #8746

Merged
merged 5 commits into from Mar 17, 2024

Conversation

Kyuuhachi
Copy link
Contributor

Description:

dbg!() output on ASTs is kinda verbose,

ExprStmt {
    span: Span {
        lo: BytePos(
            37,
        ),
        hi: BytePos(
            50,
        ),
        ctxt: #0,
    },
    expr: Lit(
        Str(
            Str {
                span: Span {
                    lo: BytePos(
                        37,
                    ),
                    hi: BytePos(
                        49,
                    ),
                    ctxt: #0,
                },
                value: "use strict",
                raw: Some(
                    "\"use strict\"",
                ),
            },
        ),
    ),
}

A lot of the space is wasted on spans — 9 lines per span, even though it's pretty much unimportant cruft. This PR changes that to just one line per span:

ExprStmt {
    span: 37..50#0,
    expr: Lit(
        Str(
            Str {
                span: 37..49#0,
                value: "use strict",
                raw: Some(
                    "\"use strict\"",
                ),
            },
        ),
    ),
}

While not a statistically meaningful measurement, in my tests (sample size = 1) this change reduces the dbg!() of a 1103-byte script from 5597 to 2885 lines, which is a 48% reduction. In {:?} mode it goes from 40034 to 25457 chars, or 37% reduction.

BREAKING CHANGE:

None, Debug output is generally not considered stable.

Related issue (if exists):

None known.

@Kyuuhachi Kyuuhachi requested a review from a team as a code owner March 13, 2024 16:41
Copy link

changeset-bot bot commented Mar 13, 2024

⚠️ No Changeset found

Latest commit: f85caa1

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@CLAassistant
Copy link

CLAassistant commented Mar 13, 2024

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@Kyuuhachi
Copy link
Contributor Author

This fails a bunch of tests under crates/jsdoc/tests/fixtures/*.debug, as well as crates/swc_ecma_parser/tests/shifted/empty-with-comments/input.ts.comments and crates/swc_ecma_parser/tests/typescript/custom/issue-236/input.ts.stdout. Since these files contain dbg!() output, it is very reasonable that they fail, but I'm not sure what to do about it. Is there some command to update the reference files to current values?

@kdy1
Copy link
Member

kdy1 commented Mar 14, 2024

UPDATE=1 cargo test should do the trick

@kdy1 kdy1 added this to the Planned milestone Mar 14, 2024
@magic-akari
Copy link
Member

magic-akari commented Mar 14, 2024

This will break https://swc-ast.vercel.app which is a tool that heavily relies on span output.
I will update it after this PR get merged.

Seems like that `.stdout` file was not used, so I'm just gonna delete it.
@Kyuuhachi Kyuuhachi requested a review from a team as a code owner March 14, 2024 14:36
@Kyuuhachi
Copy link
Contributor Author

Took a while to figure out how to run the tests, but there we go. Those jsdoc fixtures went from 48093 to 17173 lines, a 64% reduction. Also the input.ts.stdout seemed to be unused, so I just deleted it.

Massively changes the size of this PR though. Funny how that works sometimes.

@kdy1 kdy1 changed the title Debug-print spans more compactly refactor(common): Debug-print spans more compactly Mar 15, 2024
@kdy1 kdy1 enabled auto-merge (squash) March 15, 2024 01:15
Copy link
Collaborator

@swc-bot swc-bot left a comment

Choose a reason for hiding this comment

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

Automated review comment generated by auto-rebase script

Copy link
Collaborator

@swc-bot swc-bot left a comment

Choose a reason for hiding this comment

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

Automated review comment generated by auto-rebase script

Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

swc-bump:

  • swc_common

@kdy1 kdy1 merged commit f2300da into swc-project:main Mar 17, 2024
141 of 143 checks passed
@kdy1 kdy1 modified the milestones: Planned, v1.4.11 Mar 27, 2024
@swc-project swc-project locked as resolved and limited conversation to collaborators Apr 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants