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 | |
| parent | 911f1fe752173fdfee3f135acd2509c908c7fce2 (diff) | |
json: Improved parsing performance
| -rw-r--r-- | README.md | 10 | ||||
| -rw-r--r-- | include/json.h | 2 | ||||
| -rw-r--r-- | include/str.h | 7 | ||||
| -rw-r--r-- | src/json.c | 8 | ||||
| -rw-r--r-- | src/memory.c | 7 | ||||
| -rw-r--r-- | src/str.c | 11 |
6 files changed, 34 insertions, 11 deletions
@@ -12,10 +12,12 @@ ## Benchmarks -| version | result | -|---|---| -|json.loads|652 ms ± 6.65 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)| -|v1|336 ms ± 163 ms per loop (mean ± std. dev. of 10 runs, 1 loop each)| +| version | result | throughput | ram| +|---|---|---|---| +|json.loads|652 ms ± 6.65 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)||| +|v1|336 ms ± 163 ms per loop (mean ± std. dev. of 10 runs, 1 loop each)||| +|no 0 mem alloc|299 ms ± 8 ms per loop (mean ± std. dev. of 10 runs, 1 loop each)||| +|Str_view|272 ms ± 6 ms per loop (mean ± std. dev. of 10 runs, 1 loop each)|330.46 MB/s|1255 MB| ## Resources diff --git a/include/json.h b/include/json.h index 749a3e0..9bd668e 100644 --- a/include/json.h +++ b/include/json.h @@ -24,7 +24,7 @@ typedef struct { i32 integer; f32 floating; Vec array; // Vec<JSON> - Str string; + StrView string; JSONObject object; } as; } JSON; diff --git a/include/str.h b/include/str.h index 300d5ea..3ccd072 100644 --- a/include/str.h +++ b/include/str.h @@ -12,7 +12,14 @@ typedef struct { byte *data; size len; } Str; +typedef struct { + byte *data; + size len; +} StrView; i32 Str_equals(Str a, Str b); u64 Str_hash64(Str s); Str Str_copy(Arena *a, byte *data, size len); i32 Str_fprint(FILE *w, Str s); + +StrView Str_view(byte *data, size len); +i32 StrView_fprint(FILE *w, StrView s); @@ -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); +} |
