aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2025-08-22 21:15:31 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2025-08-27 11:43:09 +0200
commit07dbb6538ca55e69a33ada6761e749f4ee06affe (patch)
treebe8eaf0a1b3a20aba89a85fe6b614227655e6a39
parent911f1fe752173fdfee3f135acd2509c908c7fce2 (diff)
json: Improved parsing performance
-rw-r--r--README.md10
-rw-r--r--include/json.h2
-rw-r--r--include/str.h7
-rw-r--r--src/json.c8
-rw-r--r--src/memory.c7
-rw-r--r--src/str.c11
6 files changed, 34 insertions, 11 deletions
diff --git a/README.md b/README.md
index 6c8eda3..5c28f84 100644
--- a/README.md
+++ b/README.md
@@ -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);
diff --git a/src/json.c b/src/json.c
index 6f00da8..af93d06 100644
--- a/src/json.c
+++ b/src/json.c
@@ -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); }
diff --git a/src/str.c b/src/str.c
index eccacd8..2899ca9 100644
--- a/src/str.c
+++ b/src/str.c
@@ -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);
+}