| 1 | module main |
| 2 | |
| 3 | import scheme |
| 4 | |
| 5 | @[heap] |
| 6 | struct ByteAllocationReader { |
| 7 | bytes []u8 |
| 8 | mut: |
| 9 | position int |
| 10 | } |
| 11 | |
| 12 | fn (mut reader ByteAllocationReader) read(mut bytes []u8) !int { |
| 13 | length := int_min(bytes.len, reader.bytes.len - reader.position) |
| 14 | if length > 0 { |
| 15 | unsafe { vmemcpy(bytes.data, &u8(reader.bytes.data) + reader.position, length) } |
| 16 | } |
| 17 | reader.position += length |
| 18 | return length |
| 19 | } |
| 20 | |
| 21 | @[heap] |
| 22 | struct ByteAllocationWriter { |
| 23 | mut: |
| 24 | written int |
| 25 | } |
| 26 | |
| 27 | fn (mut writer ByteAllocationWriter) write(bytes []u8) !int { |
| 28 | writer.written += bytes.len |
| 29 | return bytes.len |
| 30 | } |
| 31 | |
| 32 | fn main() { |
| 33 | $if !gcboehm ? { |
| 34 | panic('Build with Boehm GC to measure allocation totals.') |
| 35 | } |
| 36 | println('Source/port construction, compilation, warm-up, and validation excluded.') |
| 37 | println('GC allocator rounding and fixed host-call overhead included; no timings.') |
| 38 | println('case bytes/iterations allocated bytes') |
| 39 | mut vm := scheme.new_with_options(scheme.Options{ max_steps: 0, max_frames: 16 }) or { panic(err) } |
| 40 | for name, program in { |
| 41 | 'bytevector-copy': '(lambda (b) (bytevector-copy b))' |
| 42 | 'bytevector-append': '(lambda (b) (bytevector-append b b))' |
| 43 | 'bytevector-overlap': '(lambda (b) (bytevector-copy! b 1 b 0 (- (bytevector-length b) 1)) b)' |
| 44 | } { |
| 45 | procedure := vm.eval(program) or { panic(err) } |
| 46 | vm.call(procedure, [scheme.bytevector([u8(1), 2, 3])]) or { panic(err) } |
| 47 | for count in [1000, 100000, 1000000] { |
| 48 | data := []u8{len: count, init: u8(index)} |
| 49 | args := [scheme.bytevector(data)] |
| 50 | gc_collect() |
| 51 | before := gc_heap_usage().total_bytes |
| 52 | result := vm.call(procedure, args) or { panic(err) } |
| 53 | allocated := gc_heap_usage().total_bytes - before |
| 54 | actual := result.bytevector_items() or { panic(err) } |
| 55 | if name == 'bytevector-copy' && actual != data { panic('incorrect copy') } |
| 56 | if name == 'bytevector-append' && (actual.len != 2 * count || actual[..count] != data || actual[count..] != data) { |
| 57 | panic('incorrect append') |
| 58 | } |
| 59 | if name == 'bytevector-overlap' && (actual[0] != data[0] || actual[1..] != data[..count - 1]) { |
| 60 | panic('incorrect overlap') |
| 61 | } |
| 62 | println('${name:24} ${count:15} ${allocated:20}') |
| 63 | } |
| 64 | } |
| 65 | read_into := vm.lookup('read-bytevector!') or { panic(err) } |
| 66 | for count in [1000, 100000, 1000000] { |
| 67 | data := []u8{len: count, init: u8(index)} |
| 68 | mut reader := &ByteAllocationReader{ bytes: data } |
| 69 | port := vm.input_port(reader, binary: true, read_size: 65536) or { panic(err) } |
| 70 | target := scheme.bytevector([]u8{len: count}) |
| 71 | args := [target, port] |
| 72 | gc_collect() |
| 73 | before := gc_heap_usage().total_bytes |
| 74 | result := vm.call(read_into, args) or { panic(err) } |
| 75 | allocated := gc_heap_usage().total_bytes - before |
| 76 | if result.as_integer() or { panic(err) } != count || (target.bytevector_items() or { panic(err) }) != data { |
| 77 | panic('incorrect direct read') |
| 78 | } |
| 79 | println('${'direct-read-bytevector!':24} ${count:15} ${allocated:20}') |
| 80 | vm.close_input_port(port) or { panic(err) } |
| 81 | } |
| 82 | procedure := vm.eval('(lambda (n p) (let loop ((i n)) |
| 83 | (if (= i 0) 42 (begin (write-u8 42 p) (loop (- i 1))))))') or { panic(err) } |
| 84 | mut writer := &ByteAllocationWriter{} |
| 85 | port := vm.output_port(writer, binary: true) or { panic(err) } |
| 86 | vm.call(procedure, [scheme.integer(100), port]) or { panic(err) } |
| 87 | for count in [1000, 100000, 1000000] { |
| 88 | writer.written = 0 |
| 89 | args := [scheme.integer(count), port] |
| 90 | gc_collect() |
| 91 | before := gc_heap_usage().total_bytes |
| 92 | result := vm.call(procedure, args) or { panic(err) } |
| 93 | allocated := gc_heap_usage().total_bytes - before |
| 94 | if result.as_integer() or { panic(err) } != 42 || writer.written != count { |
| 95 | panic('incorrect byte writes') |
| 96 | } |
| 97 | println('${'write-u8':24} ${count:15} ${allocated:20}') |
| 98 | } |
| 99 | } |
| 100 | |