-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_func.cpp
More file actions
611 lines (520 loc) · 19.4 KB
/
Copy pathtest_func.cpp
File metadata and controls
611 lines (520 loc) · 19.4 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
#include <iostream>
#include <new>
#include <cassert>
#include <sstream>
#include <cstddef> // sized ints
#include <functional>
#include "func.hpp"
#include "time.hpp"
using u8 = std::uint8_t;
struct Stats {
std::string _log {};
void log(std::string line) {
_log += (std::move(line) + '\n');
}
void track_heap([[maybe_unused]] void * mem, [[maybe_unused]] std::size_t bytes) {
// noop for now
}
void untrack_heap([[maybe_unused]] void * mem) {}
};
static Stats stat;
std::string address_to_str(auto * p) {
std::stringstream s;
s << (const void *)p;
return s.str();
}
template <std::size_t Sz=0, bool log_allocations=false, typename R=void>
struct Test {
const char * _name;
[[no_unique_address]] std::byte store[Sz];
u8 _version = 0;
u8 _moves = 0;
bool _moved = false;
std::string name() const {
std::string s {};
if (_moves <= 3) {
for (u8 i=0; i < _moves; ++i) { s += '.'; }
} else {
s += std::to_string(_moves) + '.';
}
s += std::string(_name) + (_version == 0 ? std::string("") : std::to_string(_version));
return s;
}
void* operator new(std::size_t bytes) {
void* mem = ::operator new(bytes);
if constexpr (log_allocations) {
stat.log("(+) [heap] new " + std::to_string(bytes) + " bytes @ " + address_to_str(mem));
stat.track_heap(mem, bytes);
}
return mem;
}
void* operator new(std::size_t count, void * mem) noexcept {
if constexpr (log_allocations) {
stat.log("(^) [placement] new " + std::to_string(count) + " bytes @" + address_to_str(mem));
}
return ::operator new(count, mem);
}
void operator delete(void* mem) {
if constexpr (log_allocations) {
stat.log("(-) delete @ " + address_to_str(mem));
stat.untrack_heap(mem);
}
::operator delete(mem);
}
Test(const char * const nm)
: _name{nm}
{
stat.log("ctor {" + name() + "}");
}
Test(Test && other) noexcept
: _name{other._name}
, _moves(other._moves + 1)
{
other._moved = true;
stat.log("move ctor {" + other.name() + "} => {" + name() + "}");
}
Test(Test const& other)
: _name{other._name}
, _version(other._version + 1)
{
stat.log("copy ctor {" + other.name() + "} => {" + name() + "}");
}
Test& operator= (Test&& other) noexcept {
_name = other._name;
_version = other._moves + 1;
other._moved = true;
stat.log("move assn (" + other.name() + ") -> (" + name() + ")");
return *this;
}
Test& operator= (Test const& other) {
_name = other._name;
_version = other._version + 1;
stat.log("copy assn (" + other.name() + ") -> (" + name() + ")");
return *this;
}
~Test() {
if (!_moved) {
stat.log("~{" + name() + "}");
}
}
auto operator()() {
if (_moved) {
stat.log("!" + name() + "() called on moved object");
} else {
stat.log(name() + "() called");
}
if constexpr (std::is_void_v<R>) {
return;
} else {
return R{};
}
}
};
template <typename func, std::size_t Sz=0>
constexpr auto test1() {
{
std::cerr << "sizeof(Test<Sz>): " << sizeof(Test<Sz>) << "\n";
func f0 = Test<Sz>{"X"}; // ctor {X}, move ctor {X} => {.X}
func f1 = Test<Sz>{"A"}; // ctor {A}, move ctor {A} => {.A}
func f2 = f1; // copy ctor {.A} => {A1}
f0 = std::move(f2); // ~{.X}, move assn {moved A+} -> (moved moved A+)
// f0 = f1;
f0();
f1();
}
const auto log = std::move(stat._log);
stat._log.clear();
return log;
};
template <typename func, std::size_t Sz=0>
constexpr auto test2() {
{
func f0 = Test<Sz>{"X"}; // ctor {X}, move ctor {X} => {.X}
f0 = Test<Sz>{"Y"};
f0();
}
const auto log = std::move(stat._log);
stat._log.clear();
return log;
};
template <typename F, std::size_t Sz=100>
constexpr auto test3() {
{
F fX = Test<Sz>{"X"}; // ctor {X}, move ctor {X} => {.X}
F fA = Test<Sz>{"A"}; // ctor {A}, move ctor {A} => {.A}
fX.swap(fA);
fX(); // -> A
fA(); // -> X
}
const auto log = std::move(stat._log);
stat._log.clear();
return log;
}
int main() {
constexpr vx::cfg::function cfg {
.SBO = 16,
.require_nothrow_movable = true,
.check_empty = true,
// .allow_heap = false,
.copyable = true,
.movable = true
};
{
const auto my_log = test1<vx::func<void(), cfg>, 100>();
const auto std_log = test1<std::function<void()>, 100>();
std::cerr << my_log << "\n\n" << std_log;
assert(my_log == std_log);
}
{
const auto my_log = test2<vx::func<void(), cfg>, 100>();
const auto std_log = test2<std::function<void()>, 100>();
std::cerr << "my:\n" << my_log << "\n\nstd:\n" << std_log;
// assert(my_log == std_log);
}
constexpr auto cfg2 = [cfg]{
auto c = cfg;
// c.allow_heap=false;
c.SBO = 0;
c.copyable = false;
return c;
}();
assert((test3<vx::func<void(), cfg2>>() == test3<std::function<void()>>()));
/// non-copyable inplace func
{
constexpr vx::cfg::function inplace_cfg = {
.can_be_empty = false,
.check_empty = false,
.copyable = false,
.movable = false
};
static_assert( not std::is_default_constructible_v<vx::func<int() const, inplace_cfg>>);
static_assert( not std::is_copy_constructible_v<vx::func<int() const, inplace_cfg>>);
static_assert( not std::is_move_constructible_v<vx::func<int() const, inplace_cfg>>);
static_assert( not std::is_copy_assignable_v<vx::func<int() const, inplace_cfg>>); // const vx::func<int() const, inplace_cfg> f2 = f; // Error: no copy ctor
static_assert( not std::is_move_assignable_v<vx::func<int() const, inplace_cfg>>);
const vx::func<int() const, inplace_cfg> f = []{ return 42; };
assert(f() == 42);
}
/// move-only func
{
constexpr vx::cfg::function move_only_cfg = {
.can_be_empty = false,
.check_empty = false,
.copyable = false,
.movable = true
};
static_assert( not std::is_default_constructible_v<vx::func<int() const, move_only_cfg>>);
static_assert( not std::is_copy_constructible_v<vx::func<int() const, move_only_cfg>>);
static_assert( std::is_move_constructible_v<vx::func<int() const, move_only_cfg>>);
static_assert( not std::is_copy_assignable_v<vx::func<int() const, move_only_cfg>>);
static_assert( std::is_move_assignable_v<vx::func<int() const, move_only_cfg>>);
vx::func<int() const, move_only_cfg> f = []{ return 42; };
const vx::func<int() const, move_only_cfg> f2 = std::move(f);
assert(f2() == 42);
}
/// copy- and move- enabled
{
constexpr vx::cfg::function copy_and_move_cfg = {
.can_be_empty = false,
.check_empty = false,
.copyable = true,
.movable = true
};
static_assert( not std::is_default_constructible_v<vx::func<int() const, copy_and_move_cfg>>);
static_assert( std::is_copy_constructible_v<vx::func<int() const, copy_and_move_cfg>>);
static_assert( std::is_move_constructible_v<vx::func<int() const, copy_and_move_cfg>>);
static_assert( std::is_copy_assignable_v<vx::func<int() const, copy_and_move_cfg>>);
static_assert( std::is_move_assignable_v<vx::func<int() const, copy_and_move_cfg>>);
vx::func<int() const, copy_and_move_cfg> f = []{ return 42; };
const vx::func<int() const, copy_and_move_cfg> f2 = std::move(f);
assert(f2() == 42);
}
/// default-constructible
{
constexpr vx::cfg::function empty_cfg = {
.can_be_empty = true,
.check_empty = true,
.copyable=false
};
static_assert( std::is_default_constructible_v<vx::func<int() const, empty_cfg>>);
static_assert( not std::is_copy_constructible_v<vx::func<int() const, empty_cfg>>);
static_assert( std::is_move_constructible_v<vx::func<int() const, empty_cfg>>);
static_assert( not std::is_copy_assignable_v<vx::func<int() const, empty_cfg>>);
static_assert( std::is_move_assignable_v<vx::func<int() const, empty_cfg>>);
vx::func<int() const, empty_cfg> f;
try {
f();
assert(false);
} catch (vx::bad_function_call) {
assert(true);
}
}
/// Move-eligible trait check:
{
constexpr vx::cfg::function move_only_cfg = {
.require_nothrow_movable = true,
.can_be_empty = false,
.check_empty = false,
// .allow_heap = false,
.copyable = false,
.movable = true
};
constexpr vx::cfg::function move_only_cfg2 = {
.require_nothrow_movable = false, ///< so that's not the problem for SBO anymore
.can_be_empty = false,
.check_empty = false,
// .allow_heap = false,
.copyable = false,
.movable = true
};
constexpr vx::cfg::function inplace_cfg = {
.require_nothrow_movable = true,
.can_be_empty = false,
.check_empty = false,
.allow_heap = false,
.copyable = false,
.movable = true
};
constexpr vx::cfg::function overaligned_cfg = {
.alignment = 32,
.require_nothrow_movable = true,
.can_be_empty = false,
.check_empty = false,
.copyable = false,
.movable = true
};
constexpr vx::cfg::function huge_inplace_cfg = {
.SBO {100},
.require_nothrow_movable = true,
.can_be_empty = false,
.check_empty = false,
.allow_heap = false,
.copyable = false,
.movable = true
};
struct SmallTrouble {
SmallTrouble() = default;
SmallTrouble (SmallTrouble&&) {} /// non noexcept, non trivial
int operator()() const { return 42; }
};
struct BiggerTrouble {
const char buffer [100];
int operator()() const { return 32; }
};
struct alignas(32) AlignmentTrouble {
int operator()() const { return 22; }
};
struct NoTrouble {
int operator()() const { return 12; }
};
static_assert( std::is_constructible_v<vx::func<int() const, move_only_cfg>, SmallTrouble>,
"Still constructible from it, but will use the heap for that");
static_assert( not std::is_constructible_v<vx::func<int() const, inplace_cfg>, SmallTrouble>,
"Not constructible since will have to use the heap which is forbidden");
// vx::func<int() const, inplace_cfg> fx = SmallTrouble{}; ///< produces hard to read error message with requires clause :C
/// Thanks to the .require_nothrow_movable we have the whole func nothrow_movable:
static_assert( std::is_nothrow_move_constructible_v<vx::func<int() const, move_only_cfg>> );
static_assert( std::is_nothrow_move_assignable_v<vx::func<int() const, move_only_cfg>> );
/// The price, however, is that the non-nothrow-movable objects will be stored on the heap, even if
/// otherwise they would fit into the SBO buffer
/// Here's how we can check if our type will be stored in the SBO buffer and won't trigger the heap allocation:
/// general usage: vx::is_sbo_eligible<vx::func<signature, cfg>, OurType> -> bool
static_assert( not vx::is_sbo_eligible<vx::func<int(), move_only_cfg>, SmallTrouble>,
"the SmallTrouble is not sbo eligible since it's not nothrow movable");
/// Same but accessed through the class itself
static_assert( not vx::func<int(), move_only_cfg>::is_sbo_eligible<BiggerTrouble> );
static_assert( not vx::func<int(), move_only_cfg>::is_sbo_eligible<AlignmentTrouble> );
/// you can overalign though
static_assert( vx::func<int(), overaligned_cfg>::is_sbo_eligible<AlignmentTrouble> );
/// But with these configurations it's OK and it will in fact be stored in SBO
static_assert( vx::is_sbo_eligible<vx::func<int(), move_only_cfg2>, SmallTrouble> );
static_assert( vx::is_sbo_eligible<vx::func<int(), huge_inplace_cfg>, BiggerTrouble> );
/// However we will lose the noexcept property for moving stuff around. However the choice is always yours ;)
static_assert( not std::is_nothrow_move_constructible_v<vx::func<int() const, move_only_cfg2>> );
static_assert( not std::is_nothrow_move_assignable_v<vx::func<int() const, move_only_cfg2>> );
/// No trouble whatsoever for good types
static_assert( vx::func<int(), move_only_cfg>::is_sbo_eligible<NoTrouble> );
vx::func<int() const, move_only_cfg> f = SmallTrouble{};
const vx::func<int() const, move_only_cfg> f2 = std::move(f);
assert(f2() == 42);
}
/// Test target
{
constexpr vx::cfg::function cfg = {
.enable_typeinfo = true,
.movable = true,
.copyable = true
};
constexpr vx::cfg::function min_cfg = {
.movable = false,
.enable_typeinfo = true
};
struct X {
int operator()() { return 7; }
};
struct Y{};
vx::func<int(), cfg> f = X{};
assert(f.target<Y>() == nullptr);
assert(f.target<X>() != nullptr);
assert(f.target<X>()->operator()() == 7);
vx::func<int(), min_cfg> mf = X{};
assert(mf.target<Y>() == nullptr);
assert(mf.target<X>() != nullptr);
assert(mf.target<X>()->operator()() == 7);
assert(f.target<X>()->operator()() * mf.target<X>()->operator()() - f.target<X>()->operator()() == 42);
}
/// Test function pointer case
{
constexpr vx::cfg::function inplace_cfg = {
.SBO = 8,
.require_nothrow_movable = true,
.can_be_empty = false,
.check_empty = false,
.allow_heap = false,
.copyable = true,
.movable = true
};
vx::func<void(), inplace_cfg> f = +[]{ std::cerr << "Hello!"; };
vx::func<void(), inplace_cfg> ff = f;
f();
constexpr vx::cfg::function optimized_fptr = {
.SBO = 8,
.require_nothrow_movable = true,
.optimize_for_func_ptrs = true,
.can_be_empty = false,
.check_empty = false,
.allow_heap = false,
.copyable = true,
.movable = true
};
vx::func<void(), optimized_fptr> f2 = +[]{ std::cerr << "Hello!"; };
vx::func<void(), optimized_fptr> ff2 = f2;
// vx::func<void(), optimized_fptr> fff2 = std::move(f2); // ERROR
f2();
}
/// Micro bench
{
constexpr std::size_t N = 1'000'000;
constexpr vx::cfg::function inplace_cfg = {
.SBO = 32,
.require_nothrow_movable = true,
.can_be_empty = false,
.check_empty = false,
.allow_heap = false,
.copyable = false,
.movable = false
};
constexpr vx::cfg::function optimized_fptr = {
.SBO = 32,
.require_nothrow_movable = true,
.optimize_for_func_ptrs = true,
.can_be_empty = false,
.check_empty = false,
.allow_heap = false,
.copyable = false,
.movable = false
};
using time_units = vx::time::ms;
std::cerr << "\n\nBenchmarking function ptr times:";
// std::function
std::cerr << "\nstd::function: " << vx::timeit([&, f = std::function<void()>(+[]{ })]{
// std::function<void()> f = +[]{ };
for (std::size_t i = 0; i < N; ++i) {
f();
}
}).in<time_units>();
// std::move_only_function
#if defined __cpp_lib_move_only_function
std::cerr << "\nstd::move_only_function: " << vx::timeit([&]{
std::move_only_function<void()> f = +[]{ };
for (std::size_t i = 0; i < N; ++i) {
f();
}
}).in<time_units>();
#endif
// Default
std::cerr << "\ndefault: " << vx::timeit([&]{
vx::func<void(), inplace_cfg> f = +[]{};
for (std::size_t i = 0; i < N; ++i) {
f();
}
}).in<time_units>();
// Optimized
std::cerr << "\noptimized: " << vx::timeit([&]{
vx::func<void(), optimized_fptr> f = +[]{};
for (std::size_t i = 0; i < N; ++i) {
f();
}
}).in<time_units>();
// Func ptr:
std::cerr << "\nplain fptr: " << vx::timeit([&]{
void (*f)() = +[]{};
for (std::size_t i = 0; i < N; ++i) {
f();
}
}).in<time_units>();
int flag = 0;
// std::cin >> flag;
std::cerr << "\nplain fptr + branch: " << vx::timeit([&]{
void (*f)() = +[]{};
for (std::size_t i = 0; i < N; ++i) {
if (flag == 0) f();
}
}).in<time_units>();
}
/// Micro bench for non-ptr callables:
{
std::cerr << "\n\nBenchmarking non-ptr callables:";
constexpr std::size_t N = 1'000'000;
constexpr vx::cfg::function inplace_cfg = {
.SBO = 8,
.require_nothrow_movable = true,
.can_be_empty = false,
.check_empty = false,
.allow_heap = false,
.copyable = false,
.movable = true
};
constexpr vx::cfg::function optimized_fptr = {
.SBO = 8,
.require_nothrow_movable = true,
.optimize_for_func_ptrs = true,
.can_be_empty = false,
.check_empty = false,
.allow_heap = false,
.copyable = false,
.movable = true
};
using time_units = vx::time::ms;
struct A {
void operator()() noexcept {}
};
// std::function
std::cerr << "\nstd::function: " << vx::timeit([&]{
std::function<void()> f = A{};
for (std::size_t i = 0; i < N; ++i) {
f();
}
}).in<time_units>();
// Default
std::cerr << "\ndefault: " << vx::timeit([&]{
vx::func<void(), inplace_cfg> f = A{};
for (std::size_t i = 0; i < N; ++i) {
f();
}
}).in<time_units>();
std::cerr << "\ndefault (noexcept): " << vx::timeit([&]{
vx::func<void() noexcept, inplace_cfg> f = A{};
for (std::size_t i = 0; i < N; ++i) {
f();
}
}).in<time_units>();
// Optimized
std::cerr << "\noptimized: " << vx::timeit([&]{
vx::func<void(), optimized_fptr> f = A{};
for (std::size_t i = 0; i < N; ++i) {
f();
}
}).in<time_units>();
}
}