aboutsummaryrefslogtreecommitdiff
path: root/notes/loopcall_alloc_optim.txt
blob: 479346efbb4bb111427ad3aec9ae3437fca6ec15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

Using "loopcall" for smarter allocation
=======================================

See `loopcall_lambdas_etc.txt`

A special loopcall calling convention could also have a mode for querying
the function for metadata, such as whether parameters escape, whether the
memory allocation size can be known before the time of call, etc.

This way, if you have a sequence of functions in a loop...

    for item in items
        Thing! a = item.construct_thing! 1
        Thing! b = item.construct_thing! 2
        Thing! c = item.construct_thing! 3
        a.process! b c
        do_stuff a
    end

...you could query the following before the loop...

* if `construct_thing` escapes/keeps its return value.
* if `process` cannot escapes/keeps its parameters
* whether the allocation size can be determined, and what it is
  (checked recursively, if `Thing` contains nested allocations in fields
  or for temporary variables)

...if both are false, then `a`, `b` and `c` could be pre-allocated
   (and possibly even on stack if there happens to be free space there)