diff options
| author | eunhhu <[email protected]> | 2026-10-18 16:44:17 +0100 |
|---|---|---|
| committer | eunhhu <[email protected]> | 2026-10-18 16:44:17 +0100 |
| commit | 241016b0fc3139414089619f0ef8cb7237722c6c (patch) | |
| tree | b782ed50757ee910278c8d1129dcfc7ded48d527 /research/experiments/044-specs/json_rec_time.c | |
| parent | 433f56c29b26b83cd2991b4a834b427fae3e75d4 (diff) | |
| download | pure-241016b0fc3139414089619f0ef8cb7237722c6c.tar.gz pure-241016b0fc3139414089619f0ef8cb7237722c6c.zip | |
Research 140, 027's stage 2c measured: JSON read into a tree by recursive descent (aup's examples/json_tree.aup: value(d, i) -> Json, number giving the value and where it ends, each call's own let's lists, string's escapes joined by char(u), a depth-first walk passing items as values) against Python's reading walked so: JSONTestSuite's 95 y_ taken alike, no n_ taken, 2b's 6 i_ taken (a lone surrogate refused by char as encoding); 100,000 and 50,000 deep refused as memory (the frames fill the event's room, 0.2 s), 30,000 deep taken (past Python's json); 300 random documents alike; in make check (module_specs' digest now depth first, three paths); 142 lines against the same design's 105 in Python (json_rec.py; the bar missed: 24 lines of declarations, 23 of braces), 46 times cJSON's time and 1.6 times plain Python's (600 ns a byte, 248 Pure instructions; 71% in the steps, about 10% in calls, 66,000 calls a document, most of them small block fns like skip); characters against Python's ord in aup's check; probes: p5-chars compiles, p4-rec (a view calling itself) refused; instruction counts: the workloads' modules the same, batches within 0.001% of 2b (BFS -0.21%), the executor alone -0.001% to +0.30%, the bank's queries -0.30% to +0.11% (four runs each; bar 1%); json_rec_time.c (cJSON, a depth-first digest compared first); CHECKPOINT's direction row through 2c
Diffstat (limited to 'research/experiments/044-specs/json_rec_time.c')
| -rw-r--r-- | research/experiments/044-specs/json_rec_time.c | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/research/experiments/044-specs/json_rec_time.c b/research/experiments/044-specs/json_rec_time.c new file mode 100644 index 0000000..03c02c4 --- /dev/null +++ b/research/experiments/044-specs/json_rec_time.c @@ -0,0 +1,119 @@ +/* Research 140, design 027 stage 2c: aup's examples/json_tree.aup (run by + * Pure: a JSON text read into a tree by recursive descent, then a digest of + * the tree, depth first) against C: + * cJSON (the system's libcjson.so.1, its few declarations written here) + * reading the same document into its tree, and the same digest. Both digests + * must be equal; then each is timed (the median of seven runs of rounds). + * + * cc -O2 -std=c17 -I../../../core json_rec_time.c ../../../build/libpure.a -l:libcjson.so.1 -lm -pthread -o json_rec_time + * ./json_rec_time json_tree.pmod ROUNDS + * + * The document is json_time.c's: 2,000 objects of a key and an array of a + * number, true, null and a string with an escape. One JSON line. */ +#define _POSIX_C_SOURCE 200809L +#include "module.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +typedef struct cJSON { + struct cJSON *next, *prev, *child; + int type; + char *valuestring; + int valueint; + double valuedouble; + char *string; +} cJSON; +cJSON *cJSON_Parse(const char *value); +void cJSON_Delete(cJSON *item); +enum { FALSE_ = 1, TRUE_ = 2, NULL_ = 4, NUMBER = 8, STRING = 16, ARRAY = 32, OBJECT = 64 }; + +/* nulls, trues, falses, numbers, strings, arrays, objects, sum, text bytes, key bytes: depth first, as json_tree.aup. */ +typedef struct { double count[7], sum, text_bytes, key_bytes; } Digest; +static void walk(const cJSON *x, Digest *d) { + switch (x->type & 0xFF) { + case NULL_: d->count[0]++; break; + case TRUE_: d->count[1]++; break; + case FALSE_: d->count[2]++; break; + case NUMBER: d->count[3]++, d->sum += x->valuedouble; break; + case STRING: d->count[4]++, d->text_bytes += (double)strlen(x->valuestring); break; + case ARRAY: d->count[5]++; for (const cJSON *y = x->child; y; y = y->next) walk(y, d); break; + default: d->count[6]++; for (const cJSON *y = x->child; y; y = y->next) d->key_bytes += (double)strlen(y->string), walk(y, d); + } +} +static Digest digest(const char *text, cJSON **queue) { + (void)queue; + Digest d = {{0}, 0, 0, 0}; + cJSON *root = cJSON_Parse(text); + walk(root, &d); + cJSON_Delete(root); + return d; +} + +static double now_ns(void) { + struct timespec t; + clock_gettime(CLOCK_MONOTONIC, &t); + return (double)t.tv_sec * 1e9 + (double)t.tv_nsec; +} + +static int by_value(const void *a, const void *b) { + double x = *(const double *)a, y = *(const double *)b; + return (x > y) - (x < y); +} + +int main(int argc, char **argv) { + if (argc != 3) { fprintf(stderr, "usage: %s MODULE ROUNDS\n", argv[0]); return 2; } + int rounds = atoi(argv[2]); + static char doc[1 << 18]; + static cJSON *queue[1 << 16]; + size_t n = 0; + doc[n++] = '['; + for (int k = 0; k < 2000; ++k) + n += (size_t)sprintf(doc + n, "%s{\"key%d\": [1.5e3, true, null, \"text \\u00e9 %d\"]}", k ? "," : "", k, k); + doc[n++] = ']'; + doc[n] = 0; + PureModule module; + char error[160]; + if (pure_module_read(argv[1], &module, error, sizeof(error)) != PURE_OK) { fprintf(stderr, "%s\n", error); return 1; } + PureApp app; + if (pure_app_start(&app, &module, (PureData){0}) != PURE_OK) return 1; + int check = pure_module_input(&module, "check"); + if (pure_app_message(&app, (uint32_t)check, NULL, -1, (PureSpan){(const unsigned char *)doc, n}) != PURE_OK) { + fprintf(stderr, "event failed\n"); + return 1; + } + uint64_t work = app.work; + Digest c = digest(doc, queue); + /* The states after valid: nulls, trues, falses, numbers, strings, arrays, objects, sum, text_bytes, key_bytes. */ + const char *names[] = {"nulls", "trues", "falses", "numbers", "strings", "arrays", "objects", "sum", "text_bytes", "key_bytes"}; + double want[] = {c.count[0], c.count[1], c.count[2], c.count[3], c.count[4], c.count[5], c.count[6], c.sum, c.text_bytes, c.key_bytes}; + for (unsigned k = 0; k < 10; ++k) { + int state = -1; + for (uint32_t s = 0; s < module.states; ++s) if (!strcmp(module.state_names[s], names[k])) state = (int)s; + if (state < 0 || app.values[state].num != want[k]) { + fprintf(stderr, "%s: %g against %g\n", names[k], state < 0 ? -1 : app.values[state].num, want[k]); + return 1; + } + } + double c_runs[7], pure_runs[7]; + volatile double sink = 0; + for (int r = 0; r < 7; ++r) { + double start = now_ns(); + for (int i = 0; i < rounds; ++i) sink += digest(doc, queue).sum; + c_runs[r] = (now_ns() - start) / rounds; + start = now_ns(); + for (int i = 0; i < rounds; ++i) + if (pure_app_message(&app, (uint32_t)check, NULL, -1, (PureSpan){(const unsigned char *)doc, n}) != PURE_OK) return 1; + pure_runs[r] = (now_ns() - start) / rounds; + } + qsort(c_runs, 7, sizeof(double), by_value); + qsort(pure_runs, 7, sizeof(double), by_value); + const char *jit = getenv("PURE_JIT"), *batch = getenv("PURE_BATCH"); + printf("{\"size\":%zu,\"jit\":\"%s\",\"batch\":\"%s\",\"c_ns_per_byte\":%.3f,\"pure_ns_per_byte\":%.3f,\"ratio\":%.1f," + "\"instructions\":%llu,\"instructions_per_byte\":%.1f}\n", + n, jit ? jit : "", batch ? batch : "", c_runs[3] / (double)n, pure_runs[3] / (double)n, pure_runs[3] / c_runs[3], + (unsigned long long)work, (double)work / (double)n); + pure_app_stop(&app); + return 0; +} |
