1 | <html> |
---|
2 | <head> |
---|
3 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
---|
4 | <title>Miscellaneous Notes</title> |
---|
5 | <link rel="stylesheet" href="../boostbook.css" type="text/css"> |
---|
6 | <meta name="generator" content="DocBook XSL Stylesheets V1.69.1"> |
---|
7 | <link rel="start" href="../index.html" title="The Boost C++ Libraries"> |
---|
8 | <link rel="up" href="../function.html" title="Chapter 4. Boost.Function"> |
---|
9 | <link rel="prev" href="faq.html" title="Frequently Asked Questions"> |
---|
10 | <link rel="next" href="testsuite.html" title="Testsuite"> |
---|
11 | </head> |
---|
12 | <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> |
---|
13 | <table cellpadding="2" width="100%"> |
---|
14 | <td valign="top"><img alt="boost.png (6897 bytes)" width="277" height="86" src="../../../boost.png"></td> |
---|
15 | <td align="center"><a href="../../../index.htm">Home</a></td> |
---|
16 | <td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td> |
---|
17 | <td align="center"><a href="../../../people/people.htm">People</a></td> |
---|
18 | <td align="center"><a href="../../../more/faq.htm">FAQ</a></td> |
---|
19 | <td align="center"><a href="../../../more/index.htm">More</a></td> |
---|
20 | </table> |
---|
21 | <hr> |
---|
22 | <div class="spirit-nav"> |
---|
23 | <a accesskey="p" href="faq.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../function.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="testsuite.html"><img src="../images/next.png" alt="Next"></a> |
---|
24 | </div> |
---|
25 | <div class="section" lang="en"> |
---|
26 | <div class="titlepage"><div><div><h3 class="title"> |
---|
27 | <a name="function.misc"></a>Miscellaneous Notes</h3></div></div></div> |
---|
28 | <div class="toc"><dl> |
---|
29 | <dt><span class="section"><a href="misc.html#id2699572">Boost.Function vs. Function Pointers</a></span></dt> |
---|
30 | <dt><span class="section"><a href="misc.html#id2699622">Performance</a></span></dt> |
---|
31 | <dt><span class="section"><a href="misc.html#id2699664">Combatting virtual function "bloat"</a></span></dt> |
---|
32 | <dt><span class="section"><a href="misc.html#id2699713">Acknowledgements</a></span></dt> |
---|
33 | </dl></div> |
---|
34 | <div class="section" lang="en"> |
---|
35 | <div class="titlepage"><div><div><h4 class="title"> |
---|
36 | <a name="id2699572"></a>Boost.Function vs. Function Pointers</h4></div></div></div> |
---|
37 | <p>Boost.Function has several advantages over function pointers, namely: |
---|
38 | |
---|
39 | </p> |
---|
40 | <div class="itemizedlist"><ul type="disc" compact> |
---|
41 | <li><p>Boost.Function allows arbitrary compatible function objects to be targets (instead of requiring an exact function signature).</p></li> |
---|
42 | <li><p>Boost.Function may be used with argument-binding and other function object construction libraries.</p></li> |
---|
43 | <li><p>Boost.Function has predictible behavior when an empty function object is called. </p></li> |
---|
44 | </ul></div> |
---|
45 | <p> And, of course, function pointers have several advantages over Boost.Function: |
---|
46 | |
---|
47 | </p> |
---|
48 | <div class="itemizedlist"><ul type="disc" compact> |
---|
49 | <li><p> Function pointers are smaller (the size of one pointer instead of three) </p></li> |
---|
50 | <li><p> Function pointers are faster (Boost.Function may require two calls through function pointers) </p></li> |
---|
51 | <li><p> Function pointers are backward-compatible with C libraries.</p></li> |
---|
52 | <li><p> More readable error messages. </p></li> |
---|
53 | </ul></div> |
---|
54 | </div> |
---|
55 | <div class="section" lang="en"> |
---|
56 | <div class="titlepage"><div><div><h4 class="title"> |
---|
57 | <a name="id2699622"></a>Performance</h4></div></div></div> |
---|
58 | <div class="toc"><dl> |
---|
59 | <dt><span class="section"><a href="misc.html#id2699625">Function object wrapper size</a></span></dt> |
---|
60 | <dt><span class="section"><a href="misc.html#id2699636">Copying efficiency</a></span></dt> |
---|
61 | <dt><span class="section"><a href="misc.html#id2699653">Invocation efficiency</a></span></dt> |
---|
62 | </dl></div> |
---|
63 | <div class="section" lang="en"> |
---|
64 | <div class="titlepage"><div><div><h5 class="title"> |
---|
65 | <a name="id2699625"></a>Function object wrapper size</h5></div></div></div> |
---|
66 | <p> Function object wrappers will be the size of two function pointers plus one function pointer or data pointer (whichever is larger). On common 32-bit platforms, this amounts to 12 bytes per wrapper. Additionally, the function object target will be allocated on the heap.</p> |
---|
67 | </div> |
---|
68 | <div class="section" lang="en"> |
---|
69 | <div class="titlepage"><div><div><h5 class="title"> |
---|
70 | <a name="id2699636"></a>Copying efficiency</h5></div></div></div> |
---|
71 | <p> Copying function object wrappers may require allocating memory for a copy of the function object target. The default allocator may be replaced with a faster custom allocator or one may choose to allow the function object wrappers to only store function object targets by reference (using <code class="computeroutput">ref</code>) if the cost of this cloning becomes prohibitive.</p> |
---|
72 | </div> |
---|
73 | <div class="section" lang="en"> |
---|
74 | <div class="titlepage"><div><div><h5 class="title"> |
---|
75 | <a name="id2699653"></a>Invocation efficiency</h5></div></div></div> |
---|
76 | <p> With a properly inlining compiler, an invocation of a function object requires one call through a function pointer. If the call is to a free function pointer, an additional call must be made to that function pointer (unless the compiler has very powerful interprocedural analysis).</p> |
---|
77 | </div> |
---|
78 | </div> |
---|
79 | <div class="section" lang="en"> |
---|
80 | <div class="titlepage"><div><div><h4 class="title"> |
---|
81 | <a name="id2699664"></a>Combatting virtual function "bloat"</h4></div></div></div> |
---|
82 | <p> The use of virtual functions tends to cause 'code bloat' on many compilers. When a class contains a virtual function, it is necessary to emit an additional function that classifies the type of the object. It has been our experience that these auxiliary functions increase the size of the executable significantly when many <code class="computeroutput">boost::function</code> objects are used. </p> |
---|
83 | <p> In Boost.Function, an alternative but equivalent approach was taken using free functions instead of virtual functions. The Boost.Function object essentially holds two pointers to make a valid target call: a void pointer to the function object it contains and a void pointer to an "invoker" that can call the function object, given the function pointer. This invoker function performs the argument and return value conversions Boost.Function provides. A third pointer points to a free function called the "manager", which handles the cloning and destruction of function objects. The scheme is typesafe because the only functions that actually handle the function object, the invoker and the manager, are instantiated given the type of the function object, so they can safely cast the incoming void pointer (the function object pointer) to the appropriate type.</p> |
---|
84 | </div> |
---|
85 | <div class="section" lang="en"> |
---|
86 | <div class="titlepage"><div><div><h4 class="title"> |
---|
87 | <a name="id2699713"></a>Acknowledgements</h4></div></div></div> |
---|
88 | <p> Many people were involved in the construction of this |
---|
89 | library. William Kempf, Jesse Jones and Karl Nelson were all |
---|
90 | extremely helpful in isolating an interface and scope for the |
---|
91 | library. John Maddock managed the formal review, and many |
---|
92 | reviewers gave excellent comments on interface, implementation, |
---|
93 | and documentation. Peter Dimov led us to the function |
---|
94 | declarator-based syntax.</p> |
---|
95 | </div> |
---|
96 | </div> |
---|
97 | <table width="100%"><tr> |
---|
98 | <td align="left"><small><p>Last revised: March 12, 2003 at 23:27:22 GMT</p></small></td> |
---|
99 | <td align="right"><small>Copyright © 2001-2004 Douglas Gregor</small></td> |
---|
100 | </tr></table> |
---|
101 | <hr> |
---|
102 | <div class="spirit-nav"> |
---|
103 | <a accesskey="p" href="faq.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../function.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="testsuite.html"><img src="../images/next.png" alt="Next"></a> |
---|
104 | </div> |
---|
105 | </body> |
---|
106 | </html> |
---|