Improve memory efficiency via plain ints for length

This commit is contained in:
2022-11-29 20:57:50 -05:00
parent d965ca142d
commit 13704fae2c
16 changed files with 183 additions and 84 deletions

View File

@@ -225,6 +225,36 @@ void test_complex(void) {
TEST_ASSERT_OBJ_INT_V(response, 9);
}
void test_memory_perf_low(void) {
response = eval("(defun fib (n) (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2)))))");
TEST_ASSERT_OBJ_SYMBOL_V(response, "fib");
response = eval("(fib 4)");
TEST_ASSERT_OBJ_INT_V(response, 3);
}
void test_memory_perf_medium(void) {
response = eval("(defun fib (n) (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2)))))");
TEST_ASSERT_OBJ_SYMBOL_V(response, "fib");
response = eval("(fib 10)");
TEST_ASSERT_OBJ_INT_V(response, 55);
}
void test_memory_perf_high(void) {
response = eval("(defun fib (n) (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2)))))");
TEST_ASSERT_OBJ_SYMBOL_V(response, "fib");
response = eval("(fib 8)");
TEST_ASSERT_OBJ_INT_V(response, 21);
}
int main(void) {
UNITY_BEGIN();
@@ -254,6 +284,9 @@ int main(void) {
RUN_TEST(test_nth_oob);
RUN_TEST(test_eval_defun_gc);
RUN_TEST(test_complex);
RUN_TEST(test_memory_perf_low);
RUN_TEST(test_memory_perf_medium);
RUN_TEST(test_memory_perf_high);
return UNITY_END();
}

View File

@@ -50,11 +50,11 @@ static void test_put_modify_get(void) {
ucl_list_append(state, obj, ucl_string_create(state, "quux"));
response = ucl_scope_get(state, scope, "foo");
TEST_ASSERT_OBJ_STRING(ucl_list_nth(state, response, 2));
TEST_ASSERT_EQUAL_STRING(ucl_list_nth(state, response, 2)->string, "quux");
TEST_ASSERT_OBJ_STRING(ucl_list_nth(response, 2));
TEST_ASSERT_EQUAL_STRING(ucl_list_nth(response, 2)->string, "quux");
TEST_ASSERT_OBJ_STRING(ucl_list_nth(state, obj, 2));
TEST_ASSERT_EQUAL_STRING(ucl_list_nth(state, obj, 2)->string, "quux");
TEST_ASSERT_OBJ_STRING(ucl_list_nth(obj, 2));
TEST_ASSERT_EQUAL_STRING(ucl_list_nth(obj, 2)->string, "quux");
}

View File

@@ -134,20 +134,19 @@ static void test_list_append_list() {
}
static void test_list_nth_nil_0() {
response = ucl_list_nth(state, ucl_nil_create(state), 0);
response = ucl_list_nth(ucl_nil_create(state), 0);
TEST_ASSERT_OBJ_ERROR(response);
}
static void test_list_nth_nil_1() {
response = ucl_list_nth(state, ucl_nil_create(state), 1);
response = ucl_list_nth(ucl_nil_create(state), 1);
TEST_ASSERT_OBJ_ERROR(response);
}
static void test_list_nth_list_0() {
response = ucl_list_nth(
state,
ucl_tuple_create(state,
ucl_t_create(state),
ucl_string_create(state, "foo")),
@@ -158,7 +157,6 @@ static void test_list_nth_list_0() {
static void test_list_nth_list_1() {
response = ucl_list_nth(
state,
ucl_tuple_create(state,
ucl_t_create(state),
ucl_string_create(state, "foo")),
@@ -168,20 +166,19 @@ static void test_list_nth_list_1() {
}
static void test_list_nth_t_0() {
response = ucl_list_nth(state, ucl_t_create(state), 1);
response = ucl_list_nth(ucl_t_create(state), 1);
TEST_ASSERT_OBJ_ERROR(response);
}
static void test_list_nth_bounds_0() {
response = ucl_list_nth(state, ucl_nil_create(state), 0);
response = ucl_list_nth(ucl_nil_create(state), 0);
TEST_ASSERT_OBJ_ERROR(response);
}
static void test_list_nth_bounds_1() {
response = ucl_list_nth(
state,
ucl_cell_create(state,
ucl_nil_create(state),
NULL),
@@ -192,7 +189,6 @@ static void test_list_nth_bounds_1() {
static void test_list_nth_bounds_2() {
response = ucl_list_nth(
state,
ucl_tuple_create(state,
ucl_nil_create(state),
ucl_nil_create(state)),

View File

@@ -40,21 +40,24 @@
} while (0)
#define TEST_ASSERT_OBJ_LIST(obj) \
TEST_ASSERT_EQUAL_MESSAGE(UCL_TYPE_CELL, obj->type, "Expected cell type")
TEST_ASSERT_EQUAL_MESSAGE(UCL_TYPE_CELL, (obj)->type, "Expected cell type")
#define TEST_ASSERT_LIST_LEN(list, len) \
do { \
TEST_ASSERT_OBJ_LIST(list); \
TEST_ASSERT_EQUAL(len, ucl_list_length(state, list)->integer); \
#define TEST_ASSERT_LIST_LEN(list, len) \
do { \
TEST_ASSERT_OBJ_LIST(list); \
TEST_ASSERT_EQUAL_MESSAGE(len, ucl_list_length(state, list)->integer, "Unexpected list length"); \
} while(0)
#define TEST_ASSERT_NIL(obj) \
TEST_ASSERT_LIST_LEN(obj, 0)
do { \
TEST_ASSERT_OBJ_LIST(obj); \
TEST_ASSERT_LIST_LEN(obj, 0); \
} while(0)
#define TEST_ASSERT_T(obj) \
do { \
TEST_ASSERT_OBJ_SYMBOL(obj); \
TEST_ASSERT_EQUAL_STRING(obj->symbol, "t"); \
#define TEST_ASSERT_T(obj) \
do { \
TEST_ASSERT_OBJ_SYMBOL(obj); \
TEST_ASSERT_EQUAL_STRING((obj)->symbol, "t"); \
} while(0)
#endif