-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmallObjectOptimization.cpp
More file actions
76 lines (68 loc) · 1.89 KB
/
SmallObjectOptimization.cpp
File metadata and controls
76 lines (68 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* \file SmallObjectOptimization.cpp
* \brief Small Object Optimization (aka small buffer optimization)
*
* Small Object Optimization means that the std::string object has a small buffer for small strings,
* which saves dynamic allocations.
*
* \see https://shaharmike.com/cpp/std-string/
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//--------------------------------------------------------------------------------------------------
// replace operator new and delete to log allocations
void *
operator new(std::size_t n)
{
std::cout << "[Allocating " << n << " bytes]";
return malloc(n);
}
//--------------------------------------------------------------------------------------------------
void
operator delete(void *p) throw()
{
free(p);
}
//--------------------------------------------------------------------------------------------------
void
operator delete(void *p, std::size_t) throw()
{
free(p);
}
//--------------------------------------------------------------------------------------------------
int main(int, char **)
{
for (std::size_t i = 0; i < 24; ++i) {
std::cout << i << ": " << std::string(i, '=') << std::endl;
}
return EXIT_SUCCESS;
}
//--------------------------------------------------------------------------------------------------
#if OUTPUT
// GCC
0:
1: =
2: ==
3: ===
4: ====
5: =====
6: ======
7: =======
8: ========
9: =========
10: ==========
11: ===========
12: ============
13: =============
14: ==============
15: ===============
16: [Allocating 17 bytes]================
17: [Allocating 18 bytes]=================
18: [Allocating 19 bytes]==================
19: [Allocating 20 bytes]===================
20: [Allocating 21 bytes]====================
21: [Allocating 22 bytes]=====================
22: [Allocating 23 bytes]======================
23: [Allocating 24 bytes]=======================
#endif