Permalink
Browse files

Adds baserefs, for provably reachable objects

It can improve performance for Scopes especially, because scopes with
children that expire quickly can now be cleaned up right away, instead
of lingering about until the next collection cycle.
  • Loading branch information...
1 parent 5dd286c commit 51af5723ef4f8f47e34c56f36b12a09a0479f0fd @gvx committed Mar 1, 2012
Showing with 45 additions and 7 deletions.
  1. +35 −1 vm/gc.c
  2. +3 −0 vm/gc.h
  3. +2 −2 vm/opcodes.c
  4. +5 −4 vm/value.h
View
36 vm/gc.c
@@ -25,6 +25,7 @@ V make_new_value(int type, bool simple, int size)
t->buffered = false;
t->type = type;
t->refs = 1;
+ t->baserefs = 0;
t->color = simple ? Green : Black;
return t;
}
@@ -42,6 +43,26 @@ V add_ref(V t)
return t;
}
+V add_base_ref(V t)
+{
+ if isInt(t)
+ return t;
+
+ t->baserefs++;
+
+ return add_ref(t);
+}
+
+V add_rooted(V t)
+{
+ if isInt(t)
+ return t;
+
+ t->baserefs++;
+
+ return t;
+}
+
void free_value(V t)
{
Stack* s;
@@ -381,9 +402,22 @@ void clear_ref(V t)
{
release_value(t);
}
- else if (t->color != Green)
+ else if (t->color != Green && t->baserefs == 0)
{
possible_root(t);
}
}
}
+
+void clear_base_ref(V t)
+{
+ if (isInt(t))
+ return;
+
+ if (t != NULL)
+ {
+ t->baserefs--;
+
+ clear_ref(t);
+ }
+}
View
@@ -7,7 +7,10 @@
V make_new_value(int, bool, int);
V add_ref(V);
+V add_rooted(V);
+V add_base_ref(V);
void clear_ref(V);
+void clear_base_ref(V);
void collect_cycles(void);
#endif
View
@@ -195,11 +195,11 @@ Error do_instruction(Header* h, Stack* S, Stack* scope_arr)
sc->pc += argument - 1;
break;
case OP_ENTER_SCOPE:
- push(scope_arr, new_scope(scope));
+ push(scope_arr, add_rooted(new_scope(scope)));
break;
case OP_LEAVE_SCOPE:
pc = sc->pc;
- clear_ref(pop(scope_arr));
+ clear_base_ref(pop(scope_arr));
sc = toScope(get_head(scope_arr));
sc->pc = pc;
break;
View
@@ -37,10 +37,11 @@ typedef enum GCColor
typedef struct Value
{
- int type;
- GCColor color;
- bool buffered;
- unsigned int refs;
+ uint16_t type;
+ uint16_t buffered;
+ uint32_t /*GCColor*/ color;
+ uint32_t refs;
+ uint32_t baserefs;
} Value;
typedef Value* V;

0 comments on commit 51af572

Please sign in to comment.