vile / bench / port_allocation.v
69 lines · 64 sloc · 2.34 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module main
2
3import arena
4import io
5import os
6import scheme
7import time
8
9@[heap]
10struct PreparedReader {
11 text string
12 chunk int
13mut:
14 position int
15}
16
17fn (mut source PreparedReader) read(mut destination []u8) !int {
18 if source.position == source.text.len { return io.Eof{} }
19 mut count := source.text.len - source.position
20 if count > source.chunk { count = source.chunk }
21 if count > destination.len { count = destination.len }
22 unsafe { vmemcpy(destination.data, source.text.str + source.position, count) }
23 source.position += count
24 return count
25}
26
27fn measure(name string, text string, count int, streaming bool) {
28 mut region := arena.new()
29 region.push()
30 mut vm := scheme.new()
31 mut source := &PreparedReader{ text: text, chunk: 17 }
32 input := if streaming {
33 vm.input_port(source, read_size: 31) or { panic(err) }
34 } else {
35 vm.open_input_string(text) or { panic(err) }
36 }
37 // Streaming storage is prepared inside the region before measuring reads.
38 vm.peek_char(input) or { panic(err) }
39 before := region.used()
40 start := time.sys_mono_now()
41 mut checksum := u64(0)
42 for _ in 0 .. count {
43 value := vm.read(input) or { panic(err) }
44 checksum += u64(value.kind())
45 }
46 elapsed := time.sys_mono_now() - start
47 bytes := region.used() - before
48 region.pop()
49 region.free()
50 println('${name}\t${count}\t${bytes}\t${checksum}')
51 if os.args.contains('--time') { eprintln('${name}\t${elapsed} ns') }
52}
53
54fn main() {
55 measure('integers-memory', '123 '.repeat(1000), 1000, false)
56 measure('integers-stream', '123 '.repeat(1000), 1000, true)
57 measure('identifiers-memory', 'Hello '.repeat(1000), 1000, false)
58 measure('identifiers-stream', 'Hello '.repeat(1000), 1000, true)
59 measure('strings-memory', ('"' + 'a'.repeat(128) + '"').repeat(1000), 1000, false)
60 measure('strings-stream', ('"' + 'a'.repeat(128) + '"').repeat(1000), 1000, true)
61 measure('lists-memory', '(1 2 3)'.repeat(1000), 1000, false)
62 measure('line-trivia-stream', ('; ' + 'comment '.repeat(100) + '\n').repeat(100) + '42 ', 1, true)
63 if os.args.contains('--extensions') {
64 measure('block-trivia-stream', '#|' + ('comment #| nested |# λ\r\n').repeat(10000) + '|#42 ', 1, true)
65 measure('datum-comment-stream', '#;(' + ('x ').repeat(100000) + ') 42 ', 1, true)
66 measure('escaped-identifiers-stream', r'|A\x0;B| '.repeat(1000), 1000, true)
67 measure('labelled-cycles-stream', '#1=(x . #1#) '.repeat(1000), 1000, true)
68 }
69}
70