Skip to content

Commit

Permalink
remove hashing and super slow lambda name generation
Browse files Browse the repository at this point in the history
  • Loading branch information
bhansconnect committed Jul 1, 2023
1 parent 279b9b2 commit 3336201
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 87 deletions.
9 changes: 2 additions & 7 deletions crates/compiler/gen_dev/src/generic64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1599,12 +1599,8 @@ impl<
HelperOp::Eq,
);

let fn_name = self.lambda_name_to_string(
LambdaName::no_niche(eq_symbol),
[*arg_layout, *arg_layout].into_iter(),
None,
Layout::U8,
);
let fn_name =
self.lambda_name_to_string(LambdaName::no_niche(eq_symbol), None, Layout::U8);

self.helper_proc_symbols.extend(eq_linker_data);

Expand Down Expand Up @@ -1986,7 +1982,6 @@ impl<

let caller_string = self.lambda_name_to_string(
LambdaName::no_niche(caller_proc.proc_symbol),
std::iter::empty(),
None,
Layout::UNIT,
);
Expand Down
79 changes: 11 additions & 68 deletions crates/compiler/gen_dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,39 +318,15 @@ trait Backend<'a> {
&mut Vec<'a, CallerProc<'a>>,
);

fn lambda_name_to_string<'b, I>(
fn lambda_name_to_string(
&self,
name: LambdaName,
arguments: I,
_lambda_set: Option<InLayout>,
result: InLayout,
) -> String
where
I: Iterator<Item = InLayout<'b>>,
{
use std::fmt::Write;
use std::hash::{BuildHasher, Hash, Hasher};

) -> String {
let symbol = name.name();

let mut buf = String::with_capacity(1024);

for a in arguments {
write!(buf, "{:?}", self.interner().dbg_stable(a)).expect("capacity");
}

// lambda set should not matter; it should already be added as an argument
// but the niche of the lambda name may be the only thing differentiating two different
// implementations of a function with the same symbol
write!(buf, "{:?}", name.niche().dbg_stable(self.interner())).expect("capacity");

write!(buf, "{:?}", self.interner().dbg_stable(result)).expect("capacity");

// NOTE: due to randomness, this will not be consistent between runs
let mut state = roc_collections::all::BuildHasher::default().build_hasher();
buf.hash(&mut state);

let interns = self.interns();

let ident_string = symbol.as_str(interns);
let module_string = interns.module_ids.get_name(symbol.module_id()).unwrap();

Expand All @@ -359,7 +335,8 @@ trait Backend<'a> {
if ident_string.contains("#help") {
format!("{}_{}_1", module_string, ident_string)
} else {
format!("{}_{}_{}", module_string, ident_string, state.finish())
// Instead of wasting time hashing or looking at arguments, just use the InLayout index directly here.
format!("{}_{}_{}", module_string, ident_string, result.index())
}
}

Expand Down Expand Up @@ -392,16 +369,11 @@ trait Backend<'a> {
}

fn increment_fn_pointer(&mut self, layout: InLayout<'a>) -> Symbol {
let box_layout = self
.interner_mut()
.insert_direct_no_semantic(LayoutRepr::Boxed(layout));

let element_increment = self.debug_symbol("element_increment");
let element_increment_symbol = self.build_indirect_inc(layout);

let element_increment_string = self.lambda_name_to_string(
LambdaName::no_niche(element_increment_symbol),
[box_layout].into_iter(),
None,
Layout::UNIT,
);
Expand All @@ -412,16 +384,11 @@ trait Backend<'a> {
}

fn decrement_fn_pointer(&mut self, layout: InLayout<'a>) -> Symbol {
let box_layout = self
.interner_mut()
.insert_direct_no_semantic(LayoutRepr::Boxed(layout));

let element_decrement = self.debug_symbol("element_decrement");
let element_decrement_symbol = self.build_indirect_dec(layout);

let element_decrement_string = self.lambda_name_to_string(
LambdaName::no_niche(element_decrement_symbol),
[box_layout].into_iter(),
None,
Layout::UNIT,
);
Expand Down Expand Up @@ -462,12 +429,8 @@ trait Backend<'a> {
proc: Proc<'a>,
layout_ids: &mut LayoutIds<'a>,
) -> (Vec<u8>, Vec<Relocation>, Vec<'a, (Symbol, String)>) {
let proc_name = self.lambda_name_to_string(
proc.name,
proc.args.iter().map(|t| t.0),
proc.closure_data_layout,
proc.ret_layout,
);
let proc_name =
self.lambda_name_to_string(proc.name, proc.closure_data_layout, proc.ret_layout);

let body = self.env().arena.alloc(proc.body);

Expand Down Expand Up @@ -723,12 +686,7 @@ trait Backend<'a> {
);
}

let fn_name = self.lambda_name_to_string(
*func_sym,
arg_layouts.iter().copied(),
None,
*ret_layout,
);
let fn_name = self.lambda_name_to_string(*func_sym, None, *ret_layout);

// Now that the arguments are needed, load them if they are literals.
self.load_literal_symbols(arguments);
Expand Down Expand Up @@ -1893,12 +1851,7 @@ trait Backend<'a> {
}
Symbol::LIST_GET | Symbol::LIST_SET | Symbol::LIST_REPLACE | Symbol::LIST_APPEND => {
// TODO: This is probably simple enough to be worth inlining.
let fn_name = self.lambda_name_to_string(
func_name,
arg_layouts.iter().copied(),
None,
*ret_layout,
);
let fn_name = self.lambda_name_to_string(func_name, None, *ret_layout);
// Now that the arguments are needed, load them if they are literals.
self.load_literal_symbols(args);
self.build_fn_call(sym, fn_name, args, arg_layouts, ret_layout)
Expand All @@ -1925,24 +1878,14 @@ trait Backend<'a> {
}
Symbol::STR_IS_VALID_SCALAR => {
// just call the function
let fn_name = self.lambda_name_to_string(
func_name,
arg_layouts.iter().copied(),
None,
*ret_layout,
);
let fn_name = self.lambda_name_to_string(func_name, None, *ret_layout);
// Now that the arguments are needed, load them if they are literals.
self.load_literal_symbols(args);
self.build_fn_call(sym, fn_name, args, arg_layouts, ret_layout)
}
_other => {
// just call the function
let fn_name = self.lambda_name_to_string(
func_name,
arg_layouts.iter().copied(),
None,
*ret_layout,
);
let fn_name = self.lambda_name_to_string(func_name, None, *ret_layout);
// Now that the arguments are needed, load them if they are literals.
self.load_literal_symbols(args);
self.build_fn_call(sym, fn_name, args, arg_layouts, ret_layout)
Expand Down
14 changes: 2 additions & 12 deletions crates/compiler/gen_dev/src/object_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,7 @@ fn build_object<'a, B: Backend<'a>>(
for ((sym, layout), proc) in helper_symbols_and_layouts.into_iter().zip(helper_procs) {
debug_assert_eq!(sym, proc.name.name());

let fn_name = backend.lambda_name_to_string(
LambdaName::no_niche(sym),
layout.arguments.iter().copied(),
None,
layout.result,
);
let fn_name = backend.lambda_name_to_string(LambdaName::no_niche(sym), None, layout.result);

if let Some(proc_id) = output.symbol_id(fn_name.as_bytes()) {
if let SymbolSection::Section(section_id) = output.symbol(proc_id).section {
Expand Down Expand Up @@ -559,12 +554,7 @@ fn build_proc_symbol<'a, B: Backend<'a>>(
Exposed::Exposed => layout_ids
.get_toplevel(sym, &layout)
.to_exposed_symbol_string(sym, backend.interns()),
Exposed::NotExposed => backend.lambda_name_to_string(
proc.name,
layout.arguments.iter().copied(),
None,
layout.result,
),
Exposed::NotExposed => backend.lambda_name_to_string(proc.name, None, layout.result),
};

let proc_symbol = Symbol {
Expand Down

0 comments on commit 3336201

Please sign in to comment.