OILS / cpp / obj_layout_test.cc View on Github | oilshell.org

210 lines, 105 significant
1#include "_gen/core/runtime.asdl.h" // Cell, etc
2#include "_gen/frontend/syntax.asdl.h"
3#include "vendor/greatest.h"
4
5TEST sizeof_syntax() {
6 // 40 bytes (after merging with line_span January 2023)
7 // - Get rid of 'string val'
8 // - Replace 'int line_id' with SourceLine
9 // - Maybe recompute length on demand
10 log("sizeof(Token) = %d", sizeof(syntax_asdl::Token));
11 log("alignof(Token) = %d", alignof(syntax_asdl::Token));
12 log("alignof(Token*) = %d", alignof(syntax_asdl::Token *));
13 log("");
14
15 // 2024-03 - both of these are 64 bytes
16 log("sizeof(BracedVarSub) = %d", sizeof(syntax_asdl::BracedVarSub));
17 log("sizeof(command::Simple) = %d", sizeof(syntax_asdl::command::Simple));
18 log("");
19
20 // Only 8 bytes
21 log("sizeof(CompoundWord) = %d", sizeof(syntax_asdl::CompoundWord));
22
23 // Reordered to be 16 bytes
24 log("sizeof(runtime_asdl::Cell) = %d", sizeof(runtime_asdl::Cell));
25
26 // 24 bytes: std::vector
27 log("sizeof(List<int>) = %d", sizeof(List<int>));
28 log("sizeof(List<BigStr*>) = %d", sizeof(List<BigStr *>));
29
30 log("sizeof(Slab<int>) = %d", sizeof(Slab<int>));
31 log("sizeof(Slab<BigStr*>) = %d", sizeof(Slab<BigStr *>));
32 // Right after object header
33 log("kSlabHeaderSize = %d", kSlabHeaderSize);
34
35 // Unlike Python, this is -1, not 255!
36 int mod = -1 % 256;
37 log("mod = %d", mod);
38
39 log("alignof(bool) = %d", alignof(bool));
40 log("alignof(int) = %d", alignof(int));
41 log("alignof(float) = %d", alignof(float));
42
43 log("sizeof(BigStr) = %d", sizeof(BigStr));
44 log("alignof(BigStr) = %d", alignof(BigStr));
45
46 log("sizeof(BigStr*) = %d", sizeof(BigStr *));
47 log("alignof(BigStr*) = %d", alignof(BigStr *));
48
49 log("alignof(max_align_t) = %d", alignof(max_align_t));
50
51 PASS();
52}
53
54// Doesn't really test anything
55TEST sizeof_core_types() {
56 log("");
57
58 // 8 byte header
59 log("sizeof(ObjHeader) = %d", sizeof(ObjHeader));
60 // 8 + 128 possible entries
61 // log("sizeof(LayoutFixed) = %d", sizeof(LayoutFixed));
62
63 // 24 = 4 + (4 + 4 + 4) + 8
64 // Feels like a small string optimization here would be nice.
65 log("sizeof(BigStr) = %d", sizeof(BigStr));
66 // 16 = 4 + pad4 + 8
67 log("sizeof(List) = %d", sizeof(List<int>));
68 // 32 = 4 + pad4 + 8 + 8 + 8
69 log("sizeof(Dict) = %d", sizeof(Dict<int, int>));
70
71#ifndef MARK_SWEEP
72 int min_obj_size = sizeof(LayoutForwarded);
73 int short_str_size = aligned(kStrHeaderSize + 1);
74
75 log("kStrHeaderSize = %d", kStrHeaderSize);
76 log("aligned(kStrHeaderSize + 1) = %d", short_str_size);
77 log("sizeof(LayoutForwarded) = %d", min_obj_size);
78
79 ASSERT(min_obj_size <= short_str_size);
80#endif
81
82#if 0
83 char* p = static_cast<char*>(gHeap.Allocate(17));
84 char* q = static_cast<char*>(gHeap.Allocate(9));
85 log("p = %p", p);
86 log("q = %p", q);
87#endif
88
89 // BigStr = 16 and List = 24.
90 // Rejected ideas about slicing:
91 //
92 // - Use data[len] == '\0' as OWNING and data[len] != '\0' as a slice?
93 // It doesn't work because s[1:] would always have that problem
94 //
95 // - s->data == (void*)(s + 1)
96 // Owning string has the data RIGHT AFTER?
97 // Maybe works? but probably a bad idea because of GLOBAL BigStr instances.
98
99 log("");
100 log("sizeof(BigStr) = %zu", sizeof(BigStr));
101 log("sizeof(List<int>) = %zu", sizeof(List<int>));
102 log("sizeof(Dict<int, BigStr*>) = %zu", sizeof(Dict<int, BigStr *>));
103 log("sizeof(Tuple2<int, int>) = %zu", sizeof(Tuple2<int, int>));
104 log("sizeof(Tuple2<BigStr*, BigStr*>) = %zu",
105 sizeof(Tuple2<BigStr *, BigStr *>));
106 log("sizeof(Tuple3<int, int, int>) = %zu", sizeof(Tuple3<int, int, int>));
107
108 PASS();
109}
110
111TEST slab_growth() {
112 // TODO: All slabs should start out at 32
113
114 auto li = Alloc<List<int>>();
115 log("li->items_ %p", li->slab_);
116
117 // At some point it moves
118 for (int i = 0; i < 20; ++i) {
119 li->append(42);
120 int size = 8 + (sizeof(int) * li->capacity_);
121 log("%2d. cap %2d, size %3d, li->slab_ %p", i, li->capacity_, size,
122 li->slab_);
123 }
124
125 log("---");
126
127 auto lp = Alloc<List<BigStr *>>();
128 log("lp->items_ %p", lp->slab_);
129
130 // At some point it moves
131 for (int i = 0; i < 20; ++i) {
132 lp->append(kEmptyString);
133 int size = 8 + (sizeof(BigStr *) * lp->capacity_);
134 log("%2d. cap %2d, size %3d, lp->slab_ %p", i, lp->capacity_, size,
135 lp->slab_);
136 }
137
138 PASS();
139}
140
141TEST malloc_address_test() {
142 struct timespec start, end;
143 if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start) < 0) {
144 FAIL("clock_gettime failed");
145 }
146
147 // glibc gives us blocks of 32 bytes!
148 // 1. diff = -240
149 // 2. diff = 94064
150 // 3. diff = 32
151 // 4. diff = 32
152 // 5. diff = 32
153
154 // tcmalloc has tighter packing!
155 // 1. diff = -8
156 // 2. diff = 32
157 // 3. diff = 8
158 // 4. diff = 8
159 // 5. diff = 8
160
161 // 2023-08: If I pass 4096, I get 4112, so 16 byte diff
162 // 2023-08: If I pass 4080, I get 4096
163
164 // int alloc_size = 24 * 682; // 16368 is close to 16384 - 16 bytes again
165 int alloc_size = 48 * 341; // heap 2 is the same size
166
167 // int alloc_size = 4080;
168 // int alloc_size = 1;
169
170#define NUM_ALLOCS 20
171 char *p[NUM_ALLOCS];
172 for (int i = 0; i < NUM_ALLOCS; ++i) {
173 p[i] = static_cast<char *>(malloc(alloc_size));
174 if (i != 0) {
175 char *prev = p[i - 1];
176 log("%2d. diff = %d", i, p[i] - prev);
177 }
178 }
179
180 for (int i = 0; i < NUM_ALLOCS; ++i) {
181 free(p[i]);
182 }
183
184 if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end) < 0) {
185 FAIL("clock_gettime failed");
186 }
187
188 log("start %d %d", start.tv_sec, start.tv_nsec);
189 log("end %d %d", end.tv_sec, end.tv_nsec);
190
191 PASS();
192}
193
194GREATEST_MAIN_DEFS();
195
196int main(int argc, char **argv) {
197 gHeap.Init();
198
199 GREATEST_MAIN_BEGIN();
200
201 RUN_TEST(sizeof_syntax);
202 RUN_TEST(sizeof_core_types);
203 RUN_TEST(slab_growth);
204 RUN_TEST(malloc_address_test);
205
206 gHeap.CleanProcessExit();
207
208 GREATEST_MAIN_END();
209 return 0;
210}