158 lines
6.9 KiB
HTML
158 lines
6.9 KiB
HTML
|
<!DOCTYPE html>
|
||
|
<html>
|
||
|
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||
|
<!-- This manual documents Guile version 3.0.10.
|
||
|
|
||
|
Copyright (C) 1996-1997, 2000-2005, 2009-2023 Free Software Foundation,
|
||
|
Inc.
|
||
|
|
||
|
Copyright (C) 2021 Maxime Devos
|
||
|
|
||
|
Copyright (C) 2024 Tomas Volf
|
||
|
|
||
|
|
||
|
Permission is granted to copy, distribute and/or modify this document
|
||
|
under the terms of the GNU Free Documentation License, Version 1.3 or
|
||
|
any later version published by the Free Software Foundation; with no
|
||
|
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
|
||
|
copy of the license is included in the section entitled "GNU Free
|
||
|
Documentation License." -->
|
||
|
<title>Cheaper Pairs (Guile Reference Manual)</title>
|
||
|
|
||
|
<meta name="description" content="Cheaper Pairs (Guile Reference Manual)">
|
||
|
<meta name="keywords" content="Cheaper Pairs (Guile Reference Manual)">
|
||
|
<meta name="resource-type" content="document">
|
||
|
<meta name="distribution" content="global">
|
||
|
<meta name="Generator" content=".texi2any-real">
|
||
|
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||
|
|
||
|
<link href="index.html" rel="start" title="Top">
|
||
|
<link href="Concept-Index.html" rel="index" title="Concept Index">
|
||
|
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
||
|
<link href="Data-Representation.html" rel="up" title="Data Representation">
|
||
|
<link href="Conservative-GC.html" rel="next" title="Conservative GC">
|
||
|
<link href="Faster-Integers.html" rel="prev" title="Faster Integers">
|
||
|
<style type="text/css">
|
||
|
<!--
|
||
|
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
|
||
|
div.example {margin-left: 3.2em}
|
||
|
span:hover a.copiable-link {visibility: visible}
|
||
|
ul.mark-bullet {list-style-type: disc}
|
||
|
-->
|
||
|
</style>
|
||
|
<link rel="stylesheet" type="text/css" href="https://www.gnu.org/software/gnulib/manual.css">
|
||
|
|
||
|
|
||
|
</head>
|
||
|
|
||
|
<body lang="en">
|
||
|
<div class="subsection-level-extent" id="Cheaper-Pairs">
|
||
|
<div class="nav-panel">
|
||
|
<p>
|
||
|
Next: <a href="Conservative-GC.html" accesskey="n" rel="next">Conservative Garbage Collection</a>, Previous: <a href="Faster-Integers.html" accesskey="p" rel="prev">Faster Integers</a>, Up: <a href="Data-Representation.html" accesskey="u" rel="up">Data Representation</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
|
||
|
</div>
|
||
|
<hr>
|
||
|
<h4 class="subsection" id="Cheaper-Pairs-1"><span>9.2.3 Cheaper Pairs<a class="copiable-link" href="#Cheaper-Pairs-1"> ¶</a></span></h4>
|
||
|
|
||
|
<p>However, there is yet another issue to confront. Most Scheme heaps
|
||
|
contain more pairs than any other type of object; Jonathan Rees said at
|
||
|
one point that pairs occupy 45% of the heap in his Scheme
|
||
|
implementation, Scheme 48. However, our representation above spends
|
||
|
three <code class="code">SCM</code>-sized words per pair — one for the type, and two for
|
||
|
the <small class="sc">CAR</small> and <small class="sc">CDR</small>. Is there any way to represent pairs using
|
||
|
only two words?
|
||
|
</p>
|
||
|
<p>Let us refine the convention we established earlier. Let us assert
|
||
|
that:
|
||
|
</p><ul class="itemize mark-bullet">
|
||
|
<li>If the bottom three bits of an <code class="code">SCM</code> value are <code class="code">#b000</code>, then
|
||
|
it is a pointer, as before.
|
||
|
</li><li>If the bottom three bits are <code class="code">#b001</code>, then the upper bits are an
|
||
|
integer. This is a bit more restrictive than before.
|
||
|
</li><li>If the bottom two bits are <code class="code">#b010</code>, then the value, with the bottom
|
||
|
three bits masked out, is the address of a pair.
|
||
|
</li></ul>
|
||
|
|
||
|
<p>Here is the new C code:
|
||
|
</p><div class="example">
|
||
|
<pre class="example-preformatted">enum type { string, vector, ... };
|
||
|
|
||
|
typedef struct value *SCM;
|
||
|
|
||
|
struct value {
|
||
|
enum type type;
|
||
|
union {
|
||
|
struct { int length; char *elts; } string;
|
||
|
struct { int length; SCM *elts; } vector;
|
||
|
...
|
||
|
} value;
|
||
|
};
|
||
|
|
||
|
struct pair {
|
||
|
SCM car, cdr;
|
||
|
};
|
||
|
|
||
|
#define POINTER_P(x) (((int) (x) & 7) == 0)
|
||
|
|
||
|
#define INTEGER_P(x) (((int) (x) & 7) == 1)
|
||
|
#define GET_INTEGER(x) ((int) (x) >> 3)
|
||
|
#define MAKE_INTEGER(x) ((SCM) (((x) << 3) | 1))
|
||
|
|
||
|
#define PAIR_P(x) (((int) (x) & 7) == 2)
|
||
|
#define GET_PAIR(x) ((struct pair *) ((int) (x) & ~7))
|
||
|
</pre></div>
|
||
|
|
||
|
<p>Notice that <code class="code">enum type</code> and <code class="code">struct value</code> now only contain
|
||
|
provisions for vectors and strings; both integers and pairs have become
|
||
|
special cases. The code above also assumes that an <code class="code">int</code> is large
|
||
|
enough to hold a pointer, which isn’t generally true.
|
||
|
</p>
|
||
|
|
||
|
<p>Our list of examples is now as follows:
|
||
|
</p><ul class="itemize mark-bullet">
|
||
|
<li>To test if <var class="var">x</var> is an integer, we can write <code class="code">INTEGER_P
|
||
|
(<var class="var">x</var>)</code>; this is as before.
|
||
|
</li><li>To find its value, we can write <code class="code">GET_INTEGER (<var class="var">x</var>)</code>, as
|
||
|
before.
|
||
|
</li><li>To test if <var class="var">x</var> is a vector, we can write:
|
||
|
<div class="example">
|
||
|
<pre class="example-preformatted"> <code class="code">POINTER_P (<var class="var">x</var>) && <var class="var">x</var>->type == vector</code>
|
||
|
</pre></div>
|
||
|
<p>We must still make sure that <var class="var">x</var> is a pointer to a <code class="code">struct
|
||
|
value</code> before dereferencing it to find its type.
|
||
|
</p></li><li>If we know <var class="var">x</var> is a vector, we can write
|
||
|
<code class="code"><var class="var">x</var>->value.vector.elts[0]</code> to refer to its first element, as
|
||
|
before.
|
||
|
</li><li>We can write <code class="code">PAIR_P (<var class="var">x</var>)</code> to determine if <var class="var">x</var> is a
|
||
|
pair, and then write <code class="code">GET_PAIR (<var class="var">x</var>)->car</code> to refer to its
|
||
|
car.
|
||
|
</li></ul>
|
||
|
|
||
|
<p>This change in representation reduces our heap size by 15%. It also
|
||
|
makes it cheaper to decide if a value is a pair, because no memory
|
||
|
references are necessary; it suffices to check the bottom two bits of
|
||
|
the <code class="code">SCM</code> value. This may be significant when traversing lists, a
|
||
|
common activity in a Scheme system.
|
||
|
</p>
|
||
|
<p>Again, most real Scheme systems use a slightly different implementation;
|
||
|
for example, if GET_PAIR subtracts off the low bits of <code class="code">x</code>, instead
|
||
|
of masking them off, the optimizer will often be able to combine that
|
||
|
subtraction with the addition of the offset of the structure member we
|
||
|
are referencing, making a modified pointer as fast to use as an
|
||
|
unmodified pointer.
|
||
|
</p>
|
||
|
|
||
|
</div>
|
||
|
<hr>
|
||
|
<div class="nav-panel">
|
||
|
<p>
|
||
|
Next: <a href="Conservative-GC.html">Conservative Garbage Collection</a>, Previous: <a href="Faster-Integers.html">Faster Integers</a>, Up: <a href="Data-Representation.html">Data Representation</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
|
||
|
</div>
|
||
|
|
||
|
|
||
|
|
||
|
</body>
|
||
|
</html>
|