BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslstl_function_smallobjectoptimization.h
Go to the documentation of this file.
1/// @file bslstl_function_smallobjectoptimization.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslstl_function_smallobjectoptimization.h -*-C++-*-
8#ifndef INCLUDED_BSLSTL_FUNCTION_SMALLOBJECTOPTIMIZATION
9#define INCLUDED_BSLSTL_FUNCTION_SMALLOBJECTOPTIMIZATION
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslstl_function_smallobjectoptimization bslstl_function_smallobjectoptimization
15/// @brief Provide small-object optimization buffer for `bsl::function`.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslstl
19/// @{
20/// @addtogroup bslstl_function_smallobjectoptimization
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslstl_function_smallobjectoptimization-purpose"> Purpose</a>
25/// * <a href="#bslstl_function_smallobjectoptimization-classes"> Classes </a>
26/// * <a href="#bslstl_function_smallobjectoptimization-description"> Description </a>
27///
28/// # Purpose {#bslstl_function_smallobjectoptimization-purpose}
29/// Provide small-object optimization buffer for `bsl::function`.
30///
31/// # Classes {#bslstl_function_smallobjectoptimization-classes}
32///
33/// - bslstl::Function_SmallObjectOptimization: impl utils for `bsl::function`.
34///
35/// @see bslstl_function
36///
37/// # Description {#bslstl_function_smallobjectoptimization-description}
38/// This private, subordinate component provides a utility
39/// `struct`, `bslstl::Function_SmallObjectOptimization`, that provides a suite
40/// of types, type traits, and constants used in the implementation of
41/// `bsl::function`, and its "small-object optimization" in particular.
42/// @}
43/** @} */
44/** @} */
45
46/** @addtogroup bsl
47 * @{
48 */
49/** @addtogroup bslstl
50 * @{
51 */
52/** @addtogroup bslstl_function_smallobjectoptimization
53 * @{
54 */
55
56#include <bslscm_version.h>
57
61
62#include <bsls_alignmentutil.h>
63
64#include <cstddef> // 'std::size_t'
65
66
67namespace bslstl {
68
69 // ======================================
70 // class Function_SmallObjectOptimization
71 // ======================================
72
73/// This utility `struct` provides a namespace for several types, type
74/// traits, and constants used in the implementation of the small-object
75/// optimization for `bsl::function`.
76///
77/// See @ref bslstl_function_smallobjectoptimization
79
80 // PRIVATE TYPES
81
82 /// `Dummy` is an incomplete type that this class uses to declare
83 /// pointers to member functions and pointers to member data.
84 class Dummy;
85
86 /// `MaxAlignedType` is an alias to a type that has the maximum
87 /// alignment, and is not over-aligned, for the current platform.
88 typedef bsls::AlignmentUtil::MaxAlignedType MaxAlignedType;
89
90 public:
91 // TYPES
92
93 /// This `union` defines the storage area for a functor representation.
94 /// The design uses the small-object optimization in an attempt to avoid
95 /// allocations for objects that are no larger than `InplaceBuffer`.
96 /// When the target object is no larger than `InplaceBuffer`, the
97 /// small-object optimization can be used by storing the target directly
98 /// within the `InplaceBuffer`.
99 ///
100 ///
101 /// \note Note that union members other than `d_object_p` are just fillers to
102 /// make sure that a function or member function pointer can fit without
103 /// allocation, and that `InplaceBuffer` has maximum alignment. The
104 /// `d_minbuf` member ensures that `InplaceBuffer` is large enough to
105 /// hold modestly complex functors, like `bdlf::Bind` objects and other
106 /// functors that store embedded arguments, eliminating the need to
107 /// allocate memory from the heap for the footprint of such objects.
108 ///
109 /// The size of this type ensures that the inplace buffer will be 6
110 /// pointers in size, and the total footprint of a `bsl::function`
111 /// object on most platforms will be 10 pointers, which matches the
112 /// sizes of previous implementations of `bdef_Function`.
114
115 // PUBLIC DATA
116 void *d_object_p; // pointer to external rep
117 void (*d_func_p)(); // pointer to function
118 void (Dummy::*d_memFunc_p)(); // pointer to member function
119 int Dummy::*d_memData_p; // pointer to member data
120 MaxAlignedType d_align; // force alignment
121 void *d_minbuf[6]; // force minimum size
122 };
123
124 // CONSTANTS
125
126 /// `SooFuncSize` (below) adds this value to the size of a small
127 /// stateful functor to indicate that, despite being small, it should
128 /// not be allocated inplace using the small object optimization (SOO),
129 /// i.e., because it does not have a nothrow move constructor and
130 /// cannot, therefore, be swapped safely. When a size larger than this
131 /// constant is seen, the actual object size can be determined by
132 /// subtracting this constant. A useful quality of this encoding is
133 /// that if `SZ <= sizeof(InplaceBuffer)` for some object size `SZ`,
134 /// then `SZ + k_NON_SOO_SMALL_SIZE > sizeof(InplaceBuffer)`, thus
135 /// `SooFuncSize` (below) for any object that should not be allocated
136 /// inplace is larger than `sizeof(InplaceBuffer)`, and the
137 /// `SooFuncSize` for any object that *should* be allocated inplace is
138 /// smaller than or equal to `sizeof(InplaceBuffer)`, making the test for "is inplace function" simple.
139 ///
140 /// \note Note that it is assumed that no
141 /// actual object has a size larger than this constant.
142 static const std::size_t k_NON_SOO_SMALL_SIZE = ~sizeof(InplaceBuffer);
143
144 // TYPES
145
146 /// This class template provides a metafunction that `bsl::function`
147 /// uses to determine the size of an object, and whether to store it
148 /// using the small-object optimization (SOO) or not. The `value`
149 /// member of this class encodes the size of the specified `TP` type as
150 /// follows:
151 ///
152 /// 1. If `TP` is larger than `InplaceBuffer`, then
153 /// `value == sizeof(TP)`.
154 /// 2. Otherwise, if `TP` has a non-throwing destructive move (i.e.,
155 /// it is bitwise movable or has a `noexcept` move constructor),
156 /// then `value == sizeof(TP)`.
157 /// 3. Otherwise, `value == sizeof(TP) + k_NON_SOO_SMALL_SIZE`. This
158 /// encoding indicates that move might throw and, therefore, `TP`
159 /// should not be allocated in place even though it would fit in the
160 /// footprint of an `InplaceBuffer`.
161 ///
162 ///
163 /// \note Note that the `Soo` prefix is used to indicate that an identifier
164 /// uses the above protocol. Thus, a variable called `SooSize` is
165 /// assumed to be encoded as above, whereas a variable called `size`
166 /// can generally be assumed not to be encoded that way.
167 ///
168 /// See @ref bslstl_function_smallobjectoptimization
169 template <class TP>
171
172 // PRIVATE CONSTANTS
173
174 // This constant is `k_NON_SOO_SMALL_SIZE` if `TP` is small enough
175 // to fit within the footprint of `InplaceBuffer` but should not be
176 // placed there because it is neither bitwise movable nor
177 // nothrow-move constructible; otherwise this constant is zero.
178 static const std::size_t k_SOO_ENCODING_OFFSET =
179 sizeof(TP) > sizeof(InplaceBuffer) ? 0 :
183
184 public:
185 // CONSTANTS
186
187 /// `value` encodes the size of the `TP` type using the algorithm
188 /// defined in the documentation for this class.
189 static const std::size_t value = sizeof(TP) + k_SOO_ENCODING_OFFSET;
190 };
191
192 // TYPES
193
194 /// This class is a Boolean metafunction that determines whether or not
195 /// the specified `FN` template parameter should be allocated within the
196 /// footprint of the `InplaceBuffer` (i.e., using the small-object
197 /// optimization.)
198 ///
199 /// # Implementation Note
200 /// In the future, `InplaceFunc` should also consider the alignment of
201 /// `FN` in its determination of whether or not `bsl::function` will use
202 /// the small-object optimization. However, `bsl::function` currently
203 /// has no way to specify alignment when it allocates memory.
204 template <class FN>
205 struct IsInplaceFunc
207 SooFuncSize<FN>::value <= sizeof(InplaceBuffer)> {
208 };
209};
210
211// ============================================================================
212// INLINE DEFINITIONS
213// ============================================================================
214
215 // ---------------------------------------------------
216 // class Function_SmallObjectOptimization::SooFuncSize
217 // ---------------------------------------------------
218
219// PRIVATE CONSTANTS
220template <class TP>
221const std::size_t
222 Function_SmallObjectOptimization::SooFuncSize<TP>::k_SOO_ENCODING_OFFSET;
223
224// CONSTANTS
225template <class TP>
226const std::size_t Function_SmallObjectOptimization::SooFuncSize<TP>::value;
227
228} // close package namespace
229
230
231#endif
232
233// ----------------------------------------------------------------------------
234// Copyright 2020 Bloomberg Finance L.P.
235//
236// Licensed under the Apache License, Version 2.0 (the "License");
237// you may not use this file except in compliance with the License.
238// You may obtain a copy of the License at
239//
240// http://www.apache.org/licenses/LICENSE-2.0
241//
242// Unless required by applicable law or agreed to in writing, software
243// distributed under the License is distributed on an "AS IS" BASIS,
244// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
245// See the License for the specific language governing permissions and
246// limitations under the License.
247// ----------------------------- END-OF-FILE ----------------------------------
248
249/** @} */
250/** @} */
251/** @} */
Definition bslstl_function_smallobjectoptimization.h:170
static const std::size_t value
Definition bslstl_function_smallobjectoptimization.h:189
Definition bslstl_function_smallobjectoptimization.h:78
static const std::size_t k_NON_SOO_SMALL_SIZE
Definition bslstl_function_smallobjectoptimization.h:142
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bslstl_algorithm.h:84
Definition bslmf_integralconstant.h:261
Definition bslmf_isnothrowmoveconstructible.h:361
Definition bslmf_isbitwisemoveable.h:718
AlignmentToType< BSLS_MAX_ALIGNMENT >::Type MaxAlignedType
Definition bsls_alignmentutil.h:307
Definition bslstl_function_smallobjectoptimization.h:113
int Dummy::* d_memData_p
Definition bslstl_function_smallobjectoptimization.h:119
void * d_minbuf[6]
Definition bslstl_function_smallobjectoptimization.h:121
void * d_object_p
Definition bslstl_function_smallobjectoptimization.h:116
void(* d_func_p)()
Definition bslstl_function_smallobjectoptimization.h:117
void(Dummy::* d_memFunc_p)()
Definition bslstl_function_smallobjectoptimization.h:118
MaxAlignedType d_align
Definition bslstl_function_smallobjectoptimization.h:120