Skip to content

Commit

Permalink
Speed up large primitive arrays by removing GC mark overhead for them
Browse files Browse the repository at this point in the history
  • Loading branch information
goodpaul6 committed Dec 6, 2024
1 parent d55d157 commit 61178f1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion tiny/src/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void DictSet(Dict *dict, Tiny_Value key, Tiny_Value value) {
// adjust growth factor and parameters (like allow it to fail
// if there's no space). Better yet, break these apart into
// DictSet and DictSetGrow so that it's clear.
if (dict->filledCount >= (dict->bucketCount * 2) / 3) Grow(dict);
if (dict->filledCount >= dict->bucketCount / 2) Grow(dict);

unsigned long hash = HashValue(key);

Expand Down
27 changes: 22 additions & 5 deletions tiny/src/std.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,28 @@ const Tiny_NativeProp ArrayProp = {
ArrayFree,
};

static TINY_FOREIGN_FUNCTION(CreateArray) {
// This elides the cost of marking every element of the array (which can be very expensive)
const Tiny_NativeProp PrimitiveArrayProp = {
"array",
NULL,
ArrayFree,
};

static Tiny_Value CreateArrayEx(Tiny_StateThread *thread, int count, const Tiny_Value *values,
bool primitive) {
Array *array = Tiny_AllocUsingContext(thread->ctx, NULL, sizeof(Array));

InitArrayEx(array, thread->ctx, count, args);
InitArrayEx(array, thread->ctx, count, values);

memcpy(array->data, args, sizeof(Tiny_Value) * count);
memcpy(array->data, values, sizeof(Tiny_Value) * count);

return Tiny_NewNative(thread, array, &ArrayProp);
return Tiny_NewNative(thread, array, primitive ? &PrimitiveArrayProp : &ArrayProp);
}

static TINY_FOREIGN_FUNCTION(CreateArray) { return CreateArrayEx(thread, count, args, false); }

static TINY_FOREIGN_FUNCTION(CreatePrimitiveArray) {
return CreateArrayEx(thread, count, args, true);
}

static TINY_FOREIGN_FUNCTION(Lib_ArrayLen) {
Expand Down Expand Up @@ -867,8 +881,11 @@ static TINY_MACRO_FUNCTION(ArrayMacroFunction) {

char sigbuf[512] = {0};

bool primitive = elemType->type == TINY_SYM_TAG_BOOL || elemType->type == TINY_SYM_TAG_INT ||
elemType->type == TINY_SYM_TAG_FLOAT;

snprintf(sigbuf, sizeof(sigbuf), "%s(...): %s", asName, asName);
Tiny_BindFunction(state, sigbuf, CreateArray);
Tiny_BindFunction(state, sigbuf, primitive ? CreatePrimitiveArray : CreateArray);

snprintf(sigbuf, sizeof(sigbuf), "%s_clear(%s): void", asName, asName);
Tiny_BindFunction(state, sigbuf, Lib_ArrayClear);
Expand Down

0 comments on commit 61178f1

Please sign in to comment.