]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/local_function/doc/alternatives.qbk
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / local_function / doc / alternatives.qbk
CommitLineData
7c673cae
FG
1
2[/ Copyright (C) 2009-2012 Lorenzo Caminiti ]
3[/ Distributed under the Boost Software License, Version 1.0 ]
4[/ (see accompanying file LICENSE_1_0.txt or a copy at ]
5[/ http://www.boost.org/LICENSE_1_0.txt) ]
6[/ Home at http://www.boost.org/libs/local_function ]
7
8[section:alternatives Annex: Alternatives]
9
10This section compares the features offered by this library with similar features offered by C++ and by other libraries.
11
12[heading Features]
13
14The following table compares local function features.
15
16[table
17[
18 [ Local Function Feature ]
19 [ Boost.LocalFunction ]
20 [ C++11 Lambda Function (Not C++03) ]
21 [ Local Functor ]
22 [ Global Functor (Not Local) ]
23 [ Boost.Phoenix ]
24]
25[
26 [ ['Can be defined locally] ]
27 [ Yes. ]
28 [ Yes. ]
29 [ Yes. ]
30 [ No.
31Therefore this not really an alternative implementation of local functions but it is listed here just for comparison. ]
32 [ Yes. ]
33]
34[
35 [ ['Can be defined using C++ statement syntax] ]
36 [ Yes.
37Plus eventual compiler errors and debugging retain their usual meaning and format. ]
38 [ Yes.
39Plus eventual compiler errors and debugging retain their usual meaning and format. ]
40 [ Yes.
41Plus eventual compiler errors and debugging retain their usual meaning and format. ]
42 [ Yes.
43Plus eventual compiler errors and debugging retain their usual meaning and format. ]
44 [ No (it uses C++ __expression_template__ syntax). ]
45]
46[
47 [ ['Can be defined within expressions] ]
48 [ No. It can be defined only within declarations. ]
49 [ Yes (plus the local function can be unnamed). ]
50 [ No. It can be defined only within declarations. ]
51 [ No. It can be defined only within declarations. ]
52 [ Yes (plus the local function can be unnamed). ]
53]
54[
55 [ ['Can be passed as template parameter (e.g., to STL algorithms)] ]
56 [ Yes.
57The __CXX03__ standard does not allow to pass local types as template parameters (see __N2657__) but this library implements a "trick" to get around this limitation (see the __Implementation__ section). ]
58 [ Yes. ]
59 [ No on __CXX03__ compilers (but yes on __CXX11__ compilers and some compilers like MSVC 8.0, see __N2657__). ]
60 [ Yes. ]
61 [ Yes. ]
62]
63[
64 [ ['Access variables in scope] ]
65 [ Yes.
66The variable names are repeated in the function declaration so they can be bound by value, by constant value, by reference, and by constant reference (the object `this` can also be bound using `this_`). ]
67 [ Yes.
68The variable names are repeated in the function declaration (plus there is a short-hand syntax to bind all variables in scope at once) so they can be bound by constant value and by reference (the object `this` can also be bound).
69However, variables cannot be bound by constant references (see below). ]
70 [ No.
71Programmers must manually program functor data members and explicitly specify their types to access variables in scope. ]
72 [ No.
73Programmers must manually program functor data members and explicitly specify their types to access variables in scope. ]
74 [ Yes.
75Variables in scope are accessible as usual within expressions (plus `boost::phoenix::let` can be used to bind variables by constant reference). ]
76]
77[
78 [ ['[@http://en.wikipedia.org/wiki/Type_polymorphism#Parametric_polymorphism Polymorphic] in the function parameter type] ]
79 [ No (local functions cannot be function templates). ]
80 [ No (__CXX11__ lambdas cannot be function templates). ]
81 [ No (local classes cannot have member function templates). ]
82 [ Yes. ]
83 [ Yes. ]
84]
85]
86
87[*C++11 Lambda Function]
88
89__CXX11_lambda_functions__ have most of the features of this library plus some additional feature (see also the example in the __Introduction__ section):
90
91* __CXX11_lambda_functions__ can be defined within expressions while this library local functions can only be defined at declaration scope.
92* __CXX11_lambda_functions__ are only supported by the __CXX11__ standard so they are not supported by all C++ compilers.
93This library local functions can be programmed also on __CXX03__ compilers (and they have performances comparable to __CXX11_lambda_functions__ on __CXX11__ compilers).
94* __CXX11_lambda_functions__ do not allow to bind variables in scope by constant reference.
95Because a variable cannot be bound by constant reference, __CXX11_lambda_functions__ can bind a variable by constant only if the variable is `CopyConstructible` and the binding requires a (potentially expensive) extra copy operation.
96Constant reference binding is instead supported by this library.
97* __CXX11_lambda_functions__ do not allow to bind data members selectively without binding also the object `this` while this library local functions can bind either selected data members or the entire object `this` (using `this_`).
98* __CXX11_lambda_functions__ provide a short-hand syntax to bind all variables in scope at once (`&` or `=`) while this library local function always require to bind variables naming them one-by-one.
99
100For example, for non-copyable objects (see also [@../../example/noncopyable_cxx11_lambda_error.cpp =noncopyable_cxx11_lambda_error.cpp=] and [@../../example/noncopyable_local_function.cpp =noncopyable_local_function.cpp=]):
101
102[table
103 [ [C++11 Lambda Function] [Boost.LocalFunction] ]
104 [ [[noncopyable_cxx11_lambda_error]] [[noncopyable_local_function]] ]
105]
106
107Or, for objects with expensive copy operations (see also [@../../example/expensive_copy_cxx11_lambda.cpp =expensive_copy_cxx11_lambda.cpp=] and [@../../example/expensive_copy_local_function.cpp =expensive_copy_local_function.cpp=]):
108
109[table
110 [ [C++11 Lambda Function] [Boost.LocalFunction] ]
111 [ [[expensive_copy_cxx11_lambda]] [[expensive_copy_local_function]] ]
112]
113
114When constant binding functionality is needed for __CXX11_lambda_functions__, the best alternative might be to bind an extra local variable declared constant and initialized to the original variable (for example, see /constant blocks/ implemented with __CXX11_lambda_functions__ in the __Examples__ section).
115
116[*Local Functor]
117
118The following example compares local functions with C++ local functors (see also [@../../example/add_local_functor.cpp =add_local_functor.cpp=] and [@../../test/add.cpp =add.cpp=]):
119
120[table
121 [ [Local Functor] [Boost.LocalFunction] ]
122 [ [[add_local_functor]] [[add]] ]
123]
124
125[*Global Functor]
126
127The following example compares local functions with C++ global functors (see also [@../../example/add_global_functor.cpp =add_global_functor.cpp=] and [@../../test/add.cpp =add.cpp=]):
128
129[table
130 [ [Global Functor] [Boost.LocalFunction] ]
131 [ [[add_global_functor]] [[add]] ]
132]
133
134However, note that global functors do not allow to define the function locally so they are not a real alternative implementation of local functions.
135
136[*Boost.Phoenix]
137
138The following example compares local functions with __Boost_Phoenix__ (see also [@../../example/add_phoenix.cpp =add_phoenix.cpp=] and [@../../test/add.cpp =add.cpp=]):
139
140[table
141 [ [Boost.Phoenix] [Boost.LocalFunction] ]
142 [ [[add_phoenix]] [[add]] ]
143]
144
145The comparison in this section does not include the __Boost_Lambda__ library because that library is obsolete and it was replaced by __Boost_Phoenix__.
146The __Boost_Phoenix__ library version 3.0 is used for this comparison.
147
148[heading Performances]
149
150The following tables compare run-times, compile-times, and binary sizes for the different alternatives to local functions presented in this section.
151
152Overall, this library has compile-times and generates binary sizes similar to the ones of the other approaches.
153This library run-times on __CXX03__ compilers were measured to be larger than other approaches when compiler optimization is enabled (using `bjam release ...`).
154However, on compilers that allow to pass local types as template parameters (e.g., MSVC 8.0 or GCC 4.5.3 with __CXX11__ features enabled [^-std=c++0x], see also __N2657__ and __Boost_Config__'s `BOOST_NO_CXX11_LOCAL_CLASS_TEMPLATE_PARAMETERS`) this library automatically generates optimized code that runs as fast as the fastest of the other approaches (see the "Boost.LocalFunction" approach below).
155When this library local function is specified `inline` (see the "Boost.LocalFunction Inline" approach below and the __Advanced_Topics__ section) its run-times are always comparable to both the "Local Functor" and "Global Functor" approaches.
156However, in these cases the local function cannot be portably passed as template parameter (see __N2657__ and __Boost_Config__'s `BOOST_NO_CXX11_LOCAL_CLASS_TEMPLATE_PARAMETERS`) so `std::for_each` is replaced by a for-loop (on MSVC the for-loop, and not the local function in fact the same applies to local functors, was measured to have worst performances than using `std::for_each`).
157Finally, this library run-times are always among the fastest when no compiler optimization is enabled (using `bjam debug ...`).
158
159[note
160The run-time performances of this library local functions are explained because on __CXX03__ compliant compilers (e.g., GCC 4.5.3 without [^-std=c++0x]) this library needs to use a function pointer in order to portably pass the local function class as a template parameter (see __N2657__ and the __Implementation__ section).
161For all tested compilers, this function pointer prevents the compiler optimization algorithms from inlining the local function calls.
162Instead, the functors used by other approaches (e.g., __Boost_Phoenix__) have been observed to allow all tested compilers to inline all the function calls for optimization.
163This run-time performance cost is not present on compilers that allow to pass local types as template parameters (e.g., MSVC 8.0 or GCC 4.5.3 with __CXX11__ features enabled [^-std=c++0x], see __Boost_Config__'s `BOOST_NO_CXX11_LOCAL_CLASS_TEMPLATE_PARAMETERS`) because this library does not have to use the extra function pointer to implement the local function call (it directly passes the local class type as template parameter).
164]
165
166This run-time performance cost on __CXX03__ compilers might or might not be an issue depending on the performance requirements of specific applications.
167For example, an application might already be using a number of indirect function calls (function pointers, virtual functions, etc) for which the overhead added by using the one extra function pointer required by the local function call might not be noticeable within the overall program run-time.
168
169Finally, note that only a very simple local function body with just a single instruction was used for the anaylsis presented here (see the source files below).
170The authors have not studied how this library and the other approaches will perform with respect to each other when a more complex set of instructions is programmed for the local function body (e.g., /if/ a more complex set of instructions in the local function body were to inhibit some compiler from inlining function objects also other approaches like __CXX11_lambda_functions__ and __Boost_Phoenix__ /could/ start to show higher run-times even when optimization is enabled).
171
172The following commands were executed from the library example directory to measure compile-time, binary size, and run-time respectively:
173
174[pre
175> touch <FILE_NAME>.cpp # force recompilation
176> python chrono.py bjam {release|debug} <FILE_NAME> # compile-time
177> size <FILE_NAME> # binary size
178> ./<FILE_NAME> # run-time
179]
180
181The local function was called =1e8= times to add together all the elements of a vector and the run-time was measured using __Boost_Chrono__ averaging over =10= executions of the vector summation (see the source files below).
182
183[table
184 [
185 [Legend]
186 [Approach]
187 [Source File]
188 ]
189 [
190 [[$../../example/profile_legend_local_function.png]]
191 [__Boost_LocalFunction__]
192 [[@../../example/profile_local_function.cpp =profile_local_function.cpp=]]
193 ]
194 [
195 [[$../../example/profile_legend_local_function_inline.png]]
196 [__Boost_LocalFunction__ inline]
197 [[@../../example/profile_local_function_inline.cpp =profile_local_function_inline.cpp=]]
198 ]
199 [
200 [[$../../example/profile_legend_cxx11_lambda.png]]
201 [__CXX11__ Lambda Function
202[footnote
203Measurements available only for __CXX11__ compilers.
204]
205 ]
206 [[@../../example/profile_cxx11_lambda.cpp =profile_cxx11_lambda.cpp=]]
207 ]
208 [
209 [[$../../example/profile_legend_local_functor.png]]
210 [Local Functor]
211 [[@../../example/profile_local_functor.cpp =profile_local_functor.cpp=]]
212 ]
213 [
214 [[$../../example/profile_legend_global_functor.png]]
215 [Global Functor]
216 [[@../../example/profile_global_functor.cpp =profile_global_functor.cpp=]]
217 ]
218 [
219 [[$../../example/profile_legend_phoenix.png]]
220 [__Boost_Phoenix__]
221 [[@../../example/profile_phoenix.cpp =profile_phoenix.cpp=]]
222 ]
223]
224
225[table
226 [ [GCC 4.5.3 With C++11 Lambda Functions and "Local Classes as Template Parameters" ([^bjam cxxflags=-std=c++0x ...])] ]
227 [ [
228 [*Compiled with =bjam release ...= for maximum optimization (=-O3 -finline-functions=)]
229 [$../../example/profile_gcc_cxx11_release.png [width 13in] [height 10in]]
230 ] ]
231 [ [
232 [*Compiled with =bjam debug ...= for no optimization (=-O0 -fno-inline=)]
233 [$../../example/profile_gcc_cxx11_debug.png [width 13in] [height 10in]]
234 ] ]
235]
236
237[table
238 [ [MSVC 8.0 With "Local Classes as Template Parameters" (Without C++11 Lambda Functions)] ]
239 [ [
240 [*Compiled with =bjam release ...= for maximum optimization (=/O2 /Ob2=)]
241 [$../../example/profile_msvc_release.png [width 13in] [height 10in]]
242 ] ]
243 [ [
244 [*Compiled with =bjam debug ...= for no optimization (=/Od /Ob0=)]
245 [$../../example/profile_msvc_debug.png [width 13in] [height 10in]]
246 ] ]
247]
248
249[table
250 [ [GCC 4.3.4 With __CXX03__ Only (Without __CXX11__ Lambda Functions and Without "Local Classes as Template Parameters")] ]
251 [ [
252 [*Compiled with =bjam release ...= for maximum optimization (=-O3 -finline-functions=)]
253 [$../../example/profile_gcc_release.png [width 13in] [height 10in]]
254 ] ]
255 [ [
256 [*Compiled with =bjam debug ...= for no optimization (=-O0 -fno-inline=)]
257 [$../../example/profile_gcc_debug.png [width 13in] [height 10in]]
258 ] ]
259]
260
261[endsect]
262