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

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