|
1 | 1 | //! STG allocation benchmarks
|
2 | 2 |
|
3 |
| -use eucalypt::common::sourcemap::Smid; |
4 |
| -use eucalypt::eval::machines::stg::env::*; |
5 |
| -use eucalypt::eval::machines::stg::syntax::dsl; |
6 |
| -use eucalypt::eval::machines::stg::syntax::LambdaForm; |
| 3 | +use std::iter; |
| 4 | + |
| 5 | +use eucalypt::{ |
| 6 | + common::sourcemap::Smid, |
| 7 | + eval::{ |
| 8 | + machine::{ |
| 9 | + env::{Closure, EnvFrame}, |
| 10 | + env_builder::EnvBuilder, |
| 11 | + }, |
| 12 | + memory::{ |
| 13 | + alloc::ScopedAllocator, |
| 14 | + array::Array, |
| 15 | + heap::Heap, |
| 16 | + mutator::MutatorHeapView, |
| 17 | + syntax::{LambdaForm, Native, Ref, RefPtr, StgBuilder}, |
| 18 | + }, |
| 19 | + stg::tags::DataConstructor, |
| 20 | + }, |
| 21 | +}; |
7 | 22 |
|
8 | 23 | use criterion::{black_box, criterion_group, criterion_main, Criterion};
|
9 | 24 |
|
10 |
| -use std::{cell::RefCell, iter, rc::Rc}; |
| 25 | +fn box_one<'guard>(view: MutatorHeapView<'guard>, empty: RefPtr<EnvFrame>) -> RefPtr<Closure> { |
| 26 | + view.alloc(Closure::new( |
| 27 | + view.data( |
| 28 | + DataConstructor::BoxedString.tag(), |
| 29 | + Array::from_slice(&view, &[Ref::V(Native::Num(1.into()))]), |
| 30 | + ) |
| 31 | + .unwrap() |
| 32 | + .as_ptr(), |
| 33 | + empty, |
| 34 | + )) |
| 35 | + .unwrap() |
| 36 | + .as_ptr() |
| 37 | +} |
| 38 | + |
| 39 | +fn identity<'guard>(view: MutatorHeapView<'guard>, empty: RefPtr<EnvFrame>) -> RefPtr<Closure> { |
| 40 | + view.alloc(Closure::close( |
| 41 | + &LambdaForm::new(1, view.atom(Ref::L(0)).unwrap().as_ptr(), Smid::default()), |
| 42 | + empty, |
| 43 | + )) |
| 44 | + .unwrap() |
| 45 | + .as_ptr() |
| 46 | +} |
11 | 47 |
|
12 |
| -fn fake_bindings(width: usize) -> Vec<LambdaForm> { |
13 |
| - iter::repeat_with(|| dsl::lambda(1, dsl::local(0))) |
14 |
| - .take(width) |
15 |
| - .collect() |
| 48 | +fn fake_bindings<'guard>(view: MutatorHeapView<'guard>, width: usize) -> Vec<LambdaForm> { |
| 49 | + iter::repeat_with(|| { |
| 50 | + LambdaForm::new(1, view.atom(Ref::L(0)).unwrap().as_ptr(), Smid::default()) |
| 51 | + }) |
| 52 | + .take(width) |
| 53 | + .collect() |
16 | 54 | }
|
17 | 55 |
|
18 |
| -fn fake_env_stack(width: usize, height: usize) -> Rc<EnvFrame> { |
19 |
| - let mut base = Rc::new(EnvFrame::empty()); |
| 56 | +fn fake_env_stack<'guard>( |
| 57 | + view: MutatorHeapView<'guard>, |
| 58 | + empty: RefPtr<EnvFrame>, |
| 59 | + width: usize, |
| 60 | + height: usize, |
| 61 | +) -> RefPtr<EnvFrame> { |
| 62 | + let mut base = empty; |
20 | 63 |
|
21 | 64 | for _ in 0..height {
|
22 |
| - let bindings = fake_bindings(width); |
23 |
| - base = EnvFrame::from_letrec(&bindings, &base, Smid::default()); |
| 65 | + let bindings = fake_bindings(view, width); |
| 66 | + base = view.from_letrec(&bindings, base, Smid::default()); |
24 | 67 | }
|
25 | 68 |
|
26 | 69 | base
|
27 | 70 | }
|
28 | 71 |
|
29 | 72 | /// Allocate a letrec of identify function bindings
|
30 |
| -fn alloc_let(bindings: &[LambdaForm]) -> Rc<EnvFrame> { |
31 |
| - EnvFrame::from_let(bindings, &Rc::new(EnvFrame::empty()), Smid::default()) |
| 73 | +fn alloc_let<'guard>( |
| 74 | + view: MutatorHeapView<'guard>, |
| 75 | + empty: RefPtr<EnvFrame>, |
| 76 | + bindings: &[LambdaForm], |
| 77 | +) -> RefPtr<EnvFrame> { |
| 78 | + view.from_let(bindings, empty, Smid::default()) |
32 | 79 | }
|
33 | 80 |
|
34 | 81 | /// Allocate a letrec of identify function bindings
|
35 |
| -fn alloc_letrec(bindings: &[LambdaForm]) -> Rc<EnvFrame> { |
36 |
| - EnvFrame::from_letrec(bindings, &Rc::new(EnvFrame::empty()), Smid::default()) |
| 82 | +fn alloc_letrec<'guard>( |
| 83 | + view: MutatorHeapView<'guard>, |
| 84 | + empty: RefPtr<EnvFrame>, |
| 85 | + bindings: &[LambdaForm], |
| 86 | +) -> RefPtr<EnvFrame> { |
| 87 | + view.from_letrec(bindings, empty, Smid::default()) |
37 | 88 | }
|
38 | 89 |
|
39 | 90 | /// Access deep closure
|
40 |
| -fn access(env: &Rc<EnvFrame>, depth: usize) -> Option<&RefCell<Closure>> { |
41 |
| - env.get(depth) |
| 91 | +fn access<'guard>( |
| 92 | + view: MutatorHeapView<'guard>, |
| 93 | + env: RefPtr<EnvFrame>, |
| 94 | + depth: usize, |
| 95 | +) -> Option<RefPtr<Closure>> { |
| 96 | + let e = view.scoped(env); |
| 97 | + (*e).get(&view, depth) |
42 | 98 | }
|
43 | 99 |
|
44 | 100 | /// Update deep closure with a new value
|
45 |
| -fn update(env: &Rc<EnvFrame>, depth: usize) { |
46 |
| - let value = Closure::new(dsl::box_num(1), Rc::new(EnvFrame::empty())); |
47 |
| - env.update(depth, value).unwrap(); |
| 101 | +fn update<'guard>( |
| 102 | + view: MutatorHeapView<'guard>, |
| 103 | + empty: RefPtr<EnvFrame>, |
| 104 | + env: RefPtr<EnvFrame>, |
| 105 | + depth: usize, |
| 106 | +) { |
| 107 | + let e = view.scoped(env); |
| 108 | + let value = box_one(view, empty); |
| 109 | + (*e).update(&view, depth, value).unwrap(); |
48 | 110 | }
|
49 | 111 |
|
50 | 112 | /// Create an identity lambda and saturate it
|
51 |
| -fn create_and_saturate_lambda() { |
52 |
| - let mut lambda = Closure::close(&dsl::lambda(1, dsl::local(0)), &Rc::new(EnvFrame::empty())); |
53 |
| - let args = vec![Closure::new(dsl::box_num(1), Rc::new(EnvFrame::empty()))]; |
54 |
| - lambda.saturate(args) |
| 113 | +fn create_and_saturate_lambda<'guard>(view: MutatorHeapView<'guard>, empty: RefPtr<EnvFrame>) { |
| 114 | + let mut lambda = view |
| 115 | + .alloc(Closure::close( |
| 116 | + &LambdaForm::new(1, view.atom(Ref::L(0)).unwrap().as_ptr(), Smid::default()), |
| 117 | + empty, |
| 118 | + )) |
| 119 | + .unwrap() |
| 120 | + .as_ptr(); |
| 121 | + let args = vec![box_one(view, empty)]; |
| 122 | + view.saturate(&*view.scoped(lambda), args.as_slice()) |
| 123 | + .unwrap(); |
55 | 124 | }
|
56 | 125 |
|
57 | 126 | /// Create an identity lambda and saturate it
|
58 |
| -fn create_partially_apply_and_saturate_lambda() { |
59 |
| - let mut lambda = Closure::close(&dsl::lambda(2, dsl::local(0)), &Rc::new(EnvFrame::empty())); |
60 |
| - let first_arg = vec![Closure::new(dsl::box_num(1), Rc::new(EnvFrame::empty()))]; |
61 |
| - let second_arg = vec![Closure::new(dsl::box_num(2), Rc::new(EnvFrame::empty()))]; |
62 |
| - |
63 |
| - lambda.partially_apply(first_arg); |
64 |
| - lambda.saturate(second_arg); |
| 127 | +fn create_partially_apply_and_saturate_lambda<'guard>( |
| 128 | + view: MutatorHeapView<'guard>, |
| 129 | + empty: RefPtr<EnvFrame>, |
| 130 | +) { |
| 131 | + let mut lambda = view |
| 132 | + .alloc(Closure::close( |
| 133 | + &LambdaForm::new(2, view.atom(Ref::L(0)).unwrap().as_ptr(), Smid::default()), |
| 134 | + empty, |
| 135 | + )) |
| 136 | + .unwrap() |
| 137 | + .as_ptr(); |
| 138 | + let first_arg = vec![box_one(view, empty)]; |
| 139 | + let second_arg = vec![box_one(view, empty)]; |
| 140 | + |
| 141 | + let lambda = view |
| 142 | + .partially_apply(&*view.scoped(lambda), first_arg.as_slice()) |
| 143 | + .unwrap(); |
| 144 | + view.saturate(&*view.scoped(lambda), second_arg.as_slice()) |
| 145 | + .unwrap(); |
65 | 146 | }
|
66 | 147 |
|
67 | 148 | fn criterion_benchmark(c: &mut Criterion) {
|
68 |
| - let bindings = fake_bindings(10); |
69 |
| - let env_stack = fake_env_stack(20, 4); |
70 |
| - c.bench_function("alloc_let", |b| b.iter(|| alloc_let(&bindings))); |
71 |
| - c.bench_function("alloc_letrec", |b| b.iter(|| alloc_letrec(&bindings))); |
| 149 | + let heap = Heap::new(); |
| 150 | + let view = MutatorHeapView::new(&heap); |
| 151 | + let empty = view.alloc(EnvFrame::default()).unwrap().as_ptr(); |
| 152 | + let bindings = fake_bindings(view, 10); |
| 153 | + let env_stack = fake_env_stack(view, empty, 20, 4); |
| 154 | + c.bench_function("alloc_let", |b| { |
| 155 | + b.iter(|| alloc_let(view, empty, &bindings)) |
| 156 | + }); |
| 157 | + c.bench_function("alloc_letrec", |b| { |
| 158 | + b.iter(|| alloc_letrec(view, empty, &bindings)) |
| 159 | + }); |
72 | 160 | c.bench_function("deep_env_access", |b| {
|
73 |
| - b.iter(|| access(&env_stack, black_box(73))) |
| 161 | + b.iter(|| access(view, env_stack, black_box(73))) |
74 | 162 | });
|
75 | 163 | c.bench_function("deep_env_update", |b| {
|
76 |
| - b.iter(|| update(&env_stack, black_box(73))) |
| 164 | + b.iter(|| update(view, empty, env_stack, black_box(73))) |
77 | 165 | });
|
78 | 166 |
|
79 | 167 | c.bench_function("create_and_saturate_lambda", |b| {
|
80 |
| - b.iter(create_and_saturate_lambda) |
| 168 | + b.iter(|| create_and_saturate_lambda(view, empty)) |
81 | 169 | });
|
82 | 170 | c.bench_function("create_partially_apply_and_saturate_lambda", |b| {
|
83 |
| - b.iter(create_partially_apply_and_saturate_lambda) |
| 171 | + b.iter(|| create_partially_apply_and_saturate_lambda(view, empty)) |
84 | 172 | });
|
85 | 173 | }
|
86 | 174 |
|
|
0 commit comments