MySQL 8.4.0
Source Code Documentation
mysql::binlog::event::compression::buffer::Grow_calculator Class Reference

Description of a heuristic to determine how much memory to allocate. More...

#include <grow_calculator.h>

Inheritance diagram for mysql::binlog::event::compression::buffer::Grow_calculator:
[legend]

Public Types

using Size_t = std::size_t
 
using Result_t = std::pair< bool, Size_t >
 Return type for compute_new_size. More...
 
- Public Types inherited from mysql::binlog::event::compression::buffer::Grow_constraint
using Size_t = std::size_t
 
using Result_t = std::pair< bool, Size_t >
 Return type for compute_new_size. More...
 

Public Member Functions

 Grow_calculator ()
 
Result_t compute_new_size (Size_t old_size, Size_t requested_size) const
 Compute the new size. More...
 
- Public Member Functions inherited from mysql::binlog::event::compression::buffer::Grow_constraint
 Grow_constraint ()=default
 
 Grow_constraint (const Grow_constraint &other)=default
 
 Grow_constraint (Grow_constraint &&other)=default
 
Grow_constraintoperator= (const Grow_constraint &other)=default
 
Grow_constraintoperator= (Grow_constraint &&other)=default
 
virtual ~Grow_constraint ()=default
 
void set_max_size (Size_t max_size)
 Set the maximum size. More...
 
Size_t get_max_size () const
 
void set_grow_factor (double grow_factor)
 Set the grow factor. More...
 
double get_grow_factor () const
 
void set_grow_increment (Size_t grow_increment)
 Set the grow increment. More...
 
Size_t get_grow_increment () const
 
void set_block_size (Size_t block_size)
 Set the block size. More...
 
Size_t get_block_size () const
 
std::string debug_string () const
 In debug mode, return a string that describes the internal structure of this object, to use for debugging. More...
 
template<class T >
combine_with (const T &other) const
 Combine the constraints of this object with another Grow_constraint or Grow_calculator object. More...
 

Static Public Attributes

static constexpr Size_t default_max_size
 By default, limit memory to 1 GiB. More...
 
static constexpr double default_grow_factor = 2.0
 By default, double the size in each allocation. More...
 
static constexpr Size_t default_grow_increment = 1024
 By default, allocate at least 1 KiB more in each call. More...
 
static constexpr Size_t default_block_size = 1024
 By default, allocate multiples of 1 KiB. More...
 
- Static Public Attributes inherited from mysql::binlog::event::compression::buffer::Grow_constraint
static constexpr Size_t machine_max_size = std::numeric_limits<Size_t>::max()
 Maximum allowed value for the application max size. More...
 

Detailed Description

Description of a heuristic to determine how much memory to allocate.

This may be used in diverse contexts such as growing a memory buffer, or growing a pool of objects.

This encapsulates several common heuristics for growth:

  • The growth rate can be exponential. This is useful for cases such as contiguous memory buffers, where each size increment may copy all the existing data, e.g., using 'realloc'. Or, more generally, any data structure where size growth has a cost that is linear in the total size. In such cases an exponential growth rate ensures that execution time is not quadratic in the number of grow operations.
  • The growth rate can be linear. This is useful for cases such as linked lists, where each size increment is linear in the increment size.
  • There can be an upper bound on the size. This is useful e.g. when there are configurable memory limits.
  • The size can be specified to be a multiple of a given number. This can potentially be useful if there is a way to align allocated objects to page sizes, or similar.

Member Typedef Documentation

◆ Result_t

Return type for compute_new_size.

◆ Size_t

Constructor & Destructor Documentation

◆ Grow_calculator()

mysql::binlog::event::compression::buffer::Grow_calculator::Grow_calculator ( )

Member Function Documentation

◆ compute_new_size()

Grow_calculator::Result_t mysql::binlog::event::compression::buffer::Grow_calculator::compute_new_size ( Size_t  old_size,
Size_t  requested_size 
) const

Compute the new size.

This follows the following rules:

  • It returns exceeds_max_size if the requested size, or the existing size, exceeds the configured max size.
  • It never shrinks. If the request is smaller than the existing size, it just returns the existing size.
  • It multiplies the old size by the grow_factor, and if needed increments the size further until it has grown by the grow_increment, and then rounds up to the nearest multiple of the block_size. If the result of these operations exceeds the max size, the result is reduced to the max size.
Parameters
old_sizeThe existing size.
requested_sizeThe total size needed.
Return values
Apair. The first component is bool and contains the error status: false means success, i.e., the requested size does not exceed the maximum size. It also counts as success if the request is less than the existing size, or if the request is zero. true means error, i.e., the requested size exceeds the maximum size. The second component is the new size. If the first component is true for error, the second component is zero.
othervalue The new size.

Member Data Documentation

◆ default_block_size

constexpr Size_t mysql::binlog::event::compression::buffer::Grow_calculator::default_block_size = 1024
staticconstexpr

By default, allocate multiples of 1 KiB.

◆ default_grow_factor

constexpr double mysql::binlog::event::compression::buffer::Grow_calculator::default_grow_factor = 2.0
staticconstexpr

By default, double the size in each allocation.

◆ default_grow_increment

constexpr Size_t mysql::binlog::event::compression::buffer::Grow_calculator::default_grow_increment = 1024
staticconstexpr

By default, allocate at least 1 KiB more in each call.

◆ default_max_size

constexpr Size_t mysql::binlog::event::compression::buffer::Grow_calculator::default_max_size
staticconstexpr
Initial value:
=
Size_t(1024) * Size_t(1024) * Size_t(1024)
std::size_t Size_t
Definition: grow_constraint.h:68

By default, limit memory to 1 GiB.


The documentation for this class was generated from the following files: