Skip to content

Commit

Permalink
Store template types in vecs for compactness
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Mar 21, 2024
1 parent 4994645 commit 167c98b
Show file tree
Hide file tree
Showing 29 changed files with 175 additions and 152 deletions.
11 changes: 3 additions & 8 deletions src/analyzer/dataflow/program_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ fn get_specialized_sources(

new_source
.specialized_calls
.entry(specialization_key.clone())
.entry(*specialization_key)
.or_default()
.insert(new_source.id.clone());

Expand All @@ -227,11 +227,7 @@ fn get_specialized_sources(
if source.specialized_calls.is_empty()
|| source.specialized_calls.contains_key(specialization)
{
let new_id = format!(
"{}-{}",
source.id,
format!("{}:{}", specialization.0 .0, specialization.1)
);
let new_id = format!("{}-{}:{}", source.id, specialization.0 .0, specialization.1);

if graph.forward_edges.contains_key(&new_id) {
let mut new_source = (*source).clone();
Expand All @@ -247,8 +243,7 @@ fn get_specialized_sources(
} else {
for (key, map) in &source.specialized_calls {
if map.contains(&source.id) {
let new_forward_edge_id =
format!("{}-{}", source.id, format!("{}:{}", key.0 .0, key.1));
let new_forward_edge_id = format!("{}-{}:{}", source.id, key.0 .0, key.1);

if graph.forward_edges.contains_key(&new_forward_edge_id) {
let mut new_source = (*source).clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ fn add_instance_property_assignment_dataflow(
if !stmt_type_inner
.parent_nodes
.iter()
.any(|n| &n.id == &var_node.id)
.any(|n| n.id == var_node.id)
{
stmt_type_inner.parent_nodes.push(var_node.clone());
}
Expand Down
42 changes: 23 additions & 19 deletions src/analyzer/expr/call/arguments_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ pub(crate) fn check_arguments_match(
if lower_bounds.len() == 1 {
class_generic_params
.entry(*template_name)
.or_insert_with(FxHashMap::default)
.insert(
.or_insert_with(Vec::new)
.push((
*class,
Arc::new(lower_bounds.first().unwrap().bound_type.clone()),
);
));
}
}
}
Expand Down Expand Up @@ -520,7 +520,7 @@ pub(crate) fn check_arguments_match(
}

fn adjust_param_type(
class_generic_params: &IndexMap<StrId, FxHashMap<StrId, Arc<TUnion>>>,
class_generic_params: &IndexMap<StrId, Vec<(StrId, Arc<TUnion>)>>,
param_type: &mut TUnion,
codebase: &CodebaseInfo,
mut arg_value_type: TUnion,
Expand Down Expand Up @@ -712,10 +712,10 @@ fn handle_closure_arg(
)),
)
})
.collect::<FxHashMap<_, _>>(),
.collect::<Vec<_>>(),
)
})
.collect::<IndexMap<_, _>>(),
.collect(),
IndexMap::new(),
);

Expand Down Expand Up @@ -806,7 +806,7 @@ fn handle_closure_arg(
| StrId::LIB_KEYSET_MAP_WITH_KEY
| StrId::LIB_DICT_MAP_WITH_KEY_ASYNC
| StrId::LIB_DICT_FROM_KEYS
| StrId::LIB_DICT_FROM_KEYS_ASYNC => {
| StrId::LIB_DICT_FROM_KEYS_ASYNC => {
if param_offset == 0 {
if let Some(ref mut signature_type) = param_storage.signature_type {
add_array_fetch_dataflow(
Expand All @@ -833,7 +833,7 @@ fn handle_closure_arg(
}

fn map_class_generic_params(
class_generic_params: &IndexMap<StrId, FxHashMap<StrId, Arc<TUnion>>>,
class_generic_params: &IndexMap<StrId, Vec<(StrId, Arc<TUnion>)>>,
param_type: &mut TUnion,
codebase: &CodebaseInfo,
interner: &Interner,
Expand Down Expand Up @@ -1083,7 +1083,7 @@ fn refine_template_result_for_functionlike(
classlike_storage: Option<&ClassLikeInfo>,
calling_classlike_storage: Option<&ClassLikeInfo>,
functionlike_storage: &FunctionLikeInfo,
class_template_params: &IndexMap<StrId, FxHashMap<StrId, Arc<TUnion>>>,
class_template_params: &IndexMap<StrId, Vec<(StrId, Arc<TUnion>)>>,
) {
let template_types = get_template_types_for_call(
codebase,
Expand Down Expand Up @@ -1117,10 +1117,11 @@ pub(crate) fn get_template_types_for_call(
declaring_classlike_storage: Option<&ClassLikeInfo>,
appearing_class_name: Option<&StrId>,
calling_classlike_storage: Option<&ClassLikeInfo>,
existing_template_types: &IndexMap<StrId, FxHashMap<StrId, Arc<TUnion>>>,
class_template_params: &IndexMap<StrId, FxHashMap<StrId, Arc<TUnion>>>,
existing_template_types: &[(StrId, Vec<(StrId, Arc<TUnion>)>)],
class_template_params: &IndexMap<StrId, Vec<(StrId, Arc<TUnion>)>>,
) -> IndexMap<StrId, FxHashMap<StrId, TUnion>> {
let mut template_types = existing_template_types.clone();
let mut template_types: IndexMap<StrId, Vec<(StrId, Arc<TUnion>)>> =
IndexMap::from_iter(existing_template_types.to_owned());

if let Some(declaring_classlike_storage) = declaring_classlike_storage {
let calling_has_extends = if let Some(calling_classlike_storage) = calling_classlike_storage
Expand Down Expand Up @@ -1177,8 +1178,8 @@ pub(crate) fn get_template_types_for_call(

template_types
.entry(*template_name)
.or_insert_with(FxHashMap::default)
.insert(declaring_classlike_storage.name, output_type);
.or_insert_with(Vec::new)
.push((declaring_classlike_storage.name, output_type));
}
}
}
Expand All @@ -1187,16 +1188,19 @@ pub(crate) fn get_template_types_for_call(
for (key, type_) in type_map {
template_types
.entry(*template_name)
.or_insert_with(FxHashMap::default)
.insert(
.or_insert_with(Vec::new)
.push((
*key,
class_template_params
.get(template_name)
.unwrap_or(&FxHashMap::default())
.get(key)
.unwrap_or(&vec![])
.iter()
.filter(|(k, _)| k == key)
.map(|(_, v)| v)
.next()
.cloned()
.unwrap_or(type_.clone()),
);
));
}
}
}
Expand Down
32 changes: 15 additions & 17 deletions src/analyzer/expr/call/class_template_param_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub(crate) fn collect(
if class_storage.name == static_class_storage.name
&& !static_class_storage.template_types.is_empty()
{
for (i, type_name) in class_storage.template_types.keys().enumerate() {
for (i, (type_name, _)) in class_storage.template_types.iter().enumerate() {
if let Some(type_param) = lhs_type_params.get(i) {
class_template_params
.entry(*type_name)
Expand Down Expand Up @@ -139,22 +139,20 @@ pub(crate) fn resolve_template_param(
..
} = &type_extends_atomic
{
if static_class_storage
if let Some(entry) = static_class_storage
.template_types
.get(param_name)
.unwrap_or(&FxHashMap::default())
.contains_key(defining_entity)
.iter()
.enumerate()
.find(|(_, (k, _))| k == param_name)
{
let mapped_offset = static_class_storage.template_types.get_index_of(param_name);

if let Some(mapped_offset) = mapped_offset {
if let Some(type_param) = type_params.get(mapped_offset) {
output_type_extends = Some(add_optional_union_type(
type_param.clone(),
output_type_extends.as_ref(),
codebase,
));
}
let mapped_offset = entry.0;

if let Some(type_param) = type_params.get(mapped_offset) {
output_type_extends = Some(add_optional_union_type(
type_param.clone(),
output_type_extends.as_ref(),
codebase,
));
}
} else if let Some(input_type_extends) = static_class_storage
.template_extended_params
Expand Down Expand Up @@ -193,7 +191,7 @@ fn expand_type(
input_type_extends: &Arc<TUnion>,
e: &FxHashMap<StrId, IndexMap<StrId, Arc<TUnion>>>,
static_classlike_name: &StrId,
static_template_types: &IndexMap<StrId, FxHashMap<StrId, Arc<TUnion>>>,
static_template_types: &Vec<(StrId, Vec<(StrId, Arc<TUnion>)>)>,
) -> Vec<TAtomic> {
let mut output_type_extends = Vec::new();

Expand All @@ -205,7 +203,7 @@ fn expand_type(
} = type_extends_atomic
{
if static_classlike_name != defining_entity
|| !static_template_types.contains_key(param_name)
|| !static_template_types.iter().any(|(k, _)| k == param_name)
{
if let Some(extended_type_map) = e.get(defining_entity) {
extended_type_map.get(param_name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ pub(crate) fn analyze(
let functionlike_template_types = functionlike_storage.template_types.clone();

let mut template_result = TemplateResult::new(
functionlike_template_types,
IndexMap::from_iter(functionlike_template_types),
class_template_params.clone().unwrap_or(IndexMap::new()),
);

Expand Down
7 changes: 3 additions & 4 deletions src/analyzer/expr/call/function_call_return_type_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,13 @@ pub(crate) fn fetch(
if !function_storage.template_types.is_empty()
&& !function_storage.template_types.is_empty()
{
let fn_id = statements_analyzer
.get_interner()
let interner = statements_analyzer.get_interner();
let fn_id = interner
.get(
format!(
"fn-{}",
match functionlike_id {
FunctionLikeIdentifier::Function(function_id) =>
function_id.0.to_string(),
FunctionLikeIdentifier::Function(function_id) => function_id.0,
FunctionLikeIdentifier::Method(_, _) => panic!(),
_ => {
panic!()
Expand Down
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 @@ -343,7 +343,7 @@ fn analyze_named_constructor(
if expr.1.is_empty() {
IndexMap::new()
} else {
storage.template_types.clone()
IndexMap::from_iter(storage.template_types.clone())
},
IndexMap::new(),
);
Expand Down Expand Up @@ -401,7 +401,7 @@ fn analyze_named_constructor(
let placeholder_name =
format!("`_{}", analysis_data.type_variable_bounds.len());

let upper_bound = (**map.iter().next().unwrap().1).clone();
let upper_bound = (*map.iter().next().unwrap().1).clone();

let mut placeholder_lower_bounds = vec![];

Expand Down Expand Up @@ -503,7 +503,7 @@ fn analyze_named_constructor(
)),
)
})
.collect::<FxHashMap<_, _>>(),
.collect::<Vec<_>>(),
)
})
.collect::<FxHashMap<_, _>>();
Expand All @@ -518,7 +518,7 @@ fn analyze_named_constructor(
} else if let Some(Variance::Contravariant) = storage.generic_variance.get(&i) {
get_nothing()
} else {
(**base_type_map.iter().next().unwrap().1).clone()
(*base_type_map.iter().next().unwrap().1).clone()
};

generic_param_type.had_template = true;
Expand Down
8 changes: 6 additions & 2 deletions src/analyzer/expr/call_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,15 @@ pub(crate) fn get_generic_param_for_offset(
classlike_name: &StrId,
template_name: &StrId,
template_extended_params: &FxHashMap<StrId, IndexMap<StrId, Arc<TUnion>>>,
found_generic_params: &FxHashMap<StrId, FxHashMap<StrId, Arc<TUnion>>>,
found_generic_params: &FxHashMap<StrId, Vec<(StrId, Arc<TUnion>)>>,
) -> Arc<TUnion> {
if let Some(found_generic_param) =
if let Some(result_map) = found_generic_params.get(template_name) {
result_map.get(classlike_name)
result_map
.iter()
.filter(|(e, _)| e == classlike_name)
.map(|(_, v)| v)
.next()
} else {
None
}
Expand Down
8 changes: 6 additions & 2 deletions src/analyzer/expr/fetch/atomic_property_fetch_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ fn get_class_property_type(
let mut type_params = vec![];

for (_, type_map) in &declaring_class_storage.template_types {
type_params.push((**type_map.iter().next().unwrap().1).clone());
type_params.push((*type_map.iter().next().unwrap().1).clone());
}

lhs_type_part = TAtomic::TNamedObject {
Expand Down Expand Up @@ -353,7 +353,11 @@ pub(crate) fn localize_property_type(
if let TAtomic::TGenericParam { param_name, .. } = &mapped_type_atomic {
let position = property_class_storage
.template_types
.get_index_of(param_name);
.iter()
.enumerate()
.filter(|(_, (k, _))| k == param_name)
.map(|(i, _)| i)
.next();

if let Some(position) = position {
if let Some(mapped_param) = lhs_type_params.get(position) {
Expand Down
4 changes: 2 additions & 2 deletions src/analyzer/functionlike_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ impl<'a> FunctionLikeAnalyzer<'a> {

wrap_atomic(TAtomic::TGenericParam {
param_name: *param_name,
as_type: Box::new((**first_map_entry.1).clone()),
defining_entity: *first_map_entry.0,
as_type: Box::new((*first_map_entry.1).clone()),
defining_entity: first_map_entry.0,
from_class: false,
extra_types: None,
})
Expand Down
6 changes: 3 additions & 3 deletions src/code_info/classlike_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub struct ClassLikeInfo {
* (i.e. the same as the class name). This allows operations with the same-named template defined
* across multiple classes to not run into trouble.
*/
pub template_types: IndexMap<StrId, FxHashMap<StrId, Arc<TUnion>>>,
pub template_types: Vec<(StrId, Vec<(StrId, Arc<TUnion>)>)>,

/*
* A list of all the templates that are only written in the constructor.
Expand Down Expand Up @@ -216,7 +216,7 @@ impl ClassLikeInfo {
) -> ClassLikeInfo {
ClassLikeInfo {
meta_start,
constants: IndexMap::default(),
constants: IndexMap::new(),
is_populated: false,
is_stubbed: false,
is_deprecated: false,
Expand Down Expand Up @@ -263,7 +263,7 @@ impl ClassLikeInfo {
template_extended_offsets: FxHashMap::default(),
template_type_implements_count: FxHashMap::default(),
template_type_uses_count: FxHashMap::default(),
template_types: IndexMap::new(),
template_types: vec![],
used_traits: FxHashSet::default(),
name,
type_constants: FxHashMap::default(),
Expand Down
4 changes: 2 additions & 2 deletions src/code_info/data_flow/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ impl DataFlowGraph {
self.specializations
.entry(unspecialized_id.clone())
.or_default()
.insert(specialization_key.clone());
.insert(*specialization_key);

self.specialized_calls
.entry(specialization_key.clone())
.entry(*specialization_key)
.or_default()
.insert(unspecialized_id.clone());
}
Expand Down
2 changes: 1 addition & 1 deletion src/code_info/data_flow/tainted_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl TaintedNode {
unspecialized_id: unspecialized_id.clone(),
label: label.clone(),
pos: pos.as_ref().map(|p| Arc::new(*p)),
specialization_key: specialization_key.clone(),
specialization_key: *specialization_key,
taint_sinks: vec![],
previous: None,
path_types: Vec::new(),
Expand Down
Loading

0 comments on commit 167c98b

Please sign in to comment.