BDE 4.14.0 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 /// Note that union members other than `d_object_p` are just fillers to
101 /// make sure that a function or member function pointer can fit without
102 /// allocation, and that `InplaceBuffer` has maximum alignment. The
103 /// `d_minbuf` member ensures that `InplaceBuffer` is large enough to
104 /// hold modestly complex functors, like `bdlf::Bind` objects and other
105 /// functors that store embedded arguments, eliminating the need to
106 /// allocate memory from the heap for the footprint of such objects.
107 ///
108 /// The size of this type ensures that the inplace buffer will be 6
109 /// pointers in size, and the total footprint of a `bsl::function`
110 /// object on most platforms will be 10 pointers, which matches the
111 /// sizes of previous implementations of `bdef_Function`.
113
114 // PUBLIC DATA
115 void *d_object_p; // pointer to external rep
116 void (*d_func_p)(); // pointer to function
117 void (Dummy::*d_memFunc_p)(); // pointer to member function
118 int Dummy::*d_memData_p; // pointer to member data
119 MaxAlignedType d_align; // force alignment
120 void *d_minbuf[6]; // force minimum size
121 };
122
123 // CONSTANTS
124
125 /// `SooFuncSize` (below) adds this value to the size of a small
126 /// stateful functor to indicate that, despite being small, it should
127 /// not be allocated inplace using the small object optimization (SOO),
128 /// i.e., because it does not have a nothrow move constructor and
129 /// cannot, therefore, be swapped safely. When a size larger than this
130 /// constant is seen, the actual object size can be determined by
131 /// subtracting this constant. A useful quality of this encoding is
132 /// that if `SZ <= sizeof(InplaceBuffer)` for some object size `SZ`,
133 /// then `SZ + k_NON_SOO_SMALL_SIZE > sizeof(InplaceBuffer)`, thus
134 /// `SooFuncSize` (below) for any object that should not be allocated
135 /// inplace is larger than `sizeof(InplaceBuffer)`, and the
136 /// `SooFuncSize` for any object that *should* be allocated inplace is
137 /// smaller than or equal to `sizeof(InplaceBuffer)`, making the test
138 /// for "is inplace function" simple. Note that it is assumed that no
139 /// actual object has a size larger than this constant.
140 static const std::size_t k_NON_SOO_SMALL_SIZE = ~sizeof(InplaceBuffer);
141
142 // TYPES
143
144 /// This class template provides a metafunction that `bsl::function`
145 /// uses to determine the size of an object, and whether to store it
146 /// using the small-object optimization (SOO) or not. The `value`
147 /// member of this class encodes the size of the specified `TP` type as
148 /// follows:
149 ///
150 /// 1. If `TP` is larger than `InplaceBuffer`, then
151 /// `value == sizeof(TP)`.
152 /// 2. Otherwise, if `TP` has a non-throwing destructive move (i.e.,
153 /// it is bitwise movable or has a `noexcept` move constructor),
154 /// then `value == sizeof(TP)`.
155 /// 3. Otherwise, `value == sizeof(TP) + k_NON_SOO_SMALL_SIZE`. This
156 /// encoding indicates that move might throw and, therefore, `TP`
157 /// should not be allocated in place even though it would fit in the
158 /// footprint of an `InplaceBuffer`.
159 ///
160 /// Note that the `Soo` prefix is used to indicate that an identifier
161 /// uses the above protocol. Thus, a variable called `SooSize` is
162 /// assumed to be encoded as above, whereas a variable called `size`
163 /// can generally be assumed not to be encoded that way.
164 ///
165 /// See @ref bslstl_function_smallobjectoptimization
166 template <class TP>
168
169 // PRIVATE CONSTANTS
170
171 // This constant is `k_NON_SOO_SMALL_SIZE` if `TP` is small enough
172 // to fit within the footprint of `InplaceBuffer` but should not be
173 // placed there because it is neither bitwise movable nor
174 // nothrow-move constructible; otherwise this constant is zero.
175 static const std::size_t k_SOO_ENCODING_OFFSET =
176 sizeof(TP) > sizeof(InplaceBuffer) ? 0 :
180
181 public:
182 // CONSTANTS
183
184 /// `value` encodes the size of the `TP` type using the algorithm
185 /// defined in the documentation for this class.
186 static const std::size_t value = sizeof(TP) + k_SOO_ENCODING_OFFSET;
187 };
188
189 // TYPES
190
191 /// This class is a Boolean metafunction that determines whether or not
192 /// the specified `FN` template parameter should be allocated within the
193 /// footprint of the `InplaceBuffer` (i.e., using the small-object
194 /// optimization.)
195 ///
196 /// # Implementation Note
197 /// In the future, `InplaceFunc` should also consider the alignment of
198 /// `FN` in its determination of whether or not `bsl::function` will use
199 /// the small-object optimization. However, `bsl::function` currently
200 /// has no way to specify alignment when it allocates memory.
201 template <class FN>
202 struct IsInplaceFunc
204 SooFuncSize<FN>::value <= sizeof(InplaceBuffer)> {
205 };
206};
207
208// ============================================================================
209// INLINE DEFINITIONS
210// ============================================================================
211
212 // ---------------------------------------------------
213 // class Function_SmallObjectOptimization::SooFuncSize
214 // ---------------------------------------------------
215
216// PRIVATE CONSTANTS
217template <class TP>
218const std::size_t
219 Function_SmallObjectOptimization::SooFuncSize<TP>::k_SOO_ENCODING_OFFSET;
220
221// CONSTANTS
222template <class TP>
223const std::size_t Function_SmallObjectOptimization::SooFuncSize<TP>::value;
224
225} // close package namespace
226
227
228#endif
229
230// ----------------------------------------------------------------------------
231// Copyright 2020 Bloomberg Finance L.P.
232//
233// Licensed under the Apache License, Version 2.0 (the "License");
234// you may not use this file except in compliance with the License.
235// You may obtain a copy of the License at
236//
237// http://www.apache.org/licenses/LICENSE-2.0
238//
239// Unless required by applicable law or agreed to in writing, software
240// distributed under the License is distributed on an "AS IS" BASIS,
241// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
242// See the License for the specific language governing permissions and
243// limitations under the License.
244// ----------------------------- END-OF-FILE ----------------------------------
245
246/** @} */
247/** @} */
248/** @} */
Definition bslstl_function_smallobjectoptimization.h:167
static const std::size_t value
Definition bslstl_function_smallobjectoptimization.h:186
Definition bslstl_function_smallobjectoptimization.h:78
static const std::size_t k_NON_SOO_SMALL_SIZE
Definition bslstl_function_smallobjectoptimization.h:140
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bslstl_algorithm.h:82
Definition bslmf_integralconstant.h:244
Definition bslmf_isnothrowmoveconstructible.h:358
Definition bslmf_isbitwisemoveable.h:718
AlignmentToType< BSLS_MAX_ALIGNMENT >::Type MaxAlignedType
Definition bsls_alignmentutil.h:282
Definition bslstl_function_smallobjectoptimization.h:112
int Dummy::* d_memData_p
Definition bslstl_function_smallobjectoptimization.h:118
void * d_minbuf[6]
Definition bslstl_function_smallobjectoptimization.h:120
void * d_object_p
Definition bslstl_function_smallobjectoptimization.h:115
void(* d_func_p)()
Definition bslstl_function_smallobjectoptimization.h:116
void(Dummy::* d_memFunc_p)()
Definition bslstl_function_smallobjectoptimization.h:117
MaxAlignedType d_align
Definition bslstl_function_smallobjectoptimization.h:119