Pairs are special in both traditional Racket and Chez Scheme.

In the traditional Racket implementation, every allocated object has a
16-bit tag, and one of those is "pair". For a structure, the tag is
"structure", and then there's another word for the structure type. So,
the test for a pair is faster (e.g., in `car`) than the test for a
structure type (e.g., in a structure accessor). Pairs don't turn out to
be any more compact, though, due to alignment constraints.

Chez Scheme has similar difference, only better. Pairs are tagged using
low bits in the pointer that refers to a pair, so a `pair?` test
doesn't even need an indirection. A pair is also allocated more
compactly --- as exactly two words --- and it's treated specially by
the GC in other ways. A structure instance has a tag in the allocated
object, but the tag does at least combine the fact that the object a
structure and the structure type that it instantiates.

The compilers in both cases know some facts about how `cons` relates to
other primitives, but they also know how structure constructors and
accessors relate, so it's not as big a difference at that level.

At Tue, 27 Nov 2018 18:06:36 -0800, Vishesh Yadav wrote:
> I recall reading some 6.x code for RacketScript, and in 6.x world pair
> was simply a pair of pointers in C structure[1] and `cons` was again a
> primitive function that created those C objects[2]. No special
> representation in byte-code [all values are opaque to bytecode (except
> ints and floats I think)?]. My impression was it didn't do anything
> special with it, at-least explicitly. Things can be different now with
> Chez.
> 
> [1] 
> https://github.com/racket/racket/blob/v6.12/racket/src/racket/include/scheme.h#
> L347
> [2] 
> https://github.com/racket/racket/blob/v6.12/racket/src/racket/src/list.c#L1050
> On Tue, Nov 27, 2018 at 5:24 PM Milo Turner <iital...@gmail.com> wrote:
> >
> > Hello all,
> >
> > There's something I have been very curious of lately, about low level 
> aspects of Racket's compiler.
> > First of all, I have always considered "cons" to be essentially the same as 
> a struct:
> >
> > (struct cons [hd tl] #:transparent)
> > (define car (procedure-rename cons-hd 'car))
> > (define cdr (procedure-rename cons-tl 'cdr))
> >
> > However I would not be surprised if cons had special treatment in the 
> compiler, runtime, etc.
> >
> > Is this the case? How frequently does Racket contain special optimizations 
> for cons cells? Does cons have a special representation at runtime or at the 
> bytecode level?
> > Thanks.
> >
> > --
> > You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> > To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to