0.8.6:
[sbcl.git] / doc / efficiency.sgml
1 <chapter id="efficiency"><title>Efficiency</>
2
3 <para>FIXME: The material in the &CMUCL; manual about getting good
4 performance from the compiler should be reviewed, reformatted in
5 DocBook, lightly edited for &SBCL;, and substituted into this
6 manual. In the meantime, the original &CMUCL; manual is still 95+%
7 correct for the &SBCL; version of the &Python; compiler. See the
8 sections
9 <itemizedlist>
10   <listitem><para>Advanced Compiler Use and Efficiency Hints</></>
11   <listitem><para>Advanced Compiler Introduction</></>
12   <listitem><para>More About Types in Python</></>
13   <listitem><para>Type Inference</></>
14   <listitem><para>Source Optimization</></>
15   <listitem><para>Tail Recursion</></>
16   <listitem><para>Local Call</></>
17   <listitem><para>Block Compilation</></>
18   <listitem><para>Inline Expansion</></>
19   <listitem><para>Object Representation</></>
20   <listitem><para>Numbers</></>
21   <listitem><para>General Efficiency Hints</></>
22   <listitem><para>Efficiency Notes</></>
23 </itemizedlist>
24 </para>
25
26 <para>Besides this information from the &CMUCL; manual, there are a
27 few other points to keep in mind.
28 <itemizedlist>
29   <listitem><para>The &CMUCL; manual doesn't seem to state it explicitly,
30     but &Python; has a mental block about type inference when
31     assignment is involved. &Python; is very aggressive and clever
32     about inferring the types of values bound with <function>let</>,
33     <function>let*</>, inline function call, and so forth. However,
34     it's much more passive and dumb about inferring the types of
35     values assigned with <function>setq</>, <function>setf</>, and
36     friends. It would be nice to fix this, but in the meantime don't
37     expect that just because it's very smart about types in most
38     respects it will be smart about types involved in assignments.
39     (This doesn't affect its ability to benefit from explicit type
40     declarations involving the assigned variables, only its ability to
41     get by without explicit type declarations.)</para></listitem>
42 <!-- FIXME: Python dislikes assignments, but not in type
43     inference. The real problems are loop induction, closed over
44     variables and aliases. -->
45   <listitem><para>Since the time the &CMUCL; manual was written,
46     &CMUCL; (and thus &SBCL;) has gotten a generational garbage
47     collector. This means that there are some efficiency implications
48     of various patterns of memory usage which aren't discussed in the
49     &CMUCL; manual. (Some new material should be written about
50     this.)</para></listitem>
51   <listitem><para>&SBCL; has some important known efficiency problems.
52     Perhaps the most important are 
53     <itemizedlist>
54       <listitem><para>There is no support for the &ANSI;
55         <parameter>dynamic-extent</> declaration, not even for 
56         closures or <parameter>&amp;rest</> lists.</para></listitem>
57       <listitem><para>The garbage collector is not particularly
58         efficient.</para></listitem>
59       <listitem><para>Various aspects of the PCL implementation
60         of CLOS are more inefficient than necessary.</para></listitem>
61     </itemizedlist>
62   </para></listitem>
63 </itemizedlist>
64 </para>
65
66 <para>Finally, note that &CommonLisp; defines many constructs which, in
67 the infamous phrase, <quote>could be compiled efficiently by a
68 sufficiently smart compiler</quote>. The phrase is infamous because
69 making a compiler which actually is sufficiently smart to find all
70 these optimizations systematically is well beyond the state of the art
71 of current compiler technology. Instead, they're optimized on a
72 case-by-case basis by hand-written code, or not optimized at all if
73 the appropriate case hasn't been hand-coded. Some cases where no such
74 hand-coding has been done as of &SBCL; version 0.6.3 include
75 <itemizedlist>
76   <listitem><para><literal>(reduce #'f x)</>
77     where the type of <varname>x</> is known at compile
78     time</para></listitem>
79   <listitem><para>various bit vector operations, e.g.
80     <literal>(position 0 some-bit-vector)</></para></listitem>
81 </itemizedlist>
82 If your system's performance is suffering because of some construct
83 which could in principle be compiled efficiently, but which the &SBCL;
84 compiler can't in practice compile efficiently, consider writing a
85 patch to the compiler and submitting it for inclusion in the main
86 sources. Such code is often reasonably straightforward to write;
87 search the sources for the string <quote><function>deftransform</></>
88 to find many examples (some straightforward, some less so).</para>
89
90 <sect1 id="modular-arithmetic"><title>Modular arithmetic</>
91 <para>
92 Some numeric functions have a property: <varname>N</> lower bits of
93 the result depend only on <varname>N</> lower bits of (all or some)
94 arguments. If the compiler sees an expression of form <literal>(logand
95 exp mask)</>, where <varname>exp</> is a tree of such "good" functions
96 and <varname>mask</> is known to be of type <type>(unsigned-byte
97 w)</>, where <varname>w</> is a "good" width, all intermediate results
98 will be cut to <varname>w</> bits (but it is not done for variables
99 and constants!). This often results in an ability to use simple
100 machine instructions for the functions.
101 </para>
102
103 <para>
104 Consider an example.
105 <programlisting>
106 (defun i (x y)
107   (declare (type (unsigned-byte 32) x y))
108   (ldb (byte 32 0) (logxor x (lognot y))))
109 </programlisting>
110 The result of <literal>(lognot y)</> will be negative and of
111 type <type>(signed-byte 33)</>, so a naive implementation on a 32-bit
112 platform is unable to use 32-bit arithmetic here. But modular
113 arithmetic optimizer is able to do it: because the result is cut down
114 to 32 bits, the compiler will replace <function>logxor</>
115 and <function>lognot</> with versions cutting results to 32 bits, and
116 because terminals (here---expressions <literal>x</> and <literal>y</>)
117 are also of type <type>(unsigned-byte 32)</>, 32-bit machine
118 arithmetic can be used.
119 </para>
120
121 <note><para> As of &SBCL; 0.8.5 "good" functions
122 are <function>+</>, <function>-</>; <function>logand</>, <function>logior</>,
123 <function>logxor</>, <function>lognot</> and their combinations;
124 and <function>ash</> with the positive second argument. "Good" widths
125 are 32 on HPPA, MIPS, PPC, Sparc and X86 and 64 on Alpha. While it is
126 possible to support smaller widths as well, currently it is not
127 implemented.
128 </para></note>
129
130 </sect1>
131
132 </chapter>