caching previous generated polymorphic functions
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 28 Aug 2020 22:37:55 +0000 (17:37 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 28 Aug 2020 22:37:55 +0000 (17:37 -0500)
onyx
progs/wasi_test.onyx
src/onyxutils.c

diff --git a/onyx b/onyx
index 266cba25507d8cfc8bfed300bca9711496cc217b..a68aa882e1dbab8a9cb98c3b0f70831dec24157b 100755 (executable)
Binary files a/onyx and b/onyx differ
index 3dc37daa47a75e0fe5b020b58f5da4c4b3f0c171..b225c14d238a1854ac61e7aa6347e18dd3293d56 100644 (file)
@@ -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));
index 7498c0eadc2c2289bc64a81f86b7f826227f3b87..94e5a520cfda93f35280906f11fb0bd4df134de4 100644 (file)
@@ -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;