| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | import reader |
| 5 | |
| 6 | fn test_context_cache_shares_unchanged_base_without_per_input_table_copies() { |
| 7 | mut vm := new() |
| 8 | mut code := vm.new_code(reader.read_all('') or { panic(err) }, vm.next_context(), unsafe { nil }, '') |
| 9 | mut builder := Builder{ code: code, function: &Function{ code: code } } |
| 10 | mut publication := arena.new(chunk_size: 1048576) |
| 11 | publication.push() |
| 12 | first := builder.capture_syntax_lookup_context() or { panic(err) } |
| 13 | cached := code.procedural.globals_cache.snapshot |
| 14 | before := publication.used() |
| 15 | for _ in 0 .. 128 { |
| 16 | current := builder.capture_syntax_lookup_context() or { panic(err) } |
| 17 | assert current.binding('+').global_slot == first.binding('+').global_slot |
| 18 | assert voidptr(code.procedural.globals_cache.snapshot) == voidptr(cached) |
| 19 | } |
| 20 | assert publication.used() - before < 128 * 2048 |
| 21 | code.release_compile_borrows() |
| 22 | assert isnil(code.procedural.globals_cache) |
| 23 | assert first.binding('+').kind == .global |
| 24 | publication.pop() |
| 25 | publication.free() |
| 26 | } |
| 27 | |
| 28 | fn test_context_cache_keeps_lexical_and_staged_binding_snapshots_immutable() { |
| 29 | mut vm := new() |
| 30 | mut code := vm.new_code(reader.read_all('') or { panic(err) }, vm.next_context(), unsafe { nil }, '') |
| 31 | function := &Function{ code: code } |
| 32 | mut outer := &Scope{ function: function } |
| 33 | first_slot := outer.declare('x') |
| 34 | mut builder := Builder{ code: code, function: function, scope: outer } |
| 35 | before := builder.capture_syntax_lookup_context() or { panic(err) } |
| 36 | snapshot := code.procedural.globals_cache.snapshot |
| 37 | mut child := &Scope{ parent: outer, function: function } |
| 38 | second_slot := child.declare('x') |
| 39 | builder.scope = child |
| 40 | mut generated := code.syntax_arena() |
| 41 | generated.globals['staged'] = SyntaxBinding{ kind: .global, name: 'staged', global_slot: 42 } |
| 42 | after := builder.capture_syntax_lookup_context() or { panic(err) } |
| 43 | assert voidptr(code.procedural.globals_cache.snapshot) == voidptr(snapshot) |
| 44 | assert before.binding('x').slot == first_slot |
| 45 | assert after.binding('x').slot == second_slot |
| 46 | assert before.binding('staged').unbound |
| 47 | assert after.binding('staged').global_slot == 42 |
| 48 | generated.globals['staged'] = SyntaxBinding{ kind: .global, name: 'staged', global_slot: 99 } |
| 49 | last := builder.capture_syntax_lookup_context() or { panic(err) } |
| 50 | assert after.binding('staged').global_slot == 42 |
| 51 | assert last.binding('staged').global_slot == 99 |
| 52 | code.release_compile_borrows() |
| 53 | } |
| 54 | |
| 55 | fn context_macro_allocated(count int) int { |
| 56 | mut vm := new() |
| 57 | vm.eval('(define-syntax pass (lambda (stx) (syntax-case stx () ((_ e) (syntax e))))) (pass 0)') or { panic(err) } |
| 58 | source := '(list ' + '(pass 1) '.repeat(count) + ')' |
| 59 | mut publication := arena.new(chunk_size: 1048576) |
| 60 | publication.push() |
| 61 | result := vm.eval(source) or { panic(err) } |
| 62 | used := publication.used() |
| 63 | publication.pop() |
| 64 | mut cursor := result |
| 65 | for _ in 0 .. count { |
| 66 | assert cursor.pair_pointer().car.as_integer() or { panic(err) } == 1 |
| 67 | cursor = cursor.pair_pointer().cdr |
| 68 | } |
| 69 | assert cursor.tag == .nil |
| 70 | publication.free() |
| 71 | return used |
| 72 | } |
| 73 | |
| 74 | fn test_identity_macro_allocation_grows_with_inputs_instead_of_full_global_tables() { |
| 75 | small := context_macro_allocated(8) |
| 76 | large := context_macro_allocated(128) |
| 77 | assert large - small < 1048576, '8:${small} 128:${large}' |
| 78 | } |
| 79 | |
| 80 | fn test_context_snapshot_initial_table_avoids_retained_growth_copies() { |
| 81 | mut vm := new() |
| 82 | mut code := vm.new_code(reader.read_all('') or { panic(err) }, vm.next_context(), unsafe { nil }, '') |
| 83 | mut builder := Builder{ code: code, function: &Function{ code: code } } |
| 84 | mut publication := arena.new(chunk_size: 1048576) |
| 85 | publication.push() |
| 86 | context := builder.capture_syntax_lookup_context() or { panic(err) } |
| 87 | assert context.binding('+').global_slot == (*vm.bindings)['+'] |
| 88 | assert publication.used() < vm.bindings.len * 512 + 65536 |
| 89 | code.release_compile_borrows() |
| 90 | publication.pop() |
| 91 | publication.free() |
| 92 | } |
| 93 | |