From 14b79425567e37a1d18e334d05c191e19e463ba9 Mon Sep 17 00:00:00 2001 From: dzaima Date: Sun, 5 Jun 2022 17:17:20 +0300 Subject: [PATCH] )clearImportCache --- docs/commands.md | 30 +++++++++++++++++------------- src/builtins/fns.c | 4 ++++ src/builtins/sysfn.c | 8 ++++++++ src/main.c | 4 ++++ 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/docs/commands.md b/docs/commands.md index 72f87dda..2c7af964 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -10,14 +10,28 @@ Execute the contents of the file as if it were REPL input (but allowing multilin Execute the expression, but don't print its result. -## `)profile expr` / `)profile@frequency expr` +## `)clearImportCache` -Profile the expression at the given sampling frequency, or 5000 samples/second by default +Clear the cache of monadic `•Import` calls, resulting in re-evaluating the source the next time one is executed ## `)t expr` / `)time expr` / `)t:n expr` / `)time:n expr` Time the argument expression. `n` specifies the number of times to repeat. Exists to allow not escaping quotes and less overhead for timing very fast & small expressions. +## `)profile expr` / `)profile@frequency expr` + +Profile the expression at the given sampling frequency, or 5000 samples/second by default + +## `)vars` + +List the globally defined variables. + +## `)erase name` + +Erase the specified variable name. + +Not a system function because it only clears the variables value (previous code `↩`ing it will still be able to), and to allow it to be executed even when the VM is completely out of memory such that it can't even parse REPL input BQN. + ## `)mem` Get statistics on memory usage. @@ -26,16 +40,6 @@ Get statistics on memory usage. `)mem s` to get a breakdown of the number of objects with a specific size. `)mem f` to get breakdown of free bucket counts per size. -## `)erase name` - -Erase the specified variable name. - -Not a system function because it only clears the variables value (previous code `↩`ing it will still be able to), and to allow it to be executed even when the VM is completely out of memory such that it can't even parse an expression. - -## `)vars` - -List the globally defined variables. - ## `)gc` Force garbage collection. @@ -46,4 +50,4 @@ Not a system function because currently CBQN doesn't support garbage collection ## `)internalPrint expr` -Use the internal object printing system to show the expression result. \ No newline at end of file +Use the internal object printing system to show the expression result. Mainly for debugging in situations where the BQN self-hosted formatter can't be used. \ No newline at end of file diff --git a/src/builtins/fns.c b/src/builtins/fns.c index 2226c66a..25fd91d4 100644 --- a/src/builtins/fns.c +++ b/src/builtins/fns.c @@ -314,6 +314,10 @@ void setPrevImport(B path, i32 pos) { bool had; i32 prev = mk_b2i(&prevImports, path, &had); prevImports->a[prev].val = pos; } +void clearImportCacheMap() { + if (prevImports!=NULL) free_b2i(prevImports); + prevImports = NULL; +} static H_b2i* globalNames; static B globalNameList; diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index 3d8e2bea..24a32348 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -635,6 +635,7 @@ B import_c2(B d, B w, B x) { // defined in fns.c i32 getPrevImport(B path); void setPrevImport(B path, i32 pos); +void clearImportCacheMap(void); static B importKeyList; // exists for GC roots as the hashmap doesn't static B importValList; @@ -677,6 +678,13 @@ static void sys_gcFn() { mm_visit(lastErrMsg); #endif } +void clearImportCache() { + if (importKeyList.u!=0) { + dec(importKeyList); importKeyList = m_f64(0); + dec(importValList); importValList = m_f64(0); + } + clearImportCacheMap(); +} static NFnDesc* fTypeDesc; diff --git a/src/main.c b/src/main.c index 7d9c6d68..04d836d3 100644 --- a/src/main.c +++ b/src/main.c @@ -44,6 +44,7 @@ bool profiler_start(i64 hz); bool profiler_stop(void); void profiler_free(void); void profiler_displayResults(void); +void clearImportCache(void); i64 readInt(char** p) { char* c = *p; @@ -129,6 +130,9 @@ void cbqn_runLine0(char* ln, i64 read) { } printf("No such variable found\n"); return; + } else if (isCmd(cmdS, &cmdE, "clearImportCache ")) { + clearImportCache(); + return; } else if (isCmd(cmdS, &cmdE, "vars")) { B r = listVars(gsc); if (q_N(r)) {