1 USING: compiler.tree.escape-analysis
2 compiler.tree.escape-analysis.allocations compiler.tree.builder
3 compiler.tree.recursive compiler.tree.normalization
4 math.functions compiler.tree.propagation compiler.tree.cleanup
5 compiler.tree.combinators compiler.tree sequences math
6 math.private kernel tools.test accessors slots.private
7 quotations.private prettyprint classes.tuple.private classes
8 classes.tuple namespaces
9 compiler.tree.propagation.info stack-checker.errors
11 kernel.private vectors ;
12 IN: compiler.tree.escape-analysis.tests
14 GENERIC: count-unboxed-allocations* ( m node -- n )
16 : (count-unboxed-allocations) ( m node -- n )
17 out-d>> first escaping-allocation? [ 1 + ] unless ;
19 M: #call count-unboxed-allocations*
20 dup immutable-tuple-boa?
21 [ (count-unboxed-allocations) ] [ drop ] if ;
23 M: #push count-unboxed-allocations*
24 dup literal>> class immutable-tuple-class?
25 [ (count-unboxed-allocations) ] [ drop ] if ;
27 M: #introduce count-unboxed-allocations*
28 out-d>> [ escaping-allocation? [ 1 + ] unless ] each ;
30 M: node count-unboxed-allocations* drop ;
32 : count-unboxed-allocations ( quot -- sizes )
40 0 swap [ count-unboxed-allocations* ] each-node ;
42 [ 0 ] [ [ [ + ] curry ] count-unboxed-allocations ] unit-test
44 [ 1 ] [ [ [ + ] curry drop ] count-unboxed-allocations ] unit-test
46 [ 1 ] [ [ [ + ] curry 3 slot ] count-unboxed-allocations ] unit-test
48 [ 1 ] [ [ [ + ] curry 3 slot drop ] count-unboxed-allocations ] unit-test
50 [ 1 ] [ [ [ + ] curry uncurry ] count-unboxed-allocations ] unit-test
52 [ 1 ] [ [ [ + ] curry call ] count-unboxed-allocations ] unit-test
54 [ 1 ] [ [ [ + ] curry call ] count-unboxed-allocations ] unit-test
56 [ 0 ] [ [ [ [ + ] curry ] [ drop [ ] ] if ] count-unboxed-allocations ] unit-test
59 [ [ [ + ] curry ] [ [ * ] curry ] if uncurry ] count-unboxed-allocations
63 [ [ [ + ] curry ] [ [ * ] curry ] if ] count-unboxed-allocations
67 [ [ [ + ] curry ] [ dup [ [ * ] curry ] [ [ / ] curry ] if ] if uncurry ] count-unboxed-allocations
71 [ [ [ + ] curry 4 ] [ dup [ [ * ] curry ] [ [ / ] curry ] if uncurry ] if ] count-unboxed-allocations
75 [ [ [ + ] curry ] [ dup [ [ * ] curry ] [ [ / ] curry ] if ] if ] count-unboxed-allocations
78 TUPLE: cons { car read-only } { cdr read-only } ;
89 ] count-unboxed-allocations
103 ] count-unboxed-allocations
115 ] count-unboxed-allocations
129 ] count-unboxed-allocations
143 ] count-unboxed-allocations
148 [ dup cons boa ] [ drop 1 2 cons boa ] if car>>
149 ] count-unboxed-allocations
155 [ cons boa ] [ cons boa 3 cons boa ] if
156 [ car>> ] [ cdr>> ] bi
157 ] count-unboxed-allocations
162 3dup [ cons boa ] [ cons boa . 1 2 cons boa ] if
163 [ car>> ] [ cdr>> ] bi
164 ] count-unboxed-allocations
168 [ [ 3 cons boa ] [ "A" throw ] if car>> ]
169 count-unboxed-allocations
173 [ 10 [ drop ] each-integer ] count-unboxed-allocations
178 1 2 cons boa 10 [ 2drop 1 2 cons boa ] each-integer car>>
179 ] count-unboxed-allocations
184 1 2 cons boa 10 [ drop 2 cons boa ] each-integer car>>
185 ] count-unboxed-allocations
188 : infinite-cons-loop ( a -- b ) 2 cons boa infinite-cons-loop ; inline recursive
192 1 2 cons boa infinite-cons-loop
193 ] count-unboxed-allocations
200 [ 0 ] [ [ <rw-box> i>> ] count-unboxed-allocations ] unit-test
202 : fake-fib ( m -- n )
203 dup i>> 1 <= [ drop 1 <rw-box> ] when ; inline recursive
205 [ 0 ] [ [ <rw-box> fake-fib i>> ] count-unboxed-allocations ] unit-test
207 TUPLE: ro-box { i read-only } ;
211 : tuple-fib ( m -- n )
220 swap i>> swap i>> + <ro-box>
221 ] if ; inline recursive
223 [ 5 ] [ [ <ro-box> tuple-fib i>> ] count-unboxed-allocations ] unit-test
225 [ 3 ] [ [ <ro-box> tuple-fib ] count-unboxed-allocations ] unit-test
227 : tuple-fib' ( m -- n )
228 dup 1 <= [ 1 - tuple-fib' i>> ] when <ro-box> ; inline recursive
230 [ 0 ] [ [ tuple-fib' ] count-unboxed-allocations ] unit-test
232 : bad-tuple-fib-1 ( m -- n )
240 bad-tuple-fib-1 dup .
241 swap i>> swap i>> + <ro-box>
242 ] if ; inline recursive
244 [ 3 ] [ [ <ro-box> bad-tuple-fib-1 i>> ] count-unboxed-allocations ] unit-test
246 : bad-tuple-fib-2 ( m -- n )
256 swap i>> swap i>> + <ro-box>
257 ] if ; inline recursive
259 [ 2 ] [ [ <ro-box> bad-tuple-fib-2 i>> ] count-unboxed-allocations ] unit-test
261 : tuple-fib-2 ( m -- n )
268 swap i>> swap i>> + <ro-box>
269 ] if ; inline recursive
271 [ 2 ] [ [ tuple-fib-2 i>> ] count-unboxed-allocations ] unit-test
273 : tuple-fib-3 ( m -- n )
279 1 - tuple-fib-3 dup .
280 swap i>> swap i>> + <ro-box>
281 ] if ; inline recursive
283 [ 0 ] [ [ tuple-fib-3 i>> ] count-unboxed-allocations ] unit-test
285 : bad-tuple-fib-3 ( m -- n )
289 1 - dup bad-tuple-fib-3
293 ] if ; inline recursive
295 [ 0 ] [ [ bad-tuple-fib-3 i>> ] count-unboxed-allocations ] unit-test
297 [ 1 ] [ [ complex boa >rect ] count-unboxed-allocations ] unit-test
299 [ 0 ] [ [ 1 cons boa 2 cons boa ] count-unboxed-allocations ] unit-test
301 [ 1 ] [ [ 1 cons boa 2 cons boa car>> ] count-unboxed-allocations ] unit-test
303 [ 0 ] [ [ 1 cons boa 2 cons boa dup . car>> ] count-unboxed-allocations ] unit-test
305 [ 0 ] [ [ 1 cons boa "x" get slot ] count-unboxed-allocations ] unit-test
307 : impeach-node ( quot: ( node -- ) -- )
308 [ call ] keep impeach-node ; inline recursive
310 : bleach-node ( quot: ( node -- ) -- )
311 [ bleach-node ] curry [ ] compose impeach-node ; inline recursive
313 [ 3 ] [ [ [ ] bleach-node ] count-unboxed-allocations ] unit-test
316 [ dup -1 over >= [ 0 >= [ "A" throw ] unless ] [ drop ] if ]
317 count-unboxed-allocations
321 [ \ too-many->r boa f f \ inference-error boa ]
322 count-unboxed-allocations
326 [ { null } declare [ 1 ] [ 2 ] if ] count-unboxed-allocations
329 ! Doug found a regression
333 [ ] [ [ empty-tuple boa layout-of ] count-unboxed-allocations drop ] unit-test
337 [ 1 ] [ [ { complex } declare real>> ] count-unboxed-allocations ] unit-test
340 [ { complex } declare [ real>> ] [ imaginary>> ] bi ]
341 count-unboxed-allocations
345 [ { vector } declare length>> ]
346 count-unboxed-allocations