diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-08-22 21:15:31 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-08-27 11:43:09 +0200 |
| commit | 07dbb6538ca55e69a33ada6761e749f4ee06affe (patch) | |
| tree | be8eaf0a1b3a20aba89a85fe6b614227655e6a39 /src | |
| parent | 911f1fe752173fdfee3f135acd2509c908c7fce2 (diff) | |
json: Improved parsing performance
Diffstat (limited to 'src')
| -rw-r--r-- | src/json.c | 8 | ||||
| -rw-r--r-- | src/memory.c | 7 | ||||
| -rw-r--r-- | src/str.c | 11 |
3 files changed, 20 insertions, 6 deletions
@@ -38,7 +38,7 @@ JSON JSON_array(Vec items) { return json; } -JSON JSON_string(Str value) { +JSON JSON_string(StrView value) { JSON json = {0}; json.type = JSON_STRING; json.as.string = value; @@ -72,7 +72,7 @@ i32 JSON_fprint(FILE *w, JSON json) { switch (json.type) { case JSON_STRING: fprintf(w, "\""); - Str_fprint(w, json.as.string); + StrView_fprint(w, json.as.string); return fprintf(w, "\""); case JSON_INTEGER: return fprintf(w, "%d", json.as.integer); @@ -182,8 +182,8 @@ parse_result parse_string(Arena *a, byte *src, uptr start_pos) { result.pos++; } assert(src[result.pos] == '"'); - result.value = JSON_string( - Str_copy(a, &src[start_pos + 1], result.pos - start_pos - 1)); + result.value = + JSON_string(Str_view(&src[start_pos + 1], result.pos - start_pos - 1)); result.pos++; return result; diff --git a/src/memory.c b/src/memory.c index 63acb39..62f47e7 100644 --- a/src/memory.c +++ b/src/memory.c @@ -22,9 +22,12 @@ void *alloc(Arena *a, size count, size tsize, size align) { size alloc_size = count * tsize; assert(beg + alloc_size < a->end); a->beg = beg + alloc_size; - return memset(beg, 0, count * tsize); + return beg; } -void Arena_reset(Arena *a) { a->beg = a->end - a->cap; } +void Arena_reset(Arena *a) { + a->beg = a->end - a->cap; + memset(a->beg, 0, a->end - a->beg); +} size Arena_get_used(Arena a) { return a.cap - (a.end - a.beg); } @@ -30,3 +30,14 @@ Str Str_copy(Arena *a, byte *data, size len) { i32 Str_fprint(FILE *w, Str s) { return fprintf(w, "%.*s", (i32)s.len, s.data); } + +StrView Str_view(byte *data, size len) { + StrView str = {0}; + str.len = len; + str.data = data; + return str; +} + +i32 StrView_fprint(FILE *w, StrView s) { + return fprintf(w, "%.*s", (i32)s.len, s.data); +} |
