Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
cbd3c298
Unverified
Commit
cbd3c298
authored
Mar 13, 2023
by
LongYinan
Committed by
GitHub
Mar 13, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleanup remove directive logic to reduce allocate (#47055)
Reduce `unwrap` and `clone` usages
parent
25aa8693
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
54 additions
and
49 deletions
+54
-49
server_actions.rs
packages/next-swc/crates/core/src/server_actions.rs
+54
-49
No files found.
packages/next-swc/crates/core/src/server_actions.rs
View file @
cbd3c298
...
@@ -97,13 +97,11 @@ impl<C: Comments> ServerActions<C> {
...
@@ -97,13 +97,11 @@ impl<C: Comments> ServerActions<C> {
}
else
{
}
else
{
// Check if the function has `"use server"`
// Check if the function has `"use server"`
if
let
Some
(
body
)
=
maybe_body
{
if
let
Some
(
body
)
=
maybe_body
{
let
directive_index
=
get_server_directive_index_in_fn
(
&
body
.stmts
);
remove_server_directive_index_in_fn
(
if
directive_index
>=
0
{
&
mut
body
.stmts
,
is_action_fn
=
true
;
remove_directive
,
if
remove_directive
{
&
mut
is_action_fn
,
body
.stmts
.remove
(
directive_index
.try_into
()
.unwrap
());
);
}
}
}
}
}
}
...
@@ -339,16 +337,17 @@ impl<C: Comments> VisitMut for ServerActions<C> {
...
@@ -339,16 +337,17 @@ impl<C: Comments> VisitMut for ServerActions<C> {
.emit
();
.emit
();
});
});
}
else
if
!
self
.in_action_file
{
}
else
if
!
self
.in_action_file
{
if
f
.ident
.is_none
()
{
let
ident
=
match
f
.ident
.as_mut
()
{
let
action_name
=
gen_ident
(
&
mut
self
.ident_cnt
);
None
=>
{
f
.ident
=
Some
(
Ident
::
new
(
action_name
,
DUMMY_SP
));
let
action_name
=
gen_ident
(
&
mut
self
.ident_cnt
);
}
let
ident
=
Ident
::
new
(
action_name
,
DUMMY_SP
);
f
.ident
.insert
(
ident
)
}
Some
(
i
)
=>
i
,
};
let
(
maybe_new_fn
,
_
)
=
self
.add_action_annotations_and_maybe_hoist
(
let
(
maybe_new_fn
,
_
)
=
f
.ident
.as_mut
()
.unwrap
(),
self
.add_action_annotations_and_maybe_hoist
(
ident
,
Some
(
&
mut
f
.function
),
None
);
Some
(
&
mut
f
.function
),
None
,
);
if
let
Some
(
new_fn
)
=
maybe_new_fn
{
if
let
Some
(
new_fn
)
=
maybe_new_fn
{
f
.function
=
new_fn
;
f
.function
=
new_fn
;
...
@@ -536,12 +535,11 @@ impl<C: Comments> VisitMut for ServerActions<C> {
...
@@ -536,12 +535,11 @@ impl<C: Comments> VisitMut for ServerActions<C> {
}
}
fn
visit_mut_module_items
(
&
mut
self
,
stmts
:
&
mut
Vec
<
ModuleItem
>
)
{
fn
visit_mut_module_items
(
&
mut
self
,
stmts
:
&
mut
Vec
<
ModuleItem
>
)
{
let
directive_index
=
get_server_directive_index_in_module
(
stmts
);
remove_server_directive_index_in_module
(
if
directive_index
>=
0
{
stmts
,
self
.in_action_file
=
true
;
&
mut
self
.in_action_file
,
self
.has_action
=
true
;
&
mut
self
.has_action
,
stmts
.remove
(
directive_index
.try_into
()
.unwrap
());
);
}
let
old_annotations
=
self
.annotations
.take
();
let
old_annotations
=
self
.annotations
.take
();
let
mut
new
=
Vec
::
with_capacity
(
stmts
.len
());
let
mut
new
=
Vec
::
with_capacity
(
stmts
.len
());
...
@@ -857,40 +855,47 @@ fn annotate_ident_as_action(
...
@@ -857,40 +855,47 @@ fn annotate_ident_as_action(
));
));
}
}
fn
get_server_directive_index_in_module
(
stmts
:
&
[
ModuleItem
])
->
i32
{
fn
remove_server_directive_index_in_module
(
for
(
i
,
stmt
)
in
stmts
.iter
()
.enumerate
()
{
stmts
:
&
mut
Vec
<
ModuleItem
>
,
if
let
ModuleItem
::
Stmt
(
Stmt
::
Expr
(
first
))
=
stmt
{
in_action_file
:
&
mut
bool
,
match
&*
first
.expr
{
has_action
:
&
mut
bool
,
Expr
::
Lit
(
Lit
::
Str
(
Str
{
value
,
..
}))
=>
{
)
{
if
value
==
"use server"
{
stmts
.retain
(|
stmt
|
{
return
i
as
i32
;
if
let
ModuleItem
::
Stmt
(
Stmt
::
Expr
(
ExprStmt
{
}
expr
:
box
Expr
::
Lit
(
Lit
::
Str
(
Str
{
value
,
..
})),
}
..
_
=>
return
-
1
,
}))
=
stmt
{
if
value
==
"use server"
{
*
in_action_file
=
true
;
*
has_action
=
true
;
return
false
;
}
}
}
else
{
return
-
1
;
}
}
}
true
-
1
});
}
}
fn
get_server_directive_index_in_fn
(
stmts
:
&
[
Stmt
])
->
i32
{
fn
remove_server_directive_index_in_fn
(
for
(
i
,
stmt
)
in
stmts
.iter
()
.enumerate
()
{
stmts
:
&
mut
Vec
<
Stmt
>
,
if
let
Stmt
::
Expr
(
first
)
=
stmt
{
remove_directive
:
bool
,
match
&*
first
.expr
{
is_action_fn
:
&
mut
bool
,
Expr
::
Lit
(
Lit
::
Str
(
Str
{
value
,
..
}))
=>
{
)
{
if
value
==
"use server"
{
stmts
.retain
(|
stmt
|
{
return
i
as
i32
;
if
let
Stmt
::
Expr
(
ExprStmt
{
}
expr
:
box
Expr
::
Lit
(
Lit
::
Str
(
Str
{
value
,
..
})),
..
})
=
stmt
{
if
value
==
"use server"
{
*
is_action_fn
=
true
;
if
remove_directive
{
return
false
;
}
}
_
=>
return
-
1
,
}
}
}
else
{
return
-
1
;
}
}
}
true
-
1
});
}
}
fn
collect_idents_in_array_pat
(
elems
:
&
[
Option
<
Pat
>
])
->
Vec
<
Id
>
{
fn
collect_idents_in_array_pat
(
elems
:
&
[
Option
<
Pat
>
])
->
Vec
<
Id
>
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment