chore: Switch from hashmap and hashset to indexmap and indexset

This commit is contained in:
Kasey White
2023-01-21 17:25:09 -05:00
committed by Lucas
parent 91bd0d1d77
commit e8fb386bdc
10 changed files with 109 additions and 76 deletions

View File

@@ -1,5 +1,6 @@
use std::{collections::HashSet, sync::Arc};
use std::sync::Arc;
use indexmap::IndexSet;
use uplc::builtins::DefaultFunction;
use crate::{
@@ -127,8 +128,8 @@ pub enum Air {
TupleClause {
scope: Vec<u64>,
tipo: Arc<Type>,
indices: HashSet<(usize, String)>,
predefined_indices: HashSet<(usize, String)>,
indices: IndexSet<(usize, String)>,
predefined_indices: IndexSet<(usize, String)>,
subject_name: String,
count: usize,
complex_clause: bool,

View File

@@ -1,10 +1,6 @@
use std::{
cell::RefCell,
collections::{HashMap, HashSet},
sync::Arc,
};
use std::{cell::RefCell, sync::Arc};
use indexmap::IndexMap;
use indexmap::{IndexMap, IndexSet};
use itertools::Itertools;
use uplc::{
ast::{
@@ -72,7 +68,7 @@ pub enum ClauseProperties {
needs_constr_var: bool,
is_complex_clause: bool,
original_subject_name: String,
defined_tuple_indices: HashSet<(usize, String)>,
defined_tuple_indices: IndexSet<(usize, String)>,
},
}
@@ -92,7 +88,7 @@ impl ClauseProperties {
needs_constr_var: false,
is_complex_clause: false,
original_subject_name: subject_name,
defined_tuple_indices: HashSet::new(),
defined_tuple_indices: IndexSet::new(),
}
} else {
ClauseProperties::ConstrClause {
@@ -542,6 +538,32 @@ pub fn list_access_to_uplc(
.into(),
}
} else if names.is_empty() {
// Maybe check list is actually empty or should we leave that to when .. is only
// this would replace term.into() if we decide to
// body: choose_list(
// apply_wrap(
// Term::Builtin(DefaultFunction::TailList).force_wrap(),
// Term::Var(Name {
// text: format!(
// "tail_index_{}_{}",
// current_index, id_list[current_index]
// ),
// unique: 0.into(),
// }),
// ),
// term,
// apply_wrap(
// apply_wrap(
// Term::Builtin(DefaultFunction::Trace).force_wrap(),
// Term::Constant(UplcConstant::String(
// "List contains more items".to_string(),
// )),
// ),
// Term::Delay(Term::Error.into()),
// )
// .force_wrap(),
// )
// .into(),
Term::Lambda {
parameter_name: Name {
text: format!("tail_index_{}_{}", current_index, id_list[current_index]),
@@ -757,7 +779,7 @@ pub fn match_ir_for_recursion(
}
}
pub fn find_generics_to_replace(tipo: &mut Arc<Type>, generic_types: &HashMap<u64, Arc<Type>>) {
pub fn find_generics_to_replace(tipo: &mut Arc<Type>, generic_types: &IndexMap<u64, Arc<Type>>) {
if let Some(id) = tipo.get_generic() {
//If generic does not have a type we know of like a None in option then just use same type
*tipo = generic_types.get(&id).unwrap_or(tipo).clone();
@@ -983,7 +1005,7 @@ pub fn wrap_validator_args(term: Term<Name>, arguments: Vec<TypedArg>) -> Term<N
pub fn monomorphize(
ir: Vec<Air>,
generic_types: HashMap<u64, Arc<Type>>,
generic_types: IndexMap<u64, Arc<Type>>,
full_type: &Arc<Type>,
) -> (String, Vec<Air>) {
let mut new_air = ir.clone();
@@ -1390,14 +1412,14 @@ pub fn monomorphize(
(new_name, new_air)
}
pub fn handle_func_deps_ir(
dep_ir: &mut Vec<Air>,
pub fn handle_func_dependencies_ir(
dependencies_ir: &mut Vec<Air>,
funt_comp: &FuncComponents,
func_components: &IndexMap<FunctionAccessKey, FuncComponents>,
defined_functions: &mut HashMap<FunctionAccessKey, ()>,
defined_functions: &mut IndexMap<FunctionAccessKey, ()>,
func_index_map: &IndexMap<FunctionAccessKey, Vec<u64>>,
func_scope: &[u64],
to_be_defined: &mut HashMap<FunctionAccessKey, ()>,
to_be_defined: &mut IndexMap<FunctionAccessKey, ()>,
) {
let mut funt_comp = funt_comp.clone();
@@ -1448,9 +1470,9 @@ pub fn handle_func_deps_ir(
temp_ir.append(&mut recursion_ir);
temp_ir.append(dep_ir);
temp_ir.append(dependencies_ir);
*dep_ir = temp_ir;
*dependencies_ir = temp_ir;
if get_common_ancestor(dep_scope, func_scope) == func_scope.to_vec() {
defined_functions.insert(dependency, ());
}
@@ -1501,7 +1523,7 @@ pub fn handle_recursion_ir(
}
pub fn lookup_data_type_by_tipo(
data_types: HashMap<DataTypeKey, &TypedDataType>,
data_types: IndexMap<DataTypeKey, &TypedDataType>,
tipo: &Type,
) -> Option<DataType<Arc<Type>>> {
match tipo {
@@ -1536,7 +1558,7 @@ pub fn lookup_data_type_by_tipo(
pub fn check_replaceable_opaque_type(
t: &Arc<Type>,
data_types: &HashMap<DataTypeKey, &TypedDataType>,
data_types: &IndexMap<DataTypeKey, &TypedDataType>,
) -> bool {
let data_type = lookup_data_type_by_tipo(data_types.clone(), t);
@@ -1548,12 +1570,12 @@ pub fn check_replaceable_opaque_type(
}
}
pub fn replace_opaque_type(t: &mut Arc<Type>, data_types: HashMap<DataTypeKey, &TypedDataType>) {
pub fn replace_opaque_type(t: &mut Arc<Type>, data_types: IndexMap<DataTypeKey, &TypedDataType>) {
if check_replaceable_opaque_type(t, &data_types) && matches!(&**t, Type::App { .. }) {
let data_type = lookup_data_type_by_tipo(data_types.clone(), t).unwrap();
let new_type_fields = data_type.typed_parameters.clone();
let mut generics_type_map: HashMap<u64, Arc<Type>> = HashMap::new();
let mut generics_type_map: IndexMap<u64, Arc<Type>> = IndexMap::new();
for (tipo, param) in new_type_fields.iter().zip(t.arg_types().unwrap()) {
let mut map = generics_type_map.into_iter().collect_vec();

View File

@@ -1,10 +1,6 @@
use std::{
collections::{HashMap, HashSet},
sync::Arc,
vec,
};
use std::{sync::Arc, vec};
use indexmap::IndexMap;
use indexmap::{IndexMap, IndexSet};
use itertools::Itertools;
use uplc::{
ast::{
@@ -28,9 +24,10 @@ use crate::{
builder::{
check_replaceable_opaque_type, check_when_pattern_needs, constants_ir,
convert_constants_to_data, convert_data_to_type, convert_type_to_data, get_common_ancestor,
get_generics_and_type, handle_func_deps_ir, handle_recursion_ir, list_access_to_uplc,
lookup_data_type_by_tipo, monomorphize, rearrange_clauses, replace_opaque_type,
wrap_validator_args, ClauseProperties, DataTypeKey, FuncComponents, FunctionAccessKey,
get_generics_and_type, handle_func_dependencies_ir, handle_recursion_ir,
list_access_to_uplc, lookup_data_type_by_tipo, monomorphize, rearrange_clauses,
replace_opaque_type, wrap_validator_args, ClauseProperties, DataTypeKey, FuncComponents,
FunctionAccessKey,
},
expr::TypedExpr,
tipo::{
@@ -41,32 +38,32 @@ use crate::{
};
pub struct CodeGenerator<'a> {
defined_functions: HashMap<FunctionAccessKey, ()>,
functions: &'a HashMap<FunctionAccessKey, &'a TypedFunction>,
// type_aliases: &'a HashMap<(String, String), &'a TypeAlias<Arc<tipo::Type>>>,
data_types: &'a HashMap<DataTypeKey, &'a TypedDataType>,
module_types: &'a HashMap<String, TypeInfo>,
defined_functions: IndexMap<FunctionAccessKey, ()>,
functions: &'a IndexMap<FunctionAccessKey, &'a TypedFunction>,
// type_aliases: &'a IndexMap<(String, String), &'a TypeAlias<Arc<tipo::Type>>>,
data_types: &'a IndexMap<DataTypeKey, &'a TypedDataType>,
module_types: &'a IndexMap<String, TypeInfo>,
id_gen: IdGenerator,
needs_field_access: bool,
zero_arg_functions: HashMap<FunctionAccessKey, Vec<Air>>,
zero_arg_functions: IndexMap<FunctionAccessKey, Vec<Air>>,
}
impl<'a> CodeGenerator<'a> {
pub fn new(
functions: &'a HashMap<FunctionAccessKey, &'a TypedFunction>,
// type_aliases: &'a HashMap<(String, String), &'a TypeAlias<Arc<tipo::Type>>>,
data_types: &'a HashMap<DataTypeKey, &'a TypedDataType>,
module_types: &'a HashMap<String, TypeInfo>,
functions: &'a IndexMap<FunctionAccessKey, &'a TypedFunction>,
// type_aliases: &'a IndexMap<(String, String), &'a TypeAlias<Arc<tipo::Type>>>,
data_types: &'a IndexMap<DataTypeKey, &'a TypedDataType>,
module_types: &'a IndexMap<String, TypeInfo>,
) -> Self {
CodeGenerator {
defined_functions: HashMap::new(),
defined_functions: IndexMap::new(),
functions,
// type_aliases,
data_types,
module_types,
id_gen: IdGenerator::new(),
needs_field_access: false,
zero_arg_functions: HashMap::new(),
zero_arg_functions: IndexMap::new(),
}
}
@@ -1040,7 +1037,7 @@ impl<'a> CodeGenerator<'a> {
PatternConstructor::Record { field_map, .. } => field_map.clone().unwrap(),
};
let mut type_map: HashMap<String, Arc<Type>> = HashMap::new();
let mut type_map: IndexMap<String, Arc<Type>> = IndexMap::new();
for (index, arg) in tipo.arg_types().unwrap().iter().enumerate() {
let label = constructor_type.arguments[index].label.clone().unwrap();
@@ -1092,7 +1089,7 @@ impl<'a> CodeGenerator<'a> {
});
}
} else {
let mut type_map: HashMap<usize, Arc<Type>> = HashMap::new();
let mut type_map: IndexMap<usize, Arc<Type>> = IndexMap::new();
for (index, arg) in tipo.arg_types().unwrap().iter().enumerate() {
let field_type = arg.clone();
@@ -1380,7 +1377,7 @@ impl<'a> CodeGenerator<'a> {
needs_constr_var: false,
is_complex_clause: false,
original_subject_name: item_name.clone(),
defined_tuple_indices: HashSet::new(),
defined_tuple_indices: IndexSet::new(),
};
let mut inner_pattern_vec = vec![];
@@ -1406,7 +1403,7 @@ impl<'a> CodeGenerator<'a> {
scope,
tipo: pattern_type.clone(),
indices: defined_indices,
predefined_indices: HashSet::new(),
predefined_indices: IndexSet::new(),
subject_name: clause_properties.original_subject_name().to_string(),
count: elems.len(),
complex_clause: false,
@@ -1625,7 +1622,7 @@ impl<'a> CodeGenerator<'a> {
}
};
let mut type_map: HashMap<String, Arc<Type>> = HashMap::new();
let mut type_map: IndexMap<String, Arc<Type>> = IndexMap::new();
for (index, arg) in tipo.arg_types().unwrap().iter().enumerate() {
let label = constructor_type.arguments[index].label.clone().unwrap();
@@ -1694,7 +1691,7 @@ impl<'a> CodeGenerator<'a> {
});
}
} else {
let mut type_map: HashMap<usize, Arc<Type>> = HashMap::new();
let mut type_map: IndexMap<usize, Arc<Type>> = IndexMap::new();
for (index, arg) in tipo.arg_types().unwrap().iter().enumerate() {
let field_type = arg.clone();
@@ -1834,8 +1831,8 @@ impl<'a> CodeGenerator<'a> {
);
let mut final_func_dep_ir = IndexMap::new();
let mut zero_arg_defined_functions = HashMap::new();
let mut to_be_defined = HashMap::new();
let mut zero_arg_defined_functions = IndexMap::new();
let mut to_be_defined = IndexMap::new();
let mut dependency_map = IndexMap::new();
let mut dependency_vec = vec![];
@@ -1865,7 +1862,7 @@ impl<'a> CodeGenerator<'a> {
if !funt_comp.args.is_empty() {
// deal with function dependencies
handle_func_deps_ir(
handle_func_dependencies_ir(
&mut dep_ir,
funt_comp,
&func_components,
@@ -1877,16 +1874,17 @@ impl<'a> CodeGenerator<'a> {
final_func_dep_ir.insert(func, dep_ir);
} else {
// since zero arg functions are run at compile time we need to pull all deps
let mut defined_functions = HashMap::new();
// note anon functions are not included in the above. They exist in a function anyway
let mut defined_functions = IndexMap::new();
// deal with function dependencies in zero arg functions
handle_func_deps_ir(
handle_func_dependencies_ir(
&mut dep_ir,
funt_comp,
&func_components,
&mut defined_functions,
&func_index_map,
func_scope,
&mut HashMap::new(),
&mut IndexMap::new(),
);
let mut final_zero_arg_ir = dep_ir;
@@ -1895,7 +1893,8 @@ impl<'a> CodeGenerator<'a> {
self.convert_opaque_type_to_inner_ir(&mut final_zero_arg_ir);
self.zero_arg_functions.insert(func, final_zero_arg_ir);
// zero arg functions don't contain the dependencies since they are pre-evaluated
// As such we add functions to defined only after dependencies for all other functions are calculated
for (key, val) in defined_functions.into_iter() {
zero_arg_defined_functions.insert(key, val);
}
@@ -2095,7 +2094,7 @@ impl<'a> CodeGenerator<'a> {
let param_types = constructor.tipo.arg_types().unwrap();
let mut generics_type_map: HashMap<u64, Arc<Type>> = HashMap::new();
let mut generics_type_map: IndexMap<u64, Arc<Type>> = IndexMap::new();
for (index, arg) in function.arguments.iter().enumerate() {
if arg.tipo.is_generic() {
@@ -2133,7 +2132,7 @@ impl<'a> CodeGenerator<'a> {
to_be_defined_map.insert(function_key.clone(), scope.to_vec());
} else {
to_be_defined_map.insert(function_key.clone(), scope.to_vec());
let mut func_calls = HashMap::new();
let mut func_calls = IndexMap::new();
for ir in func_ir.clone().into_iter() {
if let Air::Var {
@@ -2169,8 +2168,8 @@ impl<'a> CodeGenerator<'a> {
} else if let (Some(function), Type::Fn { .. }) =
(function, &*tipo)
{
let mut generics_type_map: HashMap<u64, Arc<Type>> =
HashMap::new();
let mut generics_type_map: IndexMap<u64, Arc<Type>> =
IndexMap::new();
let param_types = tipo.arg_types().unwrap();
@@ -4267,7 +4266,7 @@ impl<'a> CodeGenerator<'a> {
let record = arg_stack.pop().unwrap();
let mut args = HashMap::new();
let mut args = IndexMap::new();
let mut unchanged_field_indices = vec![];
let mut prev_index = 0;
for (index, tipo) in indices