module scheme import reader fn native_pair_test_list(n int) Value { mut xs := nil_value() for i in 1 .. n + 1 { xs = cons(integer(i),xs) } return xs } fn test_native_existing_integer_list_fold_runs_in_one_entry() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define (fold xs a)(if (null? (cdr xs)) (+ a (car xs)) (fold (cdr xs) (+ a (car xs)))))')! xs := native_pair_test_list(3000) tail := xs.pair_pointer().cdr vm.with_native_execution(fn [xs,tail] (mut vm VM) !Value { assert vm.call(vm.lookup('fold')!,[xs,integer(0)])!.whole() == 4501500 assert xs.pair_pointer().car.whole() == 3000 assert xs.pair_pointer().cdr.pointer() == tail.pointer() hits,deopts := vm.native_execution_stats() assert hits == 1 && deopts == 0 return unspecified() })! } fn test_native_pair_input_plan_handles_nil_then_mutated_existing_lists() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define xs (list 1 2 3)) (define (fold xs a)(if (null? xs) a (fold (cdr xs) (+ a (car xs)))))')! vm.with_native_execution(fn (mut vm VM) !Value { fold := vm.lookup('fold')! assert vm.call(fold,[nil_value(),integer(0)])!.whole() == 0 assert vm.call(fold,[vm.lookup('xs')!,integer(0)])!.whole() == 6 vm.eval('(set-car! xs 10) (set-cdr! (cdr xs) (list 30 40))')! $if gcboehm ? { gc_collect() } assert vm.call(fold,[vm.lookup('xs')!,integer(0)])!.whole() == 82 hits,deopts := vm.native_execution_stats() assert hits == 3 && deopts == 0 return unspecified() })! } fn test_native_pair_inputs_refresh_globals_and_same_code_captures() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define xs (list 1 2 3)) (define (global)(+ (car xs) (car (cdr xs)))) (define (make values)(lambda()(+ (car values) (car (cdr values)) (car (cdr (cdr values)))))) (define a (make (list 1 2 3))) (define b (make (list 10 20 30)))')! vm.with_native_execution(fn (mut vm VM) !Value { assert vm.call(vm.lookup('global')!,[])!.whole() == 3 assert vm.call(vm.lookup('a')!,[])!.whole() == 6 assert vm.call(vm.lookup('b')!,[])!.whole() == 60 vm.eval('(set! xs (list 10 20)) (set! a (make (list 100 200 300)))')! $if gcboehm ? { gc_collect() } assert vm.call(vm.lookup('global')!,[])!.whole() == 30 assert vm.call(vm.lookup('a')!,[])!.whole() == 600 _,deopts := vm.native_execution_stats() assert deopts == 0 return unspecified() })! } fn test_native_pair_inputs_share_capture_imports_without_exporting_identity() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! xs := native_pair_test_list(1300) vm.define('xs',xs,reader.ReadOptions{})! vm.eval('(define run ((lambda(xs)(lambda(a b)(+ (car a) (car b) (car xs) (car xs)))) xs))')! vm.with_native_execution(fn [xs] (mut vm VM) !Value { assert vm.call(vm.lookup('run')!,[xs,xs])!.whole() == 5200 assert xs.pair_pointer().car.whole() == 1300 hits,deopts := vm.native_execution_stats() assert hits == 1 && deopts == 0 return unspecified() })! } fn test_native_pair_import_rejections_are_bounded_and_keep_scalar_entries() { mut cycle := cons(integer(7),nil_value()) mut cell := cycle.pair_pointer() cell.cdr = cycle mut setup := new_with_options(Options{max_steps: 0,max_frames: 0})! big := setup.eval('9223372036854775808')! for bad in [cons(real(1.5),nil_value()),cons(big,nil_value()),cons(string_value('head'),nil_value()), cons(integer(1),integer(2)),cycle,native_pair_test_list(4097)] { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define (work xs)(if (pair? xs) (car xs) 42))')! work := vm.lookup('work')! expected := write(vm.call(work,[bad])!)! vm.with_native_execution(fn [bad,expected] (mut vm VM) !Value { work := vm.lookup('work')! assert vm.call(work,[nil_value()])!.whole() == 42 assert write(vm.call(work,[bad])!)! == expected scope := unsafe { &NativeScope(vm.native_execution_scope) } visited := scope.pair_nodes_visited assert visited > 0 && visited <= 4096 && scope.pair_input_rejections == 1 for _ in 0 .. 16 { assert write(vm.call(work,[bad])!)! == expected } assert scope.pair_nodes_visited == visited && scope.pair_input_rejections == 1 assert vm.call(work,[integer(3)])!.whole() == 42 assert vm.call(work,[nil_value()])!.whole() == 42 assert scope.hits == 3 && scope.deopts == 0 return unspecified() })! } } fn test_native_pair_pool_capacity_includes_imports_and_private_cons() { assert sizeof(C.VileNativePair) == 16 for n in [4095,4096] { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define (work xs)(car (cons 7 xs)))')! xs := native_pair_test_list(n) vm.with_native_execution(fn [xs,n] (mut vm VM) !Value { assert vm.call(vm.lookup('work')!,[xs])!.whole() == 7 hits,deopts := vm.native_execution_stats() if n == 4095 { assert hits == 1 && deopts == 0 } else { assert deopts == 1 } assert xs.pair_pointer().car.whole() == n return unspecified() })! } mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define (fold xs a)(if (null? xs) a (fold (cdr xs) (+ a (car xs)))))')! xs := native_pair_test_list(4096) vm.with_native_execution(fn [xs] (mut vm VM) !Value { assert vm.call(vm.lookup('fold')!,[xs,integer(0)])!.whole() == 8390656 hits,deopts := vm.native_execution_stats() assert hits == 1 && deopts == 0 return unspecified() })! } fn test_native_pair_results_and_stores_replay_with_original_tail_identity() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define (tail xs v)(vector-set! v 0 (+ (vector-ref v 0) 1)) (cdr xs)) (define (store xs v)(vector-set! v 0 (+ (vector-ref v 0) 1)) (vector-set! v 1 (cdr xs)) 42) (define cell (let ((held (quote ()))) (list (lambda(xs v)(vector-set! v 0 (+ (vector-ref v 0) 1)) (set! held (cdr xs)) 42) (lambda()held)))) (define capture (car cell)) (define get-held (cadr cell))')! xs := native_pair_test_list(3) tail := xs.pair_pointer().cdr vm.with_native_execution(fn [xs,tail] (mut vm VM) !Value { v := vector([integer(0),boolean(false)]) assert vm.call(vm.lookup('tail')!,[xs,v])!.pointer() == tail.pointer() assert v.vector_pointer().items[0].whole() == 1 assert vm.call(vm.lookup('store')!,[xs,v])!.whole() == 42 assert v.vector_pointer().items[0].whole() == 2 assert v.vector_pointer().items[1].pointer() == tail.pointer() assert vm.call(vm.lookup('capture')!,[xs,v])!.whole() == 42 assert v.vector_pointer().items[0].whole() == 3 assert vm.call(vm.lookup('get-held')!,[])!.pointer() == tail.pointer() _,deopts := vm.native_execution_stats() assert deopts >= 3 return unspecified() })! } fn test_native_pair_overflow_discards_scalar_and_vector_speculation_once() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define count 0) (define (make)(let ((count 0))(list (lambda(xs v)(set! count (+ count (car xs))) (vector-set! v 0 (+ (vector-ref v 0) (car xs))) (+ 9223372036854775807 1)) (lambda()count)))) (define cells (make)) (define work (car cells)) (define get (cadr cells))')! xs := native_pair_test_list(3) vm.with_native_execution(fn [xs] (mut vm VM) !Value { v := vector([integer(0)]) assert vm.call(vm.lookup('work')!,[xs,v])!.tag == .big_integer assert vm.call(vm.lookup('get')!,[])!.whole() == 3 assert v.vector_pointer().items[0].whole() == 3 assert xs.pair_pointer().car.whole() == 3 _,deopts := vm.native_execution_stats() assert deopts > 0 return unspecified() })! } fn test_native_pair_vector_reads_stay_on_the_existing_replay_path() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define (work v)(vector-set! v 0 (+ (vector-ref v 0) 1)) (car (vector-ref v 1)))')! xs := native_pair_test_list(77) vm.with_native_execution(fn [xs] (mut vm VM) !Value { v := vector([integer(0),xs]) assert vm.call(vm.lookup('work')!,[v])!.whole() == 77 assert v.vector_pointer().items[0].whole() == 1 _,deopts := vm.native_execution_stats() assert deopts == 1 return unspecified() })! } fn test_native_pair_input_folds_still_obey_vm_instruction_budgets() { mut vm := new_with_options(Options{max_steps: 200,max_frames: 0})! vm.eval('(define (fold xs a)(if (null? xs) a (fold (cdr xs) (+ a (car xs)))))')! xs := native_pair_test_list(3000) vm.with_native_execution(fn [xs] (mut vm VM) !Value { if _ := vm.call(vm.lookup('fold')!,[xs,integer(0)]) { return error('pair fold bypassed its budget') } hits,_ := vm.native_execution_stats() assert hits == 0 return unspecified() })! } fn test_native_pair_inputs_work_with_vile_frontend() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval_alt('work := fn(xs) { car(xs) + car(cdr(xs)) }')! vm.with_native_execution(fn (mut vm VM) !Value { assert vm.call(vm.lookup('work')!,[list([integer(20),integer(22)])])!.whole() == 42 hits,deopts := vm.native_execution_stats() assert hits == 1 && deopts == 0 return unspecified() })! } fn test_native_pair_inputs_share_one_bounded_pool_and_preserve_scalar_calls() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define (work a b)(if (pair? a) (+ (car a) (car b)) 42))')! xs := native_pair_test_list(3000) vm.with_native_execution(fn [xs] (mut vm VM) !Value { assert vm.call(vm.lookup('work')!,[xs,xs])!.whole() == 6000 scope := unsafe { &NativeScope(vm.native_execution_scope) } assert scope.pair_input_rejections == 1 && scope.pair_nodes_visited == 4096 assert vm.call(vm.lookup('work')!,[integer(0),nil_value()])!.whole() == 42 assert scope.hits == 1 && scope.deopts == 0 return unspecified() })! } fn test_native_pair_inputs_are_not_copied_for_graphs_that_do_not_read_them() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define (work xs)42)')! xs := native_pair_test_list(3000) vm.with_native_execution(fn [xs] (mut vm VM) !Value { assert vm.call(vm.lookup('work')!,[xs])!.whole() == 42 scope := unsafe { &NativeScope(vm.native_execution_scope) } assert scope.pair_nodes_visited == 0 && scope.hits == 0 assert vm.call(vm.lookup('work')!,[integer(0)])!.whole() == 42 assert scope.hits == 1 && scope.deopts == 0 return unspecified() })! }