From: Brendan Hansen Date: Fri, 28 Aug 2020 22:37:55 +0000 (-0500) Subject: caching previous generated polymorphic functions X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=6812805fbdcec419a5c76ccf6fb9efddc768aa1c;p=onyx.git caching previous generated polymorphic functions --- diff --git a/onyx b/onyx index 266cba25..a68aa882 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/wasi_test.onyx b/progs/wasi_test.onyx index 3dc37daa..b225c14d 100644 --- a/progs/wasi_test.onyx +++ b/progs/wasi_test.onyx @@ -280,6 +280,9 @@ main :: proc (args: []cstring) { print_u64(cast(u64) fib(20)); print("\n"); + print(add(20l, 5l)); + print(add(20l, 5l)); + print(add(20l, 5l)); print(add(20l, 5l)); print("\n"); print(cast(u64) add(20.0f, 5.0f)); diff --git a/src/onyxutils.c b/src/onyxutils.c index 7498c0ea..94e5a520 100644 --- a/src/onyxutils.c +++ b/src/onyxutils.c @@ -373,8 +373,6 @@ AstFunction* polymorphic_proc_lookup(AstPolyProc* pp, AstCall* call) { scope_clear(pp->poly_scope); - // Currently, not going to do any cacheing - bh_arr_each(AstPolyParam, param, pp->poly_params) { AstArgument* arg = call->arguments; if (param->idx >= call->arg_count) { @@ -402,9 +400,23 @@ AstFunction* polymorphic_proc_lookup(AstPolyProc* pp, AstCall* call) { symbol_introduce(pp->poly_scope, param->poly_sym->token, (AstNode *) raw); } + static char key_buf[1024]; + fori (i, 0, 1024) key_buf[i] = 0; + bh_table_each_start(AstNode *, pp->poly_scope->symbols); + strncat(key_buf, bh_bprintf("%s=", key), 1023); + strncat(key_buf, type_get_name(((AstTypeRawAlias *) value)->to), 1023); + strncat(key_buf, ";", 1023); + bh_table_each_end; + + if (bh_table_has(AstFunction *, pp->concrete_funcs, key_buf)) { + return bh_table_get(AstFunction *, pp->concrete_funcs, key_buf); + } + semstate.curr_scope = pp->poly_scope; AstFunction* func = (AstFunction *) ast_clone(semstate.node_allocator, pp->base_func); + bh_table_put(AstFunction *, pp->concrete_funcs, key_buf, func); + symres_function(func); if (check_function_header(func)) return NULL; if (check_function(func)) return NULL;