Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions compiler/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions compiler/crates/react_compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ react_compiler_ssa = { path = "../react_compiler_ssa" }
react_compiler_typeinference = { path = "../react_compiler_typeinference" }
react_compiler_validation = { path = "../react_compiler_validation" }
indexmap = "2"
rustc-hash = "2"
serde = { version = "1", features = ["derive"] }
serde_json = { version = "1", features = ["raw_value"] }
24 changes: 12 additions & 12 deletions compiler/crates/react_compiler/src/entrypoint/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
use std::collections::{HashMap, HashSet};
use rustc_hash::{FxHashMap, FxHashSet};

use react_compiler_ast::common::BaseNode;
use react_compiler_ast::declarations::{
Expand Down Expand Up @@ -72,9 +72,9 @@ pub struct ProgramContext {
pub debug_enabled: bool,

// Internal state
already_compiled: HashSet<u32>,
known_referenced_names: HashSet<String>,
imports: HashMap<String, HashMap<String, NonLocalImportSpecifier>>,
already_compiled: FxHashSet<u32>,
known_referenced_names: FxHashSet<String>,
imports: FxHashMap<String, FxHashMap<String, NonLocalImportSpecifier>>,
}

impl ProgramContext {
Expand Down Expand Up @@ -104,9 +104,9 @@ impl ProgramContext {
renames: Vec::new(),
timing: TimingData::new(profiling),
debug_enabled,
already_compiled: HashSet::new(),
known_referenced_names: HashSet::new(),
imports: HashMap::new(),
already_compiled: FxHashSet::default(),
known_referenced_names: FxHashSet::default(),
imports: FxHashMap::default(),
}
}

Expand Down Expand Up @@ -230,13 +230,13 @@ impl ProgramContext {
}

/// Get the set of known referenced names for seeding per-function Environment UID generation.
pub fn known_referenced_names(&self) -> &HashSet<String> {
pub fn known_referenced_names(&self) -> &FxHashSet<String> {
&self.known_referenced_names
}

/// Merge UID names generated during a function compilation back into the program context,
/// so subsequent function compilations avoid collisions.
pub fn merge_uid_known_names(&mut self, names: &HashSet<String>) {
pub fn merge_uid_known_names(&mut self, names: &FxHashSet<String>) {
self.known_referenced_names.extend(names.iter().cloned());
}

Expand All @@ -259,7 +259,7 @@ impl ProgramContext {
}

/// Get an immutable view of the generated imports.
pub fn imports(&self) -> &HashMap<String, HashMap<String, NonLocalImportSpecifier>> {
pub fn imports(&self) -> &FxHashMap<String, FxHashMap<String, NonLocalImportSpecifier>> {
&self.imports
}
}
Expand All @@ -274,7 +274,7 @@ pub fn validate_restricted_imports(
Some(b) if !b.is_empty() => b,
_ => return None,
};
let restricted: HashSet<&str> = blocklisted.iter().map(|s| s.as_str()).collect();
let restricted: FxHashSet<&str> = blocklisted.iter().map(|s| s.as_str()).collect();
let mut error = CompilerError::new();

for stmt in &program.body {
Expand Down Expand Up @@ -326,7 +326,7 @@ pub fn add_imports_to_program(program: &mut Program, context: &ProgramContext) {
}

// Collect existing non-namespaced imports by module name
let existing_import_indices: HashMap<String, usize> = program
let existing_import_indices: FxHashMap<String, usize> = program
.body
.iter()
.enumerate()
Expand Down
52 changes: 26 additions & 26 deletions compiler/crates/react_compiler/src/entrypoint/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
//! Analogous to TS `Pipeline.ts` (`compileFn` → `run` → `runWithEnvironment`).
//! Currently runs BuildHIR (lowering) and PruneMaybeThrows.

use indexmap::IndexMap;
use react_compiler_ast::scope::ScopeInfo;
use react_compiler_diagnostics::CompilerError;
use react_compiler_hir::ReactFunctionType;
use react_compiler_hir::environment::Environment;
use react_compiler_hir::environment::OutputMode;
use react_compiler_hir::environment_config::EnvironmentConfig;
use react_compiler_lowering::FunctionNode;
use rustc_hash::{FxBuildHasher, FxHashMap};

use super::compile_result::CodegenFunction;
use super::compile_result::CompilerErrorDetailInfo;
Expand Down Expand Up @@ -1229,25 +1231,23 @@ pub fn compile_outlined_fn(
fn build_outlined_scope_info(
func: &mut react_compiler_ast::statements::FunctionDeclaration,
) -> react_compiler_ast::scope::ScopeInfo {
use std::collections::HashMap;

use react_compiler_ast::scope::*;

let mut pos: u32 = 1; // reserve 0 for the function itself
func.base.start = Some(0);

let mut fn_bindings: HashMap<String, BindingId> = HashMap::new();
let mut fn_bindings: FxHashMap<String, BindingId> = FxHashMap::default();
let mut bindings_list: Vec<BindingData> = Vec::new();
let mut ref_to_binding: indexmap::IndexMap<u32, BindingId> = indexmap::IndexMap::new();
let mut ref_to_binding: IndexMap<u32, BindingId, FxBuildHasher> = IndexMap::default();

// Helper to add a binding
let _add_binding =
|name: &str,
kind: BindingKind,
p: u32,
fn_bindings: &mut HashMap<String, BindingId>,
fn_bindings: &mut FxHashMap<String, BindingId>,
bindings_list: &mut Vec<BindingData>,
ref_to_binding: &mut indexmap::IndexMap<u32, BindingId>| {
ref_to_binding: &mut IndexMap<u32, BindingId, FxBuildHasher>| {
if fn_bindings.contains_key(name) {
// Already exists, just add reference
let bid = fn_bindings[name];
Expand Down Expand Up @@ -1296,7 +1296,7 @@ fn build_outlined_scope_info(
id: ScopeId(0),
parent: None,
kind: ScopeKind::Program,
bindings: HashMap::new(),
bindings: FxHashMap::default(),
};
let fn_scope = ScopeData {
id: ScopeId(1),
Expand All @@ -1305,21 +1305,21 @@ fn build_outlined_scope_info(
bindings: fn_bindings,
};

let mut node_to_scope: HashMap<u32, ScopeId> = HashMap::new();
let mut node_to_scope: FxHashMap<u32, ScopeId> = FxHashMap::default();
node_to_scope.insert(0, ScopeId(1));

// Mirror position maps into node-ID maps for outlined functions
let mut node_id_to_scope: HashMap<u32, ScopeId> = HashMap::new();
let mut node_id_to_scope: FxHashMap<u32, ScopeId> = FxHashMap::default();
node_id_to_scope.insert(0, ScopeId(1));
let ref_node_id_to_binding: indexmap::IndexMap<u32, BindingId> =
let ref_node_id_to_binding: IndexMap<u32, BindingId, FxBuildHasher> =
ref_to_binding.iter().map(|(&k, &v)| (k, v)).collect();

ScopeInfo {
scopes: vec![program_scope, fn_scope],
bindings: bindings_list,
node_to_scope,
node_to_scope_end: HashMap::new(),
reference_to_binding: indexmap::IndexMap::new(),
node_to_scope_end: FxHashMap::default(),
reference_to_binding: IndexMap::default(),
ref_node_id_to_binding,
node_id_to_scope,
program_scope: ScopeId(0),
Expand All @@ -1331,9 +1331,9 @@ fn outlined_assign_pattern_positions(
pattern: &mut react_compiler_ast::patterns::PatternLike,
pos: &mut u32,
kind: react_compiler_ast::scope::BindingKind,
fn_bindings: &mut std::collections::HashMap<String, react_compiler_ast::scope::BindingId>,
fn_bindings: &mut rustc_hash::FxHashMap<String, react_compiler_ast::scope::BindingId>,
bindings_list: &mut Vec<react_compiler_ast::scope::BindingData>,
ref_to_binding: &mut indexmap::IndexMap<u32, react_compiler_ast::scope::BindingId>,
ref_to_binding: &mut IndexMap<u32, react_compiler_ast::scope::BindingId, FxBuildHasher>,
) {
use react_compiler_ast::patterns::PatternLike;
use react_compiler_ast::scope::*;
Expand Down Expand Up @@ -1432,9 +1432,9 @@ fn outlined_assign_pattern_positions(
fn outlined_assign_stmt_positions(
stmt: &mut react_compiler_ast::statements::Statement,
pos: &mut u32,
fn_bindings: &mut std::collections::HashMap<String, react_compiler_ast::scope::BindingId>,
fn_bindings: &mut rustc_hash::FxHashMap<String, react_compiler_ast::scope::BindingId>,
bindings_list: &mut Vec<react_compiler_ast::scope::BindingData>,
ref_to_binding: &mut indexmap::IndexMap<u32, react_compiler_ast::scope::BindingId>,
ref_to_binding: &mut IndexMap<u32, react_compiler_ast::scope::BindingId, FxBuildHasher>,
) {
use react_compiler_ast::statements::Statement;

Expand Down Expand Up @@ -1477,8 +1477,8 @@ fn outlined_assign_stmt_positions(
fn outlined_assign_expr_positions(
expr: &mut react_compiler_ast::expressions::Expression,
pos: &mut u32,
fn_bindings: &std::collections::HashMap<String, react_compiler_ast::scope::BindingId>,
ref_to_binding: &mut indexmap::IndexMap<u32, react_compiler_ast::scope::BindingId>,
fn_bindings: &rustc_hash::FxHashMap<String, react_compiler_ast::scope::BindingId>,
ref_to_binding: &mut IndexMap<u32, react_compiler_ast::scope::BindingId, FxBuildHasher>,
) {
use react_compiler_ast::expressions::*;

Expand Down Expand Up @@ -1538,8 +1538,8 @@ fn outlined_assign_expr_positions(
fn outlined_assign_jsx_name_positions(
name: &mut react_compiler_ast::jsx::JSXElementName,
pos: &mut u32,
fn_bindings: &std::collections::HashMap<String, react_compiler_ast::scope::BindingId>,
ref_to_binding: &mut indexmap::IndexMap<u32, react_compiler_ast::scope::BindingId>,
fn_bindings: &rustc_hash::FxHashMap<String, react_compiler_ast::scope::BindingId>,
ref_to_binding: &mut IndexMap<u32, react_compiler_ast::scope::BindingId, FxBuildHasher>,
) {
match name {
react_compiler_ast::jsx::JSXElementName::JSXIdentifier(id) => {
Expand All @@ -1561,8 +1561,8 @@ fn outlined_assign_jsx_name_positions(
fn outlined_assign_jsx_member_positions(
member: &mut react_compiler_ast::jsx::JSXMemberExpression,
pos: &mut u32,
fn_bindings: &std::collections::HashMap<String, react_compiler_ast::scope::BindingId>,
ref_to_binding: &mut indexmap::IndexMap<u32, react_compiler_ast::scope::BindingId>,
fn_bindings: &rustc_hash::FxHashMap<String, react_compiler_ast::scope::BindingId>,
ref_to_binding: &mut IndexMap<u32, react_compiler_ast::scope::BindingId, FxBuildHasher>,
) {
match &mut *member.object {
react_compiler_ast::jsx::JSXMemberExprObject::JSXIdentifier(id) => {
Expand All @@ -1583,8 +1583,8 @@ fn outlined_assign_jsx_member_positions(
fn outlined_assign_jsx_val_positions(
val: &mut react_compiler_ast::jsx::JSXAttributeValue,
pos: &mut u32,
fn_bindings: &std::collections::HashMap<String, react_compiler_ast::scope::BindingId>,
ref_to_binding: &mut indexmap::IndexMap<u32, react_compiler_ast::scope::BindingId>,
fn_bindings: &rustc_hash::FxHashMap<String, react_compiler_ast::scope::BindingId>,
ref_to_binding: &mut IndexMap<u32, react_compiler_ast::scope::BindingId, FxBuildHasher>,
) {
match val {
react_compiler_ast::jsx::JSXAttributeValue::JSXExpressionContainer(c) => {
Expand All @@ -1608,8 +1608,8 @@ fn outlined_assign_jsx_val_positions(
fn outlined_assign_jsx_child_positions(
child: &mut react_compiler_ast::jsx::JSXChild,
pos: &mut u32,
fn_bindings: &std::collections::HashMap<String, react_compiler_ast::scope::BindingId>,
ref_to_binding: &mut indexmap::IndexMap<u32, react_compiler_ast::scope::BindingId>,
fn_bindings: &rustc_hash::FxHashMap<String, react_compiler_ast::scope::BindingId>,
ref_to_binding: &mut IndexMap<u32, react_compiler_ast::scope::BindingId, FxBuildHasher>,
) {
match child {
react_compiler_ast::jsx::JSXChild::JSXExpressionContainer(c) => {
Expand Down
13 changes: 6 additions & 7 deletions compiler/crates/react_compiler/src/entrypoint/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
//! 5. Processing each function through the compilation pipeline
//! 6. Applying compiled functions back to the AST

use std::collections::HashMap;
use std::collections::HashSet;
use rustc_hash::{FxHashMap, FxHashSet};

use react_compiler_ast::File;
use react_compiler_ast::Program;
Expand Down Expand Up @@ -2084,9 +2083,9 @@ struct CompiledFnForReplacement {
fn get_functions_referenced_before_declaration(
program: &Program,
compiled_fns: &[CompiledFnForReplacement],
) -> HashSet<u32> {
) -> FxHashSet<u32> {
// Collect function names and their node_ids for compiled FunctionDeclarations
let mut fn_names: HashMap<String, u32> = HashMap::new();
let mut fn_names: FxHashMap<String, u32> = FxHashMap::default();
for compiled in compiled_fns {
if compiled.original_kind == OriginalFnKind::FunctionDeclaration {
if let Some(ref name) = compiled.fn_name {
Expand All @@ -2098,10 +2097,10 @@ fn get_functions_referenced_before_declaration(
}

if fn_names.is_empty() {
return HashSet::new();
return FxHashSet::default();
}

let mut referenced_before_decl: HashSet<u32> = HashSet::new();
let mut referenced_before_decl: FxHashSet<u32> = FxHashSet::default();

// Walk through program body in order. For each statement, check if it references
// any of the function names before the function's declaration.
Expand Down Expand Up @@ -2597,7 +2596,7 @@ fn apply_compiled_functions(
let referenced_before_decl = if has_gating {
get_functions_referenced_before_declaration(program, compiled_fns)
} else {
HashSet::new()
FxHashSet::default()
};

// For gated functions, we need to clone the original function expressions
Expand Down
Loading
Loading