It is sometimes convenient to create structures at a high rate for very temporary, local use. For example, a structure is a convenient way of packaging up state in one function to be passed to functions it is calling: If the state contains two or three dozen slots, passing a single structure through a forest of mutually recursive functions can be much more appetizing than passing the state as two or three dozen parameters to each function, or -- worse -- in two or three dozen global symbols.
In cases like these, one may be creating structures very frequently which are known to be unwanted on return from the creating function, and it may be much more efficient to allocate them on the stack than to have the general-purpose garbage collector find and recycle them all.
I intend that at some point one should be able to allocate any structure on the stack simply by doing
[ :ephemeral t ... | ]make-whatever
in place of the vanilla
[ ... | ]make-whatever
call. Unfortunately, ]make-whatever
is itself
an in-db function, and while the structure gets
stack-allocated properly, it also gets de-allocated
upon return from that function, which makes the whole
exercise fairly useless!
This will be resolved eventually by implementing
inline functions, and making ]make-whatever
inline. In the meantime, one must use the
slightly less elegant code sequence
[ :ephemeral t ... | 'whatever ]makeStructure
to allocate an ephemeral structure. For example, to create an ephemeral saint which will vanish when the expression completes execution, we may do:
Stack: [ :ephemeral t :name "Patrick" | 'saint ]makeStructure -> s s saint? Stack: t
Ephemeral structures should be used with caution.
In particular, they should never be passed to another job, because that job will search its own stack for the structure and either not find anything, or -- worse! -- find the wrong thing. For the same reason, ephemeral structures should seldom be stored into the database. In general, you should only pass them as function parameters and store them in local variables.
See section `]defstruct' in Muf Reference.
Go to the first, previous, next, last section, table of contents.