Skip to content

Commit

Permalink
Merge pull request #10 from cometlang/chore/speedup-math
Browse files Browse the repository at this point in the history
Chore/speedup math
  • Loading branch information
michaelmalonenz authored Apr 19, 2022
2 parents 08717ca + 226045e commit 544f3cc
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 223 deletions.
1 change: 1 addition & 0 deletions comet/comet.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ extern "C" {

VALUE create_number(VM* vm, double number);
double number_get_value(VALUE self);
VALUE number_operator(VM* vm, VALUE self, VALUE* arguments, OPERATOR op);

VALUE list_create(VM* vm);
VALUE list_add(VM* vm, VALUE self, int arg_count, VALUE* arguments);
Expand Down
23 changes: 12 additions & 11 deletions stdlib/enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,32 @@ typedef struct {
} EnumIterator;

typedef struct {
NumberData num;
ObjNativeInstance obj;
double num;
VALUE name;
} EnumValueData;

static void enumvalue_constructor(void *instanceData)
{
EnumValueData *data = (EnumValueData *)instanceData;
data->name = NIL_VAL;
data->num.num = 0;
data->num = 0;
}

static VALUE enumvalue_init(VM *vm, VALUE self, int UNUSED(arg_count), VALUE *arguments)
{
EnumValueData *data = GET_NATIVE_INSTANCE_DATA(EnumValueData, self);
data->name = arguments[0];
data->num.num = number_get_value(arguments[1]);
data->num = number_get_value(arguments[1]);
setNativeProperty(vm, self, "name", data->name);
setNativeProperty(vm, self, "value", data->num.num);
setNativeProperty(vm, self, "value", data->num);
return NIL_VAL;
}

uint64_t enumvalue_get_value(VALUE instance)
{
EnumValueData *data = GET_NATIVE_INSTANCE_DATA(EnumValueData, instance);
return (uint64_t) data->num.num;
return (uint64_t) data->num;
}

static VALUE enumvalue_to_string(VM UNUSED(*vm), VALUE UNUSED(self), int UNUSED(arg_count), VALUE UNUSED(*arguments))
Expand All @@ -57,7 +58,7 @@ static VALUE enumvalue_to_string(VM UNUSED(*vm), VALUE UNUSED(self), int UNUSED(
int max_len = 64 + strlen(name);
#endif
char temp_string[max_len];
int length = snprintf(temp_string, max_len, "%s:%.17g", name, data->num.num);
int length = snprintf(temp_string, max_len, "%s:%.17g", name, data->num);
return copyString(vm, temp_string, length);
}

Expand Down Expand Up @@ -107,30 +108,30 @@ static VALUE enum_parse(VM UNUSED(*vm), VALUE self, int UNUSED(arg_count), VALUE
{
EnumData *data = GET_NATIVE_INSTANCE_DATA(EnumData, self);
VALUE candidate = arguments[0];
if (AS_INSTANCE(candidate)->klass->classType == CLS_STRING)
if (IS_NUMBER(candidate))
{
for (int i = 0; i < data->array.count; i++)
{
VALUE current = data->array.values[i];
if (current != NIL_VAL)
{
EnumValueData *val = GET_NATIVE_INSTANCE_DATA(EnumValueData, current);
if (strcmp(string_get_cstr(val->name), string_get_cstr(candidate)) == 0)
if (val->num == number_get_value(candidate))
{
return current;
}
}
}
}
else if (is_a_number(candidate))
else if (IS_INSTANCE_OF_STDLIB_TYPE(candidate, CLS_STRING))
{
for (int i = 0; i < data->array.count; i++)
{
VALUE current = data->array.values[i];
if (current != NIL_VAL)
{
EnumValueData *val = GET_NATIVE_INSTANCE_DATA(EnumValueData, current);
if (val->num.num == number_get_value(candidate))
if (strcmp(string_get_cstr(val->name), string_get_cstr(candidate)) == 0)
{
return current;
}
Expand Down Expand Up @@ -220,7 +221,7 @@ void init_enum(VM *vm)
defineNativeMethod(vm, enum_class, &enum_length, "length", 0, false);
defineNativeMethod(vm, enum_class, &enum_length, "count", 0, false);

enum_value_class = defineNativeClass(vm, "EnumValue", &enumvalue_constructor, NULL, "Number", CLS_ENUM_VALUE, sizeof(EnumValueData), false);
enum_value_class = defineNativeClass(vm, "EnumValue", &enumvalue_constructor, NULL, NULL, CLS_ENUM_VALUE, sizeof(EnumValueData), false);
defineNativeMethod(vm, enum_value_class, &enumvalue_init, "init", 2, false);
defineNativeMethod(vm, enum_value_class, &enumvalue_to_string, "to_string", 0, false);

Expand Down
64 changes: 46 additions & 18 deletions stdlib/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,21 @@ VALUE list_peek(VM UNUSED(*vm), VALUE self, int UNUSED(arg_count), VALUE UNUSED(
return data->entries[data->count - 1].item;
}

VALUE list_get_at(VM UNUSED(*vm), VALUE self, int UNUSED(arg_count), VALUE *arguments)
VALUE list_get_at(VM UNUSED(*vm), VALUE self, int arg_count, VALUE *arguments)
{
if (arg_count == 1)
{
ListData *data = GET_NATIVE_INSTANCE_DATA(ListData, self);
int index = (int)number_get_value(arguments[0]);
VALUE arg = arguments[0];
int index;
if (IS_NUMBER(arg))
{
index = (int)number_get_value(arg);
}
else if (IS_INSTANCE_OF_STDLIB_TYPE(arg, CLS_ENUM_VALUE))
{
index = (int)enumvalue_get_value(arg);
}
if (index >= data->count)
{
throw_exception_native(
Expand Down Expand Up @@ -185,30 +194,49 @@ VALUE list_iterable_contains_q(VM UNUSED(*vm), VALUE self, int UNUSED(arg_count)
return FALSE_VAL;
}

static void insertion_sort(VM *vm, const ListData *data, int left, int right)
static VALUE is_lhs_less_than_or_equal_to_rhs(VALUE lhs, VALUE rhs, VALUE compare_func)
{
for (int i = left + 1; i <= right; i++)
if (IS_NUMBER(lhs))
{
VALUE temp = data->entries[i].item;
if (!IS_INSTANCE(temp) && !IS_NATIVE_INSTANCE(temp))
{
throw_exception_native(vm, "ArgumentException", "Can't sort a list containing a '%s'", objTypeName(AS_OBJ(temp)->type));
return;
}
VALUE compare_func = AS_INSTANCE(temp)->klass->operators[OPERATOR_LESS_EQUAL];
if (AS_NUMBER(lhs) <= AS_NUMBER(rhs))
return TRUE_VAL;
return FALSE_VAL;
}
return call_function(lhs, compare_func, 1, &rhs);
}

static VALUE get_compare_func(VM *vm, VALUE self)
{
if (IS_INSTANCE(self) || IS_NATIVE_INSTANCE(self))
{
VALUE compare_func = AS_INSTANCE(self)->klass->operators[OPERATOR_LESS_EQUAL];
if (compare_func == NIL_VAL)
{
throw_exception_native(
runtimeError(
vm,
"ArgumentException",
"%s doesn't implement <= as required for sorting",
getClassNameFromInstance(temp));
getClassNameFromInstance(self));
}
}
return NIL_VAL;
}

static void insertion_sort(VM *vm, const ListData *data, int left, int right)
{
for (int i = left + 1; i <= right; i++)
{
VALUE temp = data->entries[i].item;
VALUE compare_func = get_compare_func(vm, temp);
if (!IS_INSTANCE(temp) && !IS_NATIVE_INSTANCE(temp) && !IS_NUMBER(temp))
{
throw_exception_native(vm, "ArgumentException", "Can't sort a list containing a '%s'", objTypeName(AS_OBJ(temp)->type));
return;
}
int j = i - 1;
while (j >= left)
{
VALUE result = call_function(temp, compare_func, 1, &data->entries[j].item);
VALUE result = is_lhs_less_than_or_equal_to_rhs(temp, data->entries[j].item, compare_func);
if (result == TRUE_VAL)
{
data->entries[j + 1].item = data->entries[j].item;
Expand All @@ -223,7 +251,7 @@ static void insertion_sort(VM *vm, const ListData *data, int left, int right)
}
}

static void merge_sorted_runs(ListData* data, int l, int m, int r)
static void merge_sorted_runs(VM *vm, ListData* data, int l, int m, int r)
{
// Original array is broken into two parts - left and right array
int len1 = m - l + 1, len2 = r - m;
Expand All @@ -241,8 +269,8 @@ static void merge_sorted_runs(ListData* data, int l, int m, int r)
// After comparing, we merge those two arrays into a larger sub array
while (i < len1 && j < len2)
{
VALUE compare_func = AS_INSTANCE(left[i])->klass->operators[OPERATOR_LESS_EQUAL];
VALUE result = call_function(left[i], compare_func, 1, &right[j]);
VALUE compare_func = get_compare_func(vm, left[i]);
VALUE result = is_lhs_less_than_or_equal_to_rhs(left[i], right[j], compare_func);
if (result == TRUE_VAL)
{
data->entries[k].item = left[i];
Expand Down Expand Up @@ -300,7 +328,7 @@ VALUE list_sort(VM *vm, VALUE self, int UNUSED(arg_count), VALUE UNUSED(*argumen

// merge sub array arr[left.....mid] and arr[mid+1....right]
if (mid < right)
merge_sorted_runs(data, left, mid, right);
merge_sorted_runs(vm, data, left, mid, right);
}
}

Expand Down
Loading

0 comments on commit 544f3cc

Please sign in to comment.