Optimize list appends O(n^2)->O(n)

This commit is contained in:
2022-11-15 23:02:01 -05:00
parent 1595a6310d
commit 41bebd1472
3 changed files with 6 additions and 4 deletions

View File

@@ -12,11 +12,12 @@
struct ucl_object *ucl_evaluate_builtin_form(struct ucl_scope *scope, struct ucl_object *list) {
// TODO: Reasonably split builtin and non-builtin evaluation
struct ucl_object *evaluated_list = ucl_nil_create();
struct ucl_object *evaluated_list_tail = evaluated_list;
FOREACH_LIST(list, iter, item) {
struct ucl_object *obj = ucl_evaluate(scope, item);
UCL_RET_IF_ERROR(obj);
ucl_list_append(evaluated_list, obj);
evaluated_list_tail = ucl_list_append(evaluated_list_tail, obj);
};
struct ucl_object *fun = ucl_car(evaluated_list);

View File

@@ -95,11 +95,12 @@ struct ucl_object *ucl_token_next(const char **curr_src) {
struct ucl_object *ucl_tokenize(const char *source) {
struct ucl_object *tokens = ucl_nil_create();
struct ucl_object *tokens_tail = tokens;
struct ucl_object *curr_token = NULL;
const char *curr_src = source;
while ((curr_token = ucl_token_next(&curr_src)) != NULL) {
ucl_list_append(tokens, curr_token);
tokens_tail = ucl_list_append(tokens_tail, curr_token);
}
return tokens;
@@ -209,7 +210,7 @@ struct ucl_object *ucl_parse_tokens(struct ucl_object *tokens) {
return new_sexp;
}
ucl_list_append(result_tail, new_sexp);
result_tail = ucl_list_append(result_tail, new_sexp);
}
return result;

View File

@@ -116,7 +116,7 @@ struct ucl_object *ucl_list_append(struct ucl_object *list, struct ucl_object *o
if (list->cell.car == NULL) {
list->cell.car = obj;
return NULL;
return list;
}
while (iter->cell.cdr != NULL) {