module scheme import arena import reader fn test_context_cache_shares_unchanged_base_without_per_input_table_copies() { mut vm := new() mut code := vm.new_code(reader.read_all('') or { panic(err) }, vm.next_context(), unsafe { nil }, '') mut builder := Builder{ code: code, function: &Function{ code: code } } mut publication := arena.new(chunk_size: 1048576) publication.push() first := builder.capture_syntax_lookup_context() or { panic(err) } cached := code.procedural.globals_cache.snapshot before := publication.used() for _ in 0 .. 128 { current := builder.capture_syntax_lookup_context() or { panic(err) } assert current.binding('+').global_slot == first.binding('+').global_slot assert voidptr(code.procedural.globals_cache.snapshot) == voidptr(cached) } assert publication.used() - before < 128 * 2048 code.release_compile_borrows() assert isnil(code.procedural.globals_cache) assert first.binding('+').kind == .global publication.pop() publication.free() } fn test_context_cache_keeps_lexical_and_staged_binding_snapshots_immutable() { mut vm := new() mut code := vm.new_code(reader.read_all('') or { panic(err) }, vm.next_context(), unsafe { nil }, '') function := &Function{ code: code } mut outer := &Scope{ function: function } first_slot := outer.declare('x') mut builder := Builder{ code: code, function: function, scope: outer } before := builder.capture_syntax_lookup_context() or { panic(err) } snapshot := code.procedural.globals_cache.snapshot mut child := &Scope{ parent: outer, function: function } second_slot := child.declare('x') builder.scope = child mut generated := code.syntax_arena() generated.globals['staged'] = SyntaxBinding{ kind: .global, name: 'staged', global_slot: 42 } after := builder.capture_syntax_lookup_context() or { panic(err) } assert voidptr(code.procedural.globals_cache.snapshot) == voidptr(snapshot) assert before.binding('x').slot == first_slot assert after.binding('x').slot == second_slot assert before.binding('staged').unbound assert after.binding('staged').global_slot == 42 generated.globals['staged'] = SyntaxBinding{ kind: .global, name: 'staged', global_slot: 99 } last := builder.capture_syntax_lookup_context() or { panic(err) } assert after.binding('staged').global_slot == 42 assert last.binding('staged').global_slot == 99 code.release_compile_borrows() } fn context_macro_allocated(count int) int { mut vm := new() vm.eval('(define-syntax pass (lambda (stx) (syntax-case stx () ((_ e) (syntax e))))) (pass 0)') or { panic(err) } source := '(list ' + '(pass 1) '.repeat(count) + ')' mut publication := arena.new(chunk_size: 1048576) publication.push() result := vm.eval(source) or { panic(err) } used := publication.used() publication.pop() mut cursor := result for _ in 0 .. count { assert cursor.pair_pointer().car.as_integer() or { panic(err) } == 1 cursor = cursor.pair_pointer().cdr } assert cursor.tag == .nil publication.free() return used } fn test_identity_macro_allocation_grows_with_inputs_instead_of_full_global_tables() { small := context_macro_allocated(8) large := context_macro_allocated(128) assert large - small < 1048576, '8:${small} 128:${large}' } fn test_context_snapshot_initial_table_avoids_retained_growth_copies() { mut vm := new() mut code := vm.new_code(reader.read_all('') or { panic(err) }, vm.next_context(), unsafe { nil }, '') mut builder := Builder{ code: code, function: &Function{ code: code } } mut publication := arena.new(chunk_size: 1048576) publication.push() context := builder.capture_syntax_lookup_context() or { panic(err) } assert context.binding('+').global_slot == (*vm.bindings)['+'] assert publication.used() < vm.bindings.len * 512 + 65536 code.release_compile_borrows() publication.pop() publication.free() }