Skip to content

Commit

Permalink
Reduce allocation of union types
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Mar 19, 2024
1 parent 7d50869 commit 6b32a27
Show file tree
Hide file tree
Showing 28 changed files with 113 additions and 99 deletions.
3 changes: 1 addition & 2 deletions src/analyzer/expr/assignment/array_assignment_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use oxidized::{
aast::{self, Expr},
ast_defs::Pos,
};
use rustc_hash::FxHashSet;

use crate::{
expr::{expression_identifier, fetch::array_fetch_analyzer},
Expand Down Expand Up @@ -422,7 +421,7 @@ fn add_array_assignment_dataflow(

let old_parent_nodes = parent_expr_type.parent_nodes.clone();

parent_expr_type.parent_nodes = FxHashSet::from_iter([parent_node.clone()]);
parent_expr_type.parent_nodes = vec![parent_node.clone()];

for old_parent_node in old_parent_nodes {
analysis_data.data_flow_graph.add_path(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,13 @@ fn add_instance_property_assignment_dataflow(
if let Some(stmt_var_type) = stmt_var_type {
let mut stmt_type_inner = (**stmt_var_type).clone();

stmt_type_inner.parent_nodes.insert(var_node.clone());
if !stmt_type_inner
.parent_nodes
.iter()
.any(|n| &n.id == &var_node.id)
{
stmt_type_inner.parent_nodes.push(var_node.clone());
}

*stmt_var_type = Rc::new(stmt_type_inner);
}
Expand Down
4 changes: 2 additions & 2 deletions src/analyzer/expr/binop/arithmetic_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ pub(crate) fn assign_arithmetic_type(
lhs_expr.1.start_offset() as u32,
lhs_expr.1.end_offset() as u32,
)) {
cond_type.parent_nodes.insert(decision_node.clone());
cond_type.parent_nodes.push(decision_node.clone());

for old_parent_node in &lhs_type.parent_nodes {
analysis_data.data_flow_graph.add_path(
Expand All @@ -263,7 +263,7 @@ pub(crate) fn assign_arithmetic_type(
rhs_expr.1.start_offset() as u32,
rhs_expr.1.end_offset() as u32,
)) {
cond_type.parent_nodes.insert(decision_node.clone());
cond_type.parent_nodes.push(decision_node.clone());

for old_parent_node in &rhs_type.parent_nodes {
analysis_data.data_flow_graph.add_path(
Expand Down
7 changes: 3 additions & 4 deletions src/analyzer/expr/binop/assignment_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ pub(crate) fn analyze(
.data_flow_graph
.add_node(assignment_node.clone());

assign_value_type.parent_nodes.insert(assignment_node);
assign_value_type.parent_nodes.push(assignment_node);

if !context.inside_assignment_op && !var_id.starts_with("$_") {
if let Some((start_offset, end_offset)) = context.for_loop_init_bounds {
Expand All @@ -210,7 +210,7 @@ pub(crate) fn analyze(

analysis_data.data_flow_graph.add_node(for_node.clone());

assign_value_type.parent_nodes.insert(for_node);
assign_value_type.parent_nodes.push(for_node);
}
}
};
Expand Down Expand Up @@ -551,12 +551,11 @@ pub(crate) fn add_dataflow_to_assignment(
}

let parent_nodes = &assignment_type.parent_nodes;
let mut new_parent_nodes = FxHashSet::default();

let new_parent_node =
DataFlowNode::get_for_assignment(var_id.to_string(), statements_analyzer.get_hpos(var_pos));
data_flow_graph.add_node(new_parent_node.clone());
new_parent_nodes.insert(new_parent_node.clone());
let new_parent_nodes = vec![new_parent_node.clone()];

for parent_node in parent_nodes {
data_flow_graph.add_path(
Expand Down
2 changes: 1 addition & 1 deletion src/analyzer/expr/binop/concat_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pub(crate) fn analyze_concat_nodes(
get_string()
};

result_type.parent_nodes.insert(decision_node.clone());
result_type.parent_nodes.push(decision_node.clone());

analysis_data.data_flow_graph.add_node(decision_node);

Expand Down
11 changes: 5 additions & 6 deletions src/analyzer/expr/call/arguments_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use hakana_reflection_info::EFFECT_WRITE_LOCAL;
use hakana_reflection_info::data_flow::node::DataFlowNode;
use hakana_reflection_info::taint::SinkType;
use hakana_str::{Interner, StrId};
use rustc_hash::{FxHashMap, FxHashSet};
use rustc_hash::FxHashMap;

use crate::expr::binop::assignment_analyzer;
use crate::expr::call_analyzer::get_generic_param_for_offset;
Expand All @@ -33,7 +33,8 @@ use hakana_type::template::{
};
use hakana_type::type_expander::{self, StaticClassType, TypeExpansionOptions};
use hakana_type::{
add_optional_union_type, combine_optional_union_types, get_arraykey, get_mixed_any, wrap_atomic,
add_optional_union_type, combine_optional_union_types, extend_dataflow_uniquely, get_arraykey,
get_mixed_any, wrap_atomic,
};
use indexmap::IndexMap;
use oxidized::ast_defs::ParamKind;
Expand Down Expand Up @@ -1055,13 +1056,11 @@ fn handle_possibly_matching_inout_param(
Some(statements_analyzer.get_hpos(function_call_pos)),
);

inout_type.parent_nodes = FxHashSet::from_iter([out_node.clone()]);
inout_type.parent_nodes = vec![out_node.clone()];

analysis_data.data_flow_graph.add_node(out_node);
} else {
inout_type
.parent_nodes
.extend(arg_type.parent_nodes.clone());
extend_dataflow_uniquely(&mut inout_type.parent_nodes, arg_type.parent_nodes.clone());
}

assignment_analyzer::analyze_inout_param(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use oxidized::{
aast,
ast_defs::{self, Pos},
};
use rustc_hash::{FxHashMap, FxHashSet};
use rustc_hash::FxHashMap;

use crate::expr::fetch::array_fetch_analyzer::add_array_fetch_dataflow;
use crate::stmt_analyzer::AnalysisError;
Expand Down Expand Up @@ -350,7 +350,7 @@ fn handle_shapes_static_method(
);
}

new_type.parent_nodes = FxHashSet::from_iter([assignment_node.clone()]);
new_type.parent_nodes = vec![assignment_node.clone()];

analysis_data.data_flow_graph.add_node(assignment_node);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ fn add_dataflow(
}
}

stmt_type.parent_nodes.insert(function_call_node);
stmt_type.parent_nodes.push(function_call_node);

stmt_type
}
Expand Down
8 changes: 4 additions & 4 deletions src/analyzer/expr/call/method_call_return_type_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::rc::Rc;
use hakana_reflection_info::functionlike_identifier::FunctionLikeIdentifier;
use hakana_str::{Interner, StrId};
use oxidized::{aast, ast_defs};
use rustc_hash::{FxHashMap, FxHashSet};
use rustc_hash::FxHashMap;

use hakana_reflection_info::classlike_info::ClassLikeInfo;
use hakana_reflection_info::data_flow::graph::GraphKind;
Expand Down Expand Up @@ -343,7 +343,7 @@ fn add_dataflow(

let mut var_type_inner = (**var_type).clone();

var_type_inner.parent_nodes = FxHashSet::from_iter([after_construct_node.clone()]);
var_type_inner.parent_nodes = vec![after_construct_node.clone()];

data_flow_graph.add_node(after_construct_node);

Expand Down Expand Up @@ -401,7 +401,7 @@ fn add_dataflow(

let mut context_type_inner = (**context_type).clone();

context_type_inner.parent_nodes = FxHashSet::from_iter([var_node.clone()]);
context_type_inner.parent_nodes = vec![var_node.clone()];

*context_type = Rc::new(context_type_inner);

Expand Down Expand Up @@ -452,7 +452,7 @@ fn add_dataflow(

data_flow_graph.add_node(method_call_node.clone());

return_type_candidate.parent_nodes = FxHashSet::from_iter([method_call_node.clone()]);
return_type_candidate.parent_nodes = vec![method_call_node.clone()];

return_type_candidate
}
8 changes: 4 additions & 4 deletions src/analyzer/expr/call/new_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use hakana_reflection_info::data_flow::node::DataFlowNode;
use hakana_reflection_info::functionlike_info::FunctionLikeInfo;
use hakana_str::StrId;
use hakana_type::template::standin_type_replacer::get_most_specific_type_from_bounds;
use rustc_hash::{FxHashMap, FxHashSet};
use rustc_hash::FxHashMap;

use crate::expr::call_analyzer::{check_method_args, get_generic_param_for_offset};
use crate::expression_analyzer;
Expand Down Expand Up @@ -660,7 +660,7 @@ fn add_dataflow<'a>(

data_flow_graph.add_node(new_call_node.clone());

return_type_candidate.parent_nodes = FxHashSet::from_iter([new_call_node.clone()]);
return_type_candidate.parent_nodes = vec![new_call_node.clone()];

if from_classname {
let descendants = codebase.get_all_descendants(&method_id.0);
Expand All @@ -683,7 +683,7 @@ fn add_dataflow<'a>(

data_flow_graph.add_node(new_call_node.clone());

return_type_candidate.parent_nodes.insert(new_call_node);
return_type_candidate.parent_nodes.push(new_call_node);
}
}
} else {
Expand All @@ -695,7 +695,7 @@ fn add_dataflow<'a>(

data_flow_graph.add_node(new_call_node.clone());

return_type_candidate.parent_nodes = FxHashSet::from_iter([new_call_node.clone()]);
return_type_candidate.parent_nodes = vec![new_call_node.clone()];
}

return_type_candidate
Expand Down
3 changes: 1 addition & 2 deletions src/analyzer/expr/closure_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use hakana_type::type_expander;
use hakana_type::type_expander::TypeExpansionOptions;
use hakana_type::wrap_atomic;
use oxidized::aast;
use rustc_hash::FxHashSet;

pub(crate) fn analyze(
statements_analyzer: &StatementsAnalyzer,
Expand Down Expand Up @@ -123,7 +122,7 @@ pub(crate) fn analyze(
.data_flow_graph
.add_node(application_node.clone());

closure_type.parent_nodes = FxHashSet::from_iter([application_node]);
closure_type.parent_nodes = vec![application_node];
}

analysis_data.expr_types.insert(
Expand Down
9 changes: 4 additions & 5 deletions src/analyzer/expr/collection_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use oxidized::{
ast_defs::Pos,
tast::{KvcKind, VcKind},
};
use rustc_hash::FxHashSet;

use crate::{expression_analyzer, scope_analyzer::ScopeAnalyzer};
use crate::{function_analysis_data::FunctionAnalysisData, stmt_analyzer::AnalysisError};
Expand All @@ -29,7 +28,7 @@ pub(crate) struct ArrayCreationInfo {
item_key_atomic_types: Vec<TAtomic>,
item_value_atomic_types: Vec<TAtomic>,
known_items: Vec<(TAtomic, TUnion)>,
parent_nodes: FxHashSet<DataFlowNode>,
parent_nodes: Vec<DataFlowNode>,
effects: u8,
}

Expand All @@ -38,7 +37,7 @@ impl ArrayCreationInfo {
Self {
item_key_atomic_types: Vec::new(),
item_value_atomic_types: Vec::new(),
parent_nodes: FxHashSet::default(),
parent_nodes: Vec::new(),
known_items: Vec::new(),
effects: 0,
}
Expand Down Expand Up @@ -529,7 +528,7 @@ fn add_array_value_dataflow(
);
}

array_creation_info.parent_nodes.insert(new_parent_node);
array_creation_info.parent_nodes.push(new_parent_node);
}

fn add_array_key_dataflow(
Expand Down Expand Up @@ -590,5 +589,5 @@ fn add_array_key_dataflow(
);
}

array_creation_info.parent_nodes.insert(new_parent_node);
array_creation_info.parent_nodes.push(new_parent_node);
}
7 changes: 3 additions & 4 deletions src/analyzer/expr/fetch/array_fetch_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use hakana_type::{
type_comparator::{type_comparison_result::TypeComparisonResult, union_type_comparator},
};
use oxidized::{aast, ast_defs::Pos};
use rustc_hash::FxHashSet;

use crate::{
expr::expression_identifier, function_analysis_data::FunctionAnalysisData,
Expand Down Expand Up @@ -258,10 +257,10 @@ pub(crate) fn add_array_fetch_dataflow(
}
}

value_type.parent_nodes.insert(new_parent_node.clone());
value_type.parent_nodes.push(new_parent_node.clone());

if let Some(array_key_node) = &array_key_node {
key_type.parent_nodes.insert(array_key_node.clone());
key_type.parent_nodes.push(array_key_node.clone());
}
}
}
Expand Down Expand Up @@ -999,7 +998,7 @@ pub(crate) fn handle_array_access_on_mixed(
}
if let Some(stmt_type) = stmt_type {
let mut stmt_type_new = stmt_type.clone();
stmt_type_new.parent_nodes = FxHashSet::from_iter([new_parent_node.clone()]);
stmt_type_new.parent_nodes = vec![new_parent_node.clone()];
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/analyzer/expr/fetch/atomic_property_fetch_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use hakana_type::{
};
use indexmap::IndexMap;
use oxidized::{aast::Expr, ast_defs::Pos};
use rustc_hash::{FxHashMap, FxHashSet};
use rustc_hash::FxHashMap;

pub(crate) fn analyze(
statements_analyzer: &StatementsAnalyzer,
Expand Down Expand Up @@ -437,7 +437,7 @@ fn add_property_dataflow(
);
}

stmt_type.parent_nodes.insert(property_node.clone());
stmt_type.parent_nodes.push(property_node.clone());
}
}
} else {
Expand Down Expand Up @@ -467,9 +467,7 @@ fn add_property_dataflow(
.data_flow_graph
.add_node(localized_property_node.clone());

stmt_type
.parent_nodes
.insert(localized_property_node.clone());
stmt_type.parent_nodes.push(localized_property_node.clone());

stmt_type
}
Expand Down Expand Up @@ -528,7 +526,7 @@ pub(crate) fn add_unspecialized_property_fetch_dataflow(

let mut stmt_type = stmt_type.clone();

stmt_type.parent_nodes = FxHashSet::from_iter([localized_property_node.clone()]);
stmt_type.parent_nodes = vec![localized_property_node.clone()];

stmt_type
}
2 changes: 1 addition & 1 deletion src/analyzer/expr/pipe_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub(crate) fn analyze(
false,
);

pipe_expr_type.parent_nodes.insert(parent_node.clone());
pipe_expr_type.parent_nodes.push(parent_node.clone());
analysis_data.data_flow_graph.add_node(parent_node);
}

Expand Down
4 changes: 2 additions & 2 deletions src/analyzer/expr/shape_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub(crate) fn analyze(
) -> Result<(), AnalysisError> {
let codebase = statements_analyzer.get_codebase();

let mut parent_nodes = FxHashSet::default();
let mut parent_nodes = vec![];

let mut effects = 0;

Expand Down Expand Up @@ -151,7 +151,7 @@ pub(crate) fn analyze(
},
value_expr,
) {
parent_nodes.insert(new_parent_node);
parent_nodes.push(new_parent_node);
}

known_items.insert(name, (false, Arc::new(value_item_type)));
Expand Down
Loading

0 comments on commit 6b32a27

Please sign in to comment.