mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-19 01:18:40 +03:00
gc only runs when necessary
This commit is contained in:
parent
975df46e5d
commit
dae8a956ca
@ -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"))
|
||||
|
@ -29,8 +29,3 @@
|
||||
(let [x (fake)]
|
||||
(null? x)))
|
||||
|
||||
(defn fib (n)
|
||||
(if (< n 2)
|
||||
1
|
||||
(+ (fib (- n 2)) (fib (- n 1)))))
|
||||
|
||||
|
25
src/eval.c
25
src/eval.c
@ -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
|
||||
}
|
||||
|
7
src/gc.c
7
src/gc.c
@ -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]);
|
||||
|
2
src/gc.h
2
src/gc.h
@ -3,5 +3,5 @@
|
||||
#include "obj.h"
|
||||
#include "eval.h"
|
||||
|
||||
void gc(Obj *env, Obj *forms);
|
||||
void gc(Obj *env);
|
||||
void gc_all();
|
||||
|
@ -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);
|
||||
|
@ -102,6 +102,7 @@ void obj_print_cout(Obj *o);
|
||||
|
||||
Obj *obj_latest;
|
||||
int obj_total;
|
||||
int obj_total_max;
|
||||
|
||||
Obj *global_env;
|
||||
|
||||
|
10
src/repl.c
10
src/repl.c
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user