module main import scheme @[heap] struct ByteAllocationReader { bytes []u8 mut: position int } fn (mut reader ByteAllocationReader) read(mut bytes []u8) !int { length := int_min(bytes.len, reader.bytes.len - reader.position) if length > 0 { unsafe { vmemcpy(bytes.data, &u8(reader.bytes.data) + reader.position, length) } } reader.position += length return length } @[heap] struct ByteAllocationWriter { mut: written int } fn (mut writer ByteAllocationWriter) write(bytes []u8) !int { writer.written += bytes.len return bytes.len } fn main() { $if !gcboehm ? { panic('Build with Boehm GC to measure allocation totals.') } println('Source/port construction, compilation, warm-up, and validation excluded.') println('GC allocator rounding and fixed host-call overhead included; no timings.') println('case bytes/iterations allocated bytes') mut vm := scheme.new_with_options(scheme.Options{ max_steps: 0, max_frames: 16 }) or { panic(err) } for name, program in { 'bytevector-copy': '(lambda (b) (bytevector-copy b))' 'bytevector-append': '(lambda (b) (bytevector-append b b))' 'bytevector-overlap': '(lambda (b) (bytevector-copy! b 1 b 0 (- (bytevector-length b) 1)) b)' } { procedure := vm.eval(program) or { panic(err) } vm.call(procedure, [scheme.bytevector([u8(1), 2, 3])]) or { panic(err) } for count in [1000, 100000, 1000000] { data := []u8{len: count, init: u8(index)} args := [scheme.bytevector(data)] gc_collect() before := gc_heap_usage().total_bytes result := vm.call(procedure, args) or { panic(err) } allocated := gc_heap_usage().total_bytes - before actual := result.bytevector_items() or { panic(err) } if name == 'bytevector-copy' && actual != data { panic('incorrect copy') } if name == 'bytevector-append' && (actual.len != 2 * count || actual[..count] != data || actual[count..] != data) { panic('incorrect append') } if name == 'bytevector-overlap' && (actual[0] != data[0] || actual[1..] != data[..count - 1]) { panic('incorrect overlap') } println('${name:24} ${count:15} ${allocated:20}') } } read_into := vm.lookup('read-bytevector!') or { panic(err) } for count in [1000, 100000, 1000000] { data := []u8{len: count, init: u8(index)} mut reader := &ByteAllocationReader{ bytes: data } port := vm.input_port(reader, binary: true, read_size: 65536) or { panic(err) } target := scheme.bytevector([]u8{len: count}) args := [target, port] gc_collect() before := gc_heap_usage().total_bytes result := vm.call(read_into, args) or { panic(err) } allocated := gc_heap_usage().total_bytes - before if result.as_integer() or { panic(err) } != count || (target.bytevector_items() or { panic(err) }) != data { panic('incorrect direct read') } println('${'direct-read-bytevector!':24} ${count:15} ${allocated:20}') vm.close_input_port(port) or { panic(err) } } procedure := vm.eval('(lambda (n p) (let loop ((i n)) (if (= i 0) 42 (begin (write-u8 42 p) (loop (- i 1))))))') or { panic(err) } mut writer := &ByteAllocationWriter{} port := vm.output_port(writer, binary: true) or { panic(err) } vm.call(procedure, [scheme.integer(100), port]) or { panic(err) } for count in [1000, 100000, 1000000] { writer.written = 0 args := [scheme.integer(count), port] gc_collect() before := gc_heap_usage().total_bytes result := vm.call(procedure, args) or { panic(err) } allocated := gc_heap_usage().total_bytes - before if result.as_integer() or { panic(err) } != 42 || writer.written != count { panic('incorrect byte writes') } println('${'write-u8':24} ${count:15} ${allocated:20}') } }