Re: Constructors, excessive consing, R7RS Large
Marc Nieper-WiÃkirchen 14 Jan 2025 17:05 UTC
Thanks for these experiments.
Note that cp0 knows the R6RS record types by heart, so to speak. It is
able to do far more optimisations with record types (and some R6RS
semantics are geared to it so that it is possible) than with arbitrary
code. This is certainly related to the full-employment theorem [1] as
an optimiser will always have to make compromises between specificity
and generality.
This is also a problem for more primitive systems on which R6RS
records can, in principle, be effectively built. An optimiser not
tailored to a specific form of such primitive, low-level code will
likely not be working as well as with high-level constructs that are
baked in. For the language designer, it means that the principle of
reducing everything to fundamental structures and functions (which is,
from a purist's point of view, appealing) must be watered down
sometimes.
--
[1] https://en.wikipedia.org/wiki/Full-employment_theorem
Am Di., 14. Jan. 2025 um 17:52 Uhr schrieb Daphne Preston-Kendal
<xxxxxx@nonceword.org>:
>
> On 22 Nov 2024, at 08:08, Marc Nieper-Wißkirchen <xxxxxx@gmail.com> wrote:
>
> > Chez Scheme's optimiser is not able to remove the temporary heap
> > allocation in the following example, although it is able to inline the
> > procedures.
>
> With the following example, cp0 does remove the intermediate allocation:
>
> (library (a-b)
> (export my-make-b make-a a-x b-y)
> (import (rnrs))
> (define-record-type a (fields (immutable x)))
> (define-record-type b (fields (mutable y)) (parent a))
> (define (my-make-b an-a y)
> (make-b (a-x an-a) y)))
>
> (my-make-b (make-a 4) 5)
>
> If the b-y field is declared immutable, it will also instantiate the record instance during cp0.
>
> If the a-x field is declared mutable, then you have a problem.
>
> > The average Scheme implementation will likely do even fewer optimisations.
>
>
> This is probably correct. (Guile, for example, can’t do this – at least when the R6RS d-r-t is used, like here. Verified with the above example code and the ,optimize and ,disassemble REPL commands.)
>
>
> Daphne
>