Unverified Commit 489d5ac6 authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

perf: Optmize `react_server_component` transform (#67335)

### What?

 - Use `Rc` instead of cloning vector.
 - Use faster hash algorithm.

### Why?

`react_server_component` showed up in the profiling result.

### How?

Closes PACK-3120
parent 38d08e27
use std::{collections::HashMap, path::PathBuf}; use std::{collections::HashMap, path::PathBuf, rc::Rc};
use regex::Regex; use regex::Regex;
use serde::Deserialize; use serde::Deserialize;
...@@ -52,7 +52,7 @@ struct ReactServerComponents<C: Comments> { ...@@ -52,7 +52,7 @@ struct ReactServerComponents<C: Comments> {
filepath: String, filepath: String,
app_dir: Option<PathBuf>, app_dir: Option<PathBuf>,
comments: C, comments: C,
directive_import_collection: Option<(bool, bool, Vec<ModuleImports>, Vec<String>)>, directive_import_collection: Option<(bool, bool, RcVec<ModuleImports>, RcVec<String>)>,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
...@@ -475,9 +475,12 @@ struct ReactServerComponentValidator { ...@@ -475,9 +475,12 @@ struct ReactServerComponentValidator {
invalid_server_lib_apis_mapping: HashMap<&'static str, Vec<&'static str>>, invalid_server_lib_apis_mapping: HashMap<&'static str, Vec<&'static str>>,
invalid_client_imports: Vec<JsWord>, invalid_client_imports: Vec<JsWord>,
invalid_client_lib_apis_mapping: HashMap<&'static str, Vec<&'static str>>, invalid_client_lib_apis_mapping: HashMap<&'static str, Vec<&'static str>>,
pub directive_import_collection: Option<(bool, bool, Vec<ModuleImports>, Vec<String>)>, pub directive_import_collection: Option<(bool, bool, RcVec<ModuleImports>, RcVec<String>)>,
} }
// A type to workaround a clippy warning.
type RcVec<T> = Rc<Vec<T>>;
impl ReactServerComponentValidator { impl ReactServerComponentValidator {
pub fn new(is_react_server_layer: bool, filename: String, app_dir: Option<PathBuf>) -> Self { pub fn new(is_react_server_layer: bool, filename: String, app_dir: Option<PathBuf>) -> Self {
Self { Self {
...@@ -784,6 +787,8 @@ impl Visit for ReactServerComponentValidator { ...@@ -784,6 +787,8 @@ impl Visit for ReactServerComponentValidator {
fn visit_module(&mut self, module: &Module) { fn visit_module(&mut self, module: &Module) {
let (is_client_entry, is_action_file, imports, export_names) = let (is_client_entry, is_action_file, imports, export_names) =
collect_top_level_directives_and_imports(&self.app_dir, &self.filepath, module); collect_top_level_directives_and_imports(&self.app_dir, &self.filepath, module);
let imports = Rc::new(imports);
let export_names = Rc::new(export_names);
self.directive_import_collection = Some(( self.directive_import_collection = Some((
is_client_entry, is_client_entry,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment