gc only runs when necessary

This commit is contained in:
Erik Svedäng 2016-01-14 20:52:14 +01:00
parent 975df46e5d
commit dae8a956ca
8 changed files with 31 additions and 22 deletions

View File

@ -6,4 +6,4 @@
(load-lisp (str carp-dir "lisp/compiler_tests.lisp"))
(load-lisp (str carp-dir "lisp/examples.lisp"))
;; (load-lisp (str carp-dir "lisp/glfw_test.lisp"))
(load-lisp (str carp-dir "lisp/glfw_test.lisp"))

View File

@ -29,8 +29,3 @@
(let [x (fake)]
(null? x)))
(defn fib (n)
(if (< n 2)
1
(+ (fib (- n 2)) (fib (- n 1)))))

View File

@ -9,6 +9,7 @@
#define LOG_SHADOW_STACK 0
#define SHOW_MACRO_EXPANSION 0
#define LOG_FUNC_APPLICATION 0
#define GC_COLLECT_AFTER_EACH_FORM 0
#define STACK_TRACE_LEN 256
char function_trace[STACK_SIZE][STACK_TRACE_LEN];
@ -582,10 +583,18 @@ void eval_internal(Obj *env, Obj *o) {
if(LOG_EVAL) {
printf("> "); obj_print_cout(o); printf("\n");
}
if(LOG_GC_POINTS) {
printf("Running GC in eval:\n");
if(obj_total > obj_total_max) {
//printf("obj_total = %d\n", obj_total);
if(LOG_GC_POINTS) {
printf("Running GC in eval:\n");
}
gc(global_env);
obj_total_max += 1000;
//printf("new obj_total_max = %d\n", obj_total_max);
}
else {
//printf("%d/%d\n", obj_total, obj_total_max);
}
gc(global_env, o);
if(!o) {
stack_push(nil);
@ -645,7 +654,7 @@ void eval_text(Obj *env, char *text, bool print) {
if(LOG_GC_POINTS) {
printf("Running GC after error occured:\n");
}
gc(env, NULL);
gc(env);
return;
}
if(print) {
@ -658,10 +667,12 @@ void eval_text(Obj *env, char *text, bool print) {
printf("\n");
}
form = form->cdr;
if(LOG_GC_POINTS) {
printf("Running GC after evaluation of single form in eval_text:\n");
if(GC_COLLECT_AFTER_EACH_FORM) {
if(LOG_GC_POINTS) {
printf("Running GC after evaluation of single form in eval_text:\n");
}
gc(env);
}
gc(env, NULL);
}
stack_pop(); // pop the 'forms' that was pushed above
}

View File

@ -68,14 +68,11 @@ void gc_sweep() {
}
}
if(LOG_GC_KILL_COUNT) {
printf("\e[33mDeleted %d Obj:s, %d left.\e[0m\n", kill_count, obj_total);
printf("\e[33mGC:d %d Obj:s, %d left.\e[0m\n", kill_count, obj_total);
}
}
void gc(Obj *env, Obj *forms) {
if(forms) {
obj_mark_alive(forms);
}
void gc(Obj *env) {
obj_mark_alive(env);
for(int i = 0; i < stack_pos; i++) {
obj_mark_alive(stack[i]);

View File

@ -3,5 +3,5 @@
#include "obj.h"
#include "eval.h"
void gc(Obj *env, Obj *forms);
void gc(Obj *env);
void gc_all();

View File

@ -3,6 +3,7 @@
#include "../out/shared.h"
int main() {
obj_total_max = 100000;
shadow_stack_pos = 0;
env_new_global();
eval_text(global_env, "(load-lisp (str (getenv \"CARP_DIR\") \"lisp/boot.carp\"))", false);

View File

@ -102,6 +102,7 @@ void obj_print_cout(Obj *o);
Obj *obj_latest;
int obj_total;
int obj_total_max;
Obj *global_env;

View File

@ -10,12 +10,16 @@
#define MAX_INPUT_BUFFER_SIZE 2048
char input[MAX_INPUT_BUFFER_SIZE];
#define GC_COLLECT_BEFORE_REPL_INPUT 0
void repl(Obj *env) {
while(1) {
if(LOG_GC_POINTS) {
printf("Running GC before taking REPL input:\n");
if(GC_COLLECT_BEFORE_REPL_INPUT) {
if(LOG_GC_POINTS) {
printf("Running GC before taking REPL input:\n");
}
gc(env);
}
gc(env, NULL);
printf("\e[36mλ>\e[0m ");
fgets(input, MAX_INPUT_BUFFER_SIZE, stdin);
if(strcmp(input, "q\n") == 0) {