aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2025-08-23 21:00:32 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2025-08-27 11:46:11 +0200
commitb22b59adb72371d6ca7072eca2c166f4c2534677 (patch)
treea8b0ec127bfafeb9e6e11fff8842e36617bdc38b
parent413aac1035c855f3e735bf8539c0864182f9c6fb (diff)
json: wider initial array/object
-rw-r--r--README.md33
-rw-r--r--main.c6
-rw-r--r--src/json.c6
-rw-r--r--tests/test_json.c12
4 files changed, 22 insertions, 35 deletions
diff --git a/README.md b/README.md
index 496a416..9fa35fe 100644
--- a/README.md
+++ b/README.md
@@ -7,33 +7,20 @@
- Static functions for private?
- `valgrind --leak-check=full ./your_program`
- web server, Json parsing, struct codegen
-- what is a const char \*
-- whitechar simd
- assert not in release
## Benchmarks
-| 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 |
-| simd | 254 ms ± 6 ms per loop (mean ± std. dev. of 10 runs, 1 loop each) | 353.36 MB/s | 1255 MB |
-| negative | 206 ms ± 6 ms per loop (mean ± std. dev. of 10 runs, 1 loop each) | 436.25 MB/s | 1255MB |
-
-```c
-void print_bytes(byte_32 *data) {
- for (i32 i = 0; i < 32; i++) {
- byte c = ((byte *)data)[i];
- if (c >= 32 && c <= 126) {
- printf("%c", c);
- } else {
- printf(".");
- }
- }
-}
-```
+| version | result | throughput | ram |
+| ---------------- | ------------------------------------------------------------------- | ----------- | ------- |
+| json.loads | 652 ms ± 6.65 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) | 137.73 MB/s | |
+| orjson.loads | 535 ms ± 8.64 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) | 167.85 MB/s | |
+| 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 |
+| simd | 254 ms ± 6 ms per loop (mean ± std. dev. of 10 runs, 1 loop each) | 353.36 MB/s | 1255 MB |
+| negative | 206 ms ± 6 ms per loop (mean ± std. dev. of 10 runs, 1 loop each) | 436.25 MB/s | 1255 MB |
+| init 6 obj array | 186 ms ± 6 ms per loop (mean ± std. dev. of 10 runs, 1 loop each) | 483.96 MB/s | 1026 MB |
## Resources
diff --git a/main.c b/main.c
index 5b18022..516bb12 100644
--- a/main.c
+++ b/main.c
@@ -62,7 +62,7 @@ void run_benchmark(const char *filename, i32 iterations) {
printf("Running %d iterations...\n\n", iterations);
// Statistics tracking
- f32 timings[iterations] = {};
+ f32 timings[iterations];
f32 timing_avg = 0.0;
Arena arena = Arena_new(8e9);
@@ -72,7 +72,7 @@ void run_benchmark(const char *filename, i32 iterations) {
Arena_reset(&arena);
size used = Arena_get_used(arena);
double start_time = get_time_ns();
- JSON result = JSON_parse(&arena, content, file_size);
+ JSON_parse(&arena, content, file_size);
double end_time = get_time_ns();
timings[i] = end_time - start_time;
@@ -80,7 +80,7 @@ void run_benchmark(const char *filename, i32 iterations) {
size new_used = Arena_get_used(arena);
size allocated = new_used - used;
- printf("Iteration %d: %td MB allocated\n", i + 1,
+ printf("Iteration %d: %3.f ms\t%td MB\n", i + 1, timings[i] / 1000000,
allocated / 1024 / 1024);
if ((i + 1) % 100 == 0) {
diff --git a/src/json.c b/src/json.c
index 9c81e7f..c299b63 100644
--- a/src/json.c
+++ b/src/json.c
@@ -161,7 +161,7 @@ parse_result parse_array(Arena *a, byte *src, uptr start_pos, uptr max_pos) {
result.pos++;
return result;
}
- Vec items = Vec_new(a, JSON, 2);
+ Vec items = Vec_new(a, JSON, 6);
for (;;) {
result.pos = absorb_whitespaces(src, result.pos, max_pos);
parse_result item_result = parse_any(a, src, result.pos, max_pos);
@@ -216,8 +216,8 @@ parse_result parse_object(Arena *a, byte *src, uptr start_pos, uptr max_pos) {
parse_result result = {0};
result.pos = absorb_whitespaces(src, start_pos + 1, max_pos);
size len = 0;
- Vec keys = Vec_new(a, Str, 2);
- Vec values = Vec_new(a, JSON, 2);
+ Vec keys = Vec_new(a, Str, 6);
+ Vec values = Vec_new(a, JSON, 6);
if (src[result.pos] == '}') {
result.pos++;
result.value = JSON_object(len, keys, values);
diff --git a/tests/test_json.c b/tests/test_json.c
index b2f8ece..53dc6e3 100644
--- a/tests/test_json.c
+++ b/tests/test_json.c
@@ -57,7 +57,7 @@ i32 test_json_nested_array() {
t_assert(val_1.as.integer == 1);
JSON val_2 = *Vec_get(sub_array.as.array, JSON, 1);
t_assert(Str_equals(StrView_copy(&perm, val_2.as.string), S("abc")));
- t_assert_equal(Arena_get_used(perm), 387);
+ t_assert_equal(Arena_get_used(perm), 1155);
return 0;
}
@@ -79,7 +79,7 @@ i32 test_json_empty_object() {
JSON doc = JSON_parse_raw(&perm, "{}");
t_assert_equal(doc.as.object.len, 0);
- t_assert_equal(Arena_get_used(perm), 224);
+ t_assert_equal(Arena_get_used(perm), 672);
return 0;
}
@@ -93,7 +93,7 @@ i32 test_json_object() {
t_assert(Str_equals(*Vec_get(doc.as.object.keys, Str, 0), S("key")));
JSON value = *JSONObject_get(doc.as.object, S("key"));
t_assert(Str_equals(StrView_copy(&perm, value.as.string), S("value")));
- t_assert_equal(Arena_get_used(perm), 229);
+ t_assert_equal(Arena_get_used(perm), 677);
return 0;
}
@@ -107,7 +107,7 @@ i32 test_json_boolean() {
t_assert(a.as.boolean == true);
JSON b = *JSONObject_get(doc.as.object, S("b"));
t_assert(b.as.boolean == false);
- t_assert_equal(Arena_get_used(perm), 224);
+ t_assert_equal(Arena_get_used(perm), 672);
return 0;
}
@@ -121,7 +121,7 @@ i32 test_json_float() {
t_assert_fequal(first.as.floating, 1.2);
JSON second = *Vec_get(doc.as.array, JSON, 1);
t_assert_fequal(second.as.floating, 2.3);
- t_assert_equal(Arena_get_used(perm), 192);
+ t_assert_equal(Arena_get_used(perm), 576);
return 0;
}
@@ -133,7 +133,7 @@ i32 test_json_null() {
t_assert_equal(doc.as.array.len, 1);
JSON first = *Vec_get(doc.as.array, JSON, 0);
t_assert(first.type == JSON_NULL);
- t_assert_equal(Arena_get_used(perm), 192);
+ t_assert_equal(Arena_get_used(perm), 576);
return 0;
}