diff options
author | polwex <polwex@sortug.com> | 2025-10-06 02:19:52 +0700 |
---|---|---|
committer | polwex <polwex@sortug.com> | 2025-10-06 02:19:52 +0700 |
commit | d7edee0821eeff39d8f28f064d5e7a85fca6ad94 (patch) | |
tree | 52257a59891e80ddc53b6f54895b9baec37b7a1f /vere/pkg | |
parent | c4b71435d9afdb67450f320f54fb7aa99dcae85e (diff) |
yeahyeah
Diffstat (limited to 'vere/pkg')
-rw-r--r-- | vere/pkg/vere/jam_bench_compare.c | 188 | ||||
-rw-r--r-- | vere/pkg/vere/jam_compare.c | 64 |
2 files changed, 252 insertions, 0 deletions
diff --git a/vere/pkg/vere/jam_bench_compare.c b/vere/pkg/vere/jam_bench_compare.c new file mode 100644 index 0000000..baad686 --- /dev/null +++ b/vere/pkg/vere/jam_bench_compare.c @@ -0,0 +1,188 @@ +/// @file +/// Jam/cue benchmarks matching OCaml structure for direct comparison + +#include "noun.h" +#include "vere.h" +#include <stdio.h> +#include <sys/time.h> + +static void _setup(void) { + u3m_boot_lite(1 << 24); +} + +static double get_time(void) { + struct timeval tv; + gettimeofday(&tv, NULL); + return tv.tv_sec + tv.tv_usec / 1000000.0; +} + +static void benchmark(const char* name, int iterations, void (*f)(void)) { + // Warmup + int warmup = iterations / 10; + if (warmup > 100) warmup = 100; + for (int i = 0; i < warmup; i++) { + f(); + } + + // Actual benchmark + double start = get_time(); + for (int i = 0; i < iterations; i++) { + f(); + } + double elapsed = get_time() - start; + + double avg = elapsed / iterations; + double total = elapsed; + fprintf(stdout, "%-40s %d iters: avg=%.6f total=%.6f\n", + name, iterations, avg, total); +} + +// Test data +static u3_noun test_atom_small; +static u3_noun test_atom_large; +static u3_noun test_cell; +static u3_noun test_tree; +static u3_noun test_list; +static u3_noun test_deep; +static c3_y* jam_small_bytes; +static c3_d jam_small_len; +static c3_y* jam_tree_bytes; +static c3_d jam_tree_len; + +// Benchmark functions +static void bench_jam_cue_small(void) { + c3_d len; + c3_y* bytes; + u3s_jam_xeno(test_atom_small, &len, &bytes); + u3_noun result = u3s_cue_xeno(len, bytes); + c3_free(bytes); + u3z(result); +} + +static void bench_jam_cue_large(void) { + c3_d len; + c3_y* bytes; + u3s_jam_xeno(test_atom_large, &len, &bytes); + u3_noun result = u3s_cue_xeno(len, bytes); + c3_free(bytes); + u3z(result); +} + +static void bench_jam_cue_cell(void) { + c3_d len; + c3_y* bytes; + u3s_jam_xeno(test_cell, &len, &bytes); + u3_noun result = u3s_cue_xeno(len, bytes); + c3_free(bytes); + u3z(result); +} + +static void bench_jam_cue_tree(void) { + c3_d len; + c3_y* bytes; + u3s_jam_xeno(test_tree, &len, &bytes); + u3_noun result = u3s_cue_xeno(len, bytes); + c3_free(bytes); + u3z(result); +} + +static void bench_jam_cue_list(void) { + c3_d len; + c3_y* bytes; + u3s_jam_xeno(test_list, &len, &bytes); + u3_noun result = u3s_cue_xeno(len, bytes); + c3_free(bytes); + u3z(result); +} + +static void bench_jam_cue_deep(void) { + c3_d len; + c3_y* bytes; + u3s_jam_xeno(test_deep, &len, &bytes); + u3_noun result = u3s_cue_xeno(len, bytes); + c3_free(bytes); + u3z(result); +} + +static void bench_jam_only_small(void) { + c3_d len; + c3_y* bytes; + u3s_jam_xeno(test_atom_small, &len, &bytes); + c3_free(bytes); +} + +static void bench_jam_only_tree(void) { + c3_d len; + c3_y* bytes; + u3s_jam_xeno(test_tree, &len, &bytes); + c3_free(bytes); +} + +static void bench_cue_only_small(void) { + u3_noun result = u3s_cue_xeno(jam_small_len, jam_small_bytes); + u3z(result); +} + +static void bench_cue_only_tree(void) { + u3_noun result = u3s_cue_xeno(jam_tree_len, jam_tree_bytes); + u3z(result); +} + +int main(int argc, char* argv[]) { + _setup(); + + // Create test data matching OCaml benchmarks + test_atom_small = 42; + test_atom_large = u3i_chubs(1, (c3_d[]){1ULL << 63}); + test_cell = u3nc(1, 2); + + // Balanced tree: [[1 2] [3 4]] [[5 6] [7 8]] + test_tree = u3nc( + u3nc(u3nc(1, 2), u3nc(3, 4)), + u3nc(u3nc(5, 6), u3nc(7, 8)) + ); + + // List structure: [20 [19 [18 ... [1 0]]]] + test_list = 0; + for (int i = 1; i <= 20; i++) { + test_list = u3nc(i, test_list); + } + + // Deep nesting: [100 [99 [98 ... [1 0]]]] + test_deep = 0; + for (int i = 1; i <= 100; i++) { + test_deep = u3nc(i, test_deep); + } + + // Pre-jam for cue-only benchmarks + u3s_jam_xeno(test_atom_small, &jam_small_len, &jam_small_bytes); + u3s_jam_xeno(test_tree, &jam_tree_len, &jam_tree_bytes); + + fprintf(stdout, "========================================\n"); + fprintf(stdout, "Jam/Cue Serialization Benchmarks (C)\n"); + fprintf(stdout, "========================================\n\n"); + + fprintf(stdout, "Round-trip benchmarks:\n"); + benchmark("jam/cue small atom (42)", 100000, bench_jam_cue_small); + benchmark("jam/cue large atom (2^64)", 10000, bench_jam_cue_large); + benchmark("jam/cue simple cell [1 2]", 100000, bench_jam_cue_cell); + benchmark("jam/cue balanced tree (depth 3)", 50000, bench_jam_cue_tree); + benchmark("jam/cue list structure (20 elements)", 10000, bench_jam_cue_list); + benchmark("jam/cue deep nesting (100 levels)", 1000, bench_jam_cue_deep); + + fprintf(stdout, "\nJam-only benchmarks:\n"); + benchmark("jam only (small atom)", 100000, bench_jam_only_small); + benchmark("jam only (balanced tree)", 50000, bench_jam_only_tree); + + fprintf(stdout, "\nCue-only benchmarks:\n"); + benchmark("cue only (small atom)", 100000, bench_cue_only_small); + benchmark("cue only (balanced tree)", 50000, bench_cue_only_tree); + + fprintf(stdout, "\n========================================\n"); + + // Cleanup + c3_free(jam_small_bytes); + c3_free(jam_tree_bytes); + + return 0; +} diff --git a/vere/pkg/vere/jam_compare.c b/vere/pkg/vere/jam_compare.c new file mode 100644 index 0000000..bea8032 --- /dev/null +++ b/vere/pkg/vere/jam_compare.c @@ -0,0 +1,64 @@ +/// @file +/// Outputs jam encodings for comparison with OCaml + +#include "noun.h" +#include "vere.h" +#include <stdio.h> + +static void _setup(void) { + u3m_boot_lite(1 << 24); +} + +static void print_hex(c3_y* bytes, c3_d len) { + for (c3_d i = 0; i < len; i++) { + fprintf(stdout, "%02x", bytes[i]); + } + fprintf(stdout, "\n"); +} + +static void jam_and_print(const char* label, u3_noun noun) { + c3_d len; + c3_y* bytes; + u3s_jam_xeno(noun, &len, &bytes); + + fprintf(stdout, "%s: ", label); + print_hex(bytes, len); + + c3_free(bytes); +} + +int main(int argc, char* argv[]) { + _setup(); + + fprintf(stdout, "# C jam outputs (hex)\n"); + + // Simple atoms + jam_and_print("0", 0); + jam_and_print("1", 1); + jam_and_print("2", 2); + jam_and_print("42", 42); + jam_and_print("255", 255); + jam_and_print("256", 256); + + // Simple cells + jam_and_print("[1 2]", u3nc(1, 2)); + jam_and_print("[0 0]", u3nc(0, 0)); + jam_and_print("[42 43]", u3nc(42, 43)); + + // Nested cells + jam_and_print("[[1 2] 3]", u3nc(u3nc(1, 2), 3)); + jam_and_print("[1 [2 3]]", u3nc(1, u3nc(2, 3))); + + // Balanced tree + jam_and_print("[[1 2] [3 4]]", + u3nc(u3nc(1, 2), u3nc(3, 4))); + + // Larger tree + jam_and_print("[[[1 2] [3 4]] [[5 6] [7 8]]]", + u3nc( + u3nc(u3nc(1, 2), u3nc(3, 4)), + u3nc(u3nc(5, 6), u3nc(7, 8)) + )); + + return 0; +} |