Skip to content

Commit

Permalink
Get rid of stack cache since it seems to actually decrease performance
Browse files Browse the repository at this point in the history
  • Loading branch information
StavromulaBeta committed Aug 30, 2024
1 parent fa9c22a commit 7b40349
Showing 1 changed file with 5 additions and 27 deletions.
32 changes: 5 additions & 27 deletions src/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ typedef struct cognate_stack
{
ANYPTR start; // Pointer to start.
ANYPTR top; // Pointer to top.
ANY cache;
ANYPTR absolute_start; // For the garbage collector
} cognate_stack;

Expand Down Expand Up @@ -233,7 +232,6 @@ static void cleanup(void);
static void push(ANY);
static ANY pop(void);
static ANY peek(void);
static void flush_stack_cache(void);
static int stack_length(void);

// Builtin functions needed by compiled source file defined in functions.c
Expand Down Expand Up @@ -388,8 +386,8 @@ int main(int argc, char** argv)
}
static void cleanup(void)
{
if unlikely(stack.top != stack.start || stack.cache.type)
throw_error_fmt("Exiting with %ti object(s) on the stack", stack.top - stack.start + (stack.cache.type != 0));
if unlikely(stack.top != stack.start)
throw_error_fmt("Exiting with %ti object(s) on the stack", stack.top - stack.start);
}

#ifdef DEBUG
Expand Down Expand Up @@ -455,7 +453,6 @@ static void debugger_step()
exit(EXIT_SUCCESS);
case 's': case 'S':
// Stack
flush_stack_cache();
for (ANY* a = stack.top - 1; a >= stack.start; --a)
{
fputs(show_object(*a, 0, NULL), stderr);
Expand Down Expand Up @@ -748,43 +745,31 @@ static void init_stack(void)
{
stack.absolute_start = stack.top = stack.start
= mmap(0, system_memory/10, MEM_PROT, MEM_FLAGS, -1, 0);
stack.cache.type = 0;
}

__attribute__((hot))
static void push(ANY object)
{
if likely(stack.cache.type == NIL) { stack.cache = object; return; }
*stack.top++ = stack.cache;
stack.cache = object;
*stack.top++ = object;
}

__attribute__((hot))
static ANY pop(void)
{
if likely(stack.cache.type != NIL) { const ANY a = stack.cache; stack.cache.type = NIL; return a; }
if unlikely(stack.top == stack.start) throw_error("Stack underflow");
return *--stack.top;
}

__attribute__((hot))
static ANY peek(void)
{
if likely(stack.cache.type != NIL) return stack.cache;
if unlikely(stack.top == stack.start) throw_error("Stack underflow");
return *(stack.top - 1);
}

static void flush_stack_cache(void)
{
if (stack.cache.type == NIL) return;
push(stack.cache);
pop();
}

static int stack_length(void)
{
return stack.top - stack.start + (stack.cache.type != NIL);
return stack.top - stack.start;
}

static const char* lookup_type(cognate_type type)
Expand Down Expand Up @@ -1207,7 +1192,6 @@ static __attribute__((noinline,hot)) void gc_collect(void)
alloc[z] = 0;
bitmap[z][0] = ALLOC;

flush_stack_cache();
for (uintptr_t* root = (uintptr_t*)stack.absolute_start; root != (uintptr_t*)stack.top; ++root)
gc_collect_root(root);

Expand Down Expand Up @@ -1276,7 +1260,7 @@ static NUMBER ___random(NUMBER low, NUMBER high)
#endif
}

static void ___clear(void) { stack.cache.type = NIL; stack.top=stack.start; }
static void ___clear(void) { stack.top=stack.start; }

static BOOLEAN ___true(void) { return 1; }
static BOOLEAN ___false(void) { return 0; }
Expand Down Expand Up @@ -1355,14 +1339,12 @@ static BOOLEAN ___emptyQ(LIST lst)

static LIST ___list(BLOCK expr)
{
flush_stack_cache();
ANYPTR tmp_stack_start = stack.start;
stack.start = stack.top;
// Eval expr
call_block(expr);
// Move to a list.
cognate_list* lst = NULL;
flush_stack_cache();
size_t len = stack_length();
for (size_t i = 0; i < len; ++i)
{
Expand Down Expand Up @@ -1469,7 +1451,6 @@ static STRING ___path(void)
static LIST ___stack(void)
{
LIST lst = NULL;
flush_stack_cache();
for (size_t i = 0; i + stack.start < stack.top; ++i)
{
cognate_list* tmp = gc_malloc (sizeof *tmp);
Expand Down Expand Up @@ -1619,7 +1600,6 @@ static void ___wait(NUMBER seconds)
/*
static BLOCK ___precompute(BLOCK blk)
{
flush_stack_cache();
ANYPTR tmp_stack_start = stack.start;
stack.start = stack.top;
blk();
Expand Down Expand Up @@ -2001,14 +1981,12 @@ static LIST ___empty (void)

static DICT ___dict (BLOCK expr)
{
flush_stack_cache();
ANYPTR tmp_stack_start = stack.start;
stack.start = stack.top;
// Eval expr
call_block(expr);
// Move to a list.
DICT d = NULL;
flush_stack_cache();
size_t len = stack_length();
if (len % 2 != 0) throw_error("Dict initialiser must be key-value pairs");
for (size_t i = 0; i < len; i += 2)
Expand Down

0 comments on commit 7b40349

Please sign in to comment.