MySQL 8.0.40
Source Code Documentation
grow_calculator.h
Go to the documentation of this file.
1/* Copyright (c) 2023, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24/// @addtogroup Replication
25/// @{
26/// @file grow_calculator.h
27
28#ifndef MYSQL_BUFFER_GROW_CALCULATOR_H_
29#define MYSQL_BUFFER_GROW_CALCULATOR_H_
30
31#include "libbinlogevents/include/buffer/grow_constraint.h" // Grow_constraint
32
33#include <algorithm> // std::min
34#include <limits> // std::numeric_limits
35#include <string> // std::string
36#ifdef NDEBUG
37#include <sstream> // std::stringstream
38#endif
39
40namespace mysqlns::buffer {
41
42/// Description of a heuristic to determine how much memory to allocate.
43///
44/// This may be used in diverse contexts such as growing a memory
45/// buffer, or growing a pool of objects.
46///
47/// This encapsulates several common heuristics for growth:
48///
49/// - The growth rate can be exponential. This is useful for cases
50/// such as contiguous memory buffers, where each size increment may
51/// copy all the existing data, e.g., using 'realloc'. Or, more
52/// generally, any data structure where size growth has a cost that
53/// is linear in the total size. In such cases an exponential
54/// growth rate ensures that execution time is not quadratic in the
55/// number of grow operations.
56///
57/// - The growth rate can be linear. This is useful for cases such as
58/// linked lists, where each size increment is linear in the
59/// increment size.
60///
61/// - There can be an upper bound on the size. This is useful
62/// e.g. when there are configurable memory limits.
63///
64/// - The size can be specified to be a multiple of a given number.
65/// This can potentially be useful if there is a way to align
66/// allocated objects to page sizes, or similar.
68 public:
70 /// Return type for compute_new_size.
72 /// By default, limit memory to 1 GiB.
73 static constexpr Size_t default_max_size =
74 Size_t(1024) * Size_t(1024) * Size_t(1024);
75 /// By default, double the size in each allocation.
76 static constexpr double default_grow_factor = 2.0;
77 /// By default, allocate at least 1 KiB more in each call.
78 static constexpr Size_t default_grow_increment = 1024;
79 /// By default, allocate multiples of 1 KiB.
80 static constexpr Size_t default_block_size = 1024;
81
83
84 /// Compute the new size.
85 ///
86 /// This follows the following rules:
87 ///
88 /// - It returns exceeds_max_size if the requested size, or the
89 /// existing size, exceeds the configured max size.
90 ///
91 /// - It never shrinks. If the request is smaller than the existing
92 /// size, it just returns the existing size.
93 ///
94 /// - It multiplies the old size by the grow_factor, and if needed
95 /// increments the size further until it has grown by the
96 /// grow_increment, and then rounds up to the nearest multiple of
97 /// the block_size. If the result of these operations exceeds the
98 /// max size, the result is reduced to the max size.
99 ///
100 /// @param old_size The existing size.
101 ///
102 /// @param requested_size The total size needed.
103 ///
104 /// @retval A pair. The first component is `bool` and contains the
105 /// error status: `false` means success, i.e., the requested size
106 /// does not exceed the maximum size. It also counts as success if
107 /// the request is less than the existing size, or if the request is
108 /// zero. `true` means error, i.e., the requested size exceeds the
109 /// maximum size. The second component is the new size. If the
110 /// first component is `true` for error, the second component is
111 /// zero.
112 ///
113 /// @retval other value The new size.
114 Result_t compute_new_size(Size_t old_size, Size_t requested_size) const;
115};
116
117} // namespace mysqlns::buffer
118
119/// @} (end of group Replication)
120
121#endif /* MYSQL_BUFFER_GROW_CALCULATOR_H_ */
Description of a heuristic to determine how much memory to allocate.
Definition: grow_calculator.h:67
static constexpr Size_t default_block_size
By default, allocate multiples of 1 KiB.
Definition: grow_calculator.h:80
Grow_calculator()
Definition: grow_calculator.cpp:32
static constexpr Size_t default_grow_increment
By default, allocate at least 1 KiB more in each call.
Definition: grow_calculator.h:78
std::size_t Size_t
Definition: grow_constraint.h:69
static constexpr double default_grow_factor
By default, double the size in each allocation.
Definition: grow_calculator.h:76
static constexpr Size_t default_max_size
By default, limit memory to 1 GiB.
Definition: grow_calculator.h:73
Result_t compute_new_size(Size_t old_size, Size_t requested_size) const
Compute the new size.
Definition: grow_calculator.cpp:39
Description of a heuristic to determine how much memory to allocate.
Definition: grow_constraint.h:67
std::size_t Size_t
Definition: grow_constraint.h:69
std::pair< bool, Size_t > Result_t
Return type for compute_new_size.
Definition: grow_constraint.h:71
Definition: buffer_sequence_view.h:51