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(es/ast): Box Stmt and ModuleDecl #7034

Closed
wants to merge 105 commits into from
Closed

Conversation

kdy1
Copy link
Member

@kdy1 kdy1 commented Mar 8, 2023

Description:

Related issue:

@kdy1 kdy1 added this to the Planned milestone Mar 8, 2023
@kdy1 kdy1 self-assigned this Mar 8, 2023
kodiakhq[bot]
kodiakhq bot previously approved these changes Mar 9, 2023
kodiakhq[bot]
kodiakhq bot previously approved these changes Mar 9, 2023
@@ -76,7 +76,7 @@ impl Take for Module {
pub struct Script {
pub span: Span,

pub body: Vec<Stmt>,
pub body: Vec<Box<Stmt>>,
Copy link
Contributor

@dsherret dsherret Mar 9, 2023

Choose a reason for hiding this comment

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

I think this shouldn’t be boxed become it’s already in a vec? There are some other places this is done too that could be reverted.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry. I was not thinking and it's late here :)

#[tag("*")]
Stmt(Stmt),
Stmt(Box<Stmt>),
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just box the large variants? I'm doubtful this is going to have much impact on performance. Have you been able to benchmark it? Is this the main change in this pr?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I profiled it using the minifier

Copy link
Member Author

Choose a reason for hiding this comment

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

The performance gain was about 10% (8% ~ 15%), on total time

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll run it one more time

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm... I can't reproduce it.

Copy link
Contributor

Choose a reason for hiding this comment

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

If there are gains, I bet most of it is from the change on BlockStmt because that will be more prevalent than top level module items (generally, most files will have a low number of module items). It seems most of the nightly feature is used to support the boxing here though.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds reasonable. I'll wait for the profiling result as I requested one to someone

@kdy1
Copy link
Member Author

kdy1 commented Mar 9, 2023

I profiled this again, and seems like this also regresses performance

@Boshen
Copy link
Contributor

Boshen commented Mar 9, 2023

I don't think boxing outside is going to work, because you're essential allocating twice for each stmt - elements of Vec are already pushed onto the heap.

Shrinking the struct only takes effect in places where we clone the whole Vec<Stmt>.

@kdy1
Copy link
Member Author

kdy1 commented Mar 9, 2023

There are lots of codes that replace Vec<Stmt> with another vector and moves elements from the old one to the new one. memmove is much more costly than you think.

@kdy1
Copy link
Member Author

kdy1 commented Mar 9, 2023

If you clone it, this patch does not help. It's a new allocation anyway. Tihs PR is about removing needless memory operations, not required operations.

@kdy1
Copy link
Member Author

kdy1 commented Mar 9, 2023

Closing in favor of #7041

@kdy1 kdy1 closed this Mar 9, 2023
@kdy1 kdy1 deleted the ast-box branch March 9, 2023 13:17
@kdy1 kdy1 restored the ast-box branch March 9, 2023 13:23
@kdy1
Copy link
Member Author

kdy1 commented Mar 9, 2023

Well, I think I may need this/

@kdy1 kdy1 reopened this Mar 9, 2023
@kdy1 kdy1 marked this pull request as draft March 9, 2023 14:03
@magic-akari
Copy link
Member

There are lots of codes that replace Vec<Stmt> with another vector and moves elements from the old one to the new one. memmove is much more costly than you think.

There are many prepend_stmt operations. Should we allocate a separate field for the directive "use strict"?

Alternatively, is there any other data structure that would facilitate our insertion operations?

@kdy1
Copy link
Member Author

kdy1 commented Mar 13, 2023

Box<Stmt> is also important for prepend_stmt, but I'm not sure if there's another clean way to optimize memmove of Stmt

@kdy1
Copy link
Member Author

kdy1 commented Mar 22, 2023

I'll create another PR in the future

@kdy1 kdy1 closed this Mar 22, 2023
@kdy1 kdy1 deleted the ast-box branch March 22, 2023 01:45
@kdy1 kdy1 modified the milestones: Planned, v1.3.42 Mar 22, 2023
@swc-project swc-project locked as resolved and limited conversation to collaborators Apr 21, 2023
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.

perf(ast): shrink large struct sizes
5 participants