module main import os import reader import strconv struct MemoryCase { name string source string } fn cases(size int) []MemoryCase { mixed := '(define (square x) (+ (* x x) 123))\n' quoted := "'`(alpha ,beta ,@gamma)\n" return [ MemoryCase{'mixed', mixed.repeat(size / mixed.len + 1)}, MemoryCase{'flat-list', '(' + 'a '.repeat(size / 2) + ')'}, MemoryCase{'flat-vector', '#(' + '1 '.repeat(size / 2) + ')'}, MemoryCase{'quoted', quoted.repeat(size / quoted.len + 1)}, MemoryCase{'atoms', 'name 123 #t '.repeat(size / 12 + 1)}, MemoryCase{'string', '"' + 'x'.repeat(size) + '"'}, MemoryCase{'escaped', '"' + 'x'.repeat(size) + r'\"' + '"'}, MemoryCase{'uppercase', 'a'.repeat(size) + 'Z'}, ] } fn measure(c MemoryCase, samples int) ! { warm := reader.read_all(c.source)! gc_collect() mut read_bytes := u64(0) mut write_bytes := u64(0) mut value_bytes := u64(0) mut read_gcs := u64(0) mut write_gcs := u64(0) mut value_gcs := u64(0) mut output := 0 for _ in 0 .. samples { read_before := gc_heap_usage().total_bytes read_gc_before := collections() doc := reader.read_all(c.source)! read_gcs += collections() - read_gc_before read_bytes += u64(gc_heap_usage().total_bytes - read_before) if doc.arena.len() != warm.arena.len() || doc.forms.len != warm.forms.len { return error('${c.name}: parse counts changed') } write_before := gc_heap_usage().total_bytes write_gc_before := collections() mut written := 0 for id in doc.forms { written += doc.arena.write(id)!.len } write_gcs += collections() - write_gc_before write_bytes += u64(gc_heap_usage().total_bytes - write_before) if output != 0 && written != output { return error('${c.name}: printer length changed') } output = written if c.name in ['string', 'escaped', 'uppercase'] { value_before := gc_heap_usage().total_bytes value_gc_before := collections() value := if c.name == 'uppercase' { doc.arena.symbol_name(doc.forms[0])! } else { doc.arena.string_value(doc.forms[0])! } value_gcs += collections() - value_gc_before value_bytes += u64(gc_heap_usage().total_bytes - value_before) if value.len == 0 { return error('unexpected empty decoded value') } } } println('${c.name:12} ${c.source.len:9} ${warm.arena.len():8} ${read_bytes / u64(samples):12} ${write_bytes / u64(samples):12} ${value_bytes / u64(samples):12} ${read_gcs:8} ${write_gcs:9} ${value_gcs:9} ${warm.arena.storage_bytes():12}') } fn main() { $if !gcboehm ? { eprintln('This allocation benchmark requires -gc boehm (the default).') exit(1) } size := strconv.parse_int(if os.args.len > 1 { os.args[1] } else { '65536' }, 10, 32) or { panic(err) } samples := strconv.parse_int(if os.args.len > 2 { os.args[2] } else { '9' }, 10, 32) or { panic(err) } selected := if os.args.len > 3 { os.args[3] } else { '' } if size < 128 || samples < 1 || samples > 101 { eprintln('Usage: memory benchmark [bytes >= 128] [samples: 1..101] [case]') exit(1) } mut found := false println('GC enabled: ${gc_is_enabled()}; fresh documents; allocation totals include allocator rounding.') println('Bytes are mean allocation per operation; GC counts are totals across ${samples} samples.') println('Corpus construction, warm-up and explicit pre-case collection are excluded. Arenas exclude source, roots and scratch.') println('Value decoding is measured for strings and uppercase symbols only; other rows report zero.') println('case source nodes read alloc write alloc value alloc read GC write GC value GC arena bytes') for c in cases(int(size)) { if selected != '' && c.name != selected { continue } found = true measure(c, int(samples)) or { panic(err) } } if !found { eprintln('unknown case: ${selected}') exit(1) } }