-
Notifications
You must be signed in to change notification settings - Fork 4
/
art_internal_impl.hpp
1982 lines (1677 loc) · 73.6 KB
/
art_internal_impl.hpp
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019-2024 Laurynas Biveinis
#ifndef UNODB_DETAIL_ART_INTERNAL_IMPL_HPP
#define UNODB_DETAIL_ART_INTERNAL_IMPL_HPP
#include "global.hpp"
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <limits>
#include <memory>
#include <stdexcept>
#include <type_traits>
#include <utility>
#ifdef UNODB_DETAIL_X86_64
#include <emmintrin.h>
#ifdef UNODB_DETAIL_AVX2
#include <immintrin.h>
#elif defined(UNODB_DETAIL_SSE4_2)
#include <smmintrin.h>
#endif
#elif defined(__aarch64__)
#include <arm_neon.h>
#endif
#include <gsl/util>
#include "art_common.hpp"
#include "art_internal.hpp"
#include "assert.hpp"
#include "heap.hpp"
#include "node_type.hpp"
#include "portability_builtins.hpp"
namespace unodb {
class db;
class olc_db;
} // namespace unodb
namespace unodb::detail {
template <>
[[nodiscard, gnu::const]] UNODB_DETAIL_CONSTEXPR_NOT_MSVC std::uint64_t
basic_art_key<std::uint64_t>::make_binary_comparable(std::uint64_t k) noexcept {
#ifdef UNODB_DETAIL_LITTLE_ENDIAN
return bswap(k);
#else
#error Needs implementing
#endif
}
#ifdef UNODB_DETAIL_X86_64
// Idea from https://stackoverflow.com/a/32945715/80458
[[nodiscard, gnu::const]] inline auto _mm_cmple_epu8(__m128i x,
__m128i y) noexcept {
return _mm_cmpeq_epi8(_mm_max_epu8(y, x), y);
}
#elif !defined(__aarch64__)
// From public domain
// https://graphics.stanford.edu/~seander/bithacks.html
[[nodiscard, gnu::const]] constexpr std::uint32_t has_zero_byte(
std::uint32_t v) noexcept {
return ((v - 0x01010101UL) & ~v & 0x80808080UL);
}
[[nodiscard, gnu::const]] constexpr std::uint32_t contains_byte(
std::uint32_t v, std::byte b) noexcept {
return has_zero_byte(v ^ (~0U / 255 * static_cast<std::uint8_t>(b)));
}
#endif // #ifdef UNODB_DETAIL_X86_64
template <class Header>
class [[nodiscard]] basic_leaf final : public Header {
public:
using value_size_type = std::uint32_t;
UNODB_DETAIL_DISABLE_MSVC_WARNING(26495)
constexpr basic_leaf(art_key k, value_view v) noexcept
: key{k}, value_size{gsl::narrow_cast<value_size_type>(v.size())} {
UNODB_DETAIL_ASSERT(v.size() <= max_value_size);
if (!v.empty()) std::memcpy(&value_start[0], v.data(), value_size);
}
UNODB_DETAIL_RESTORE_MSVC_WARNINGS()
[[nodiscard, gnu::pure]] constexpr auto get_key() const noexcept {
return key;
}
[[nodiscard, gnu::pure]] constexpr auto matches(art_key k) const noexcept {
return k == get_key();
}
[[nodiscard, gnu::pure]] constexpr auto get_value_view() const noexcept {
return value_view{&value_start[0], value_size};
}
[[nodiscard, gnu::pure]] constexpr auto get_size() const noexcept {
return compute_size(value_size);
}
[[gnu::cold]] UNODB_DETAIL_NOINLINE void dump(std::ostream &os) const {
os << ", " << get_key() << ", value size: " << value_size << '\n';
}
[[nodiscard, gnu::const]] static constexpr auto compute_size(
value_size_type val_size) noexcept {
return sizeof(basic_leaf<Header>) + val_size - 1;
}
static constexpr std::size_t max_value_size =
std::numeric_limits<value_size_type>::max();
private:
const art_key key;
const value_size_type value_size;
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
std::byte value_start[1];
};
template <class Header, class Db>
[[nodiscard]] auto make_db_leaf_ptr(art_key k, value_view v,
Db &db UNODB_DETAIL_LIFETIMEBOUND) {
using leaf_type = basic_leaf<Header>;
if (UNODB_DETAIL_UNLIKELY(v.size() > leaf_type::max_value_size)) {
throw std::length_error("Value length must fit in std::uint32_t");
}
const auto size = leaf_type::compute_size(
gsl::narrow_cast<typename leaf_type::value_size_type>(v.size()));
auto *const leaf_mem = static_cast<std::byte *>(
allocate_aligned(size, alignment_for_new<leaf_type>()));
db.increment_leaf_count(size);
return basic_db_leaf_unique_ptr<Header, Db>{
new (leaf_mem) leaf_type{k, v}, basic_db_leaf_deleter<Header, Db>{db}};
}
template <class INode, class Node4, class Node16, class Node48, class Node256>
struct basic_inode_def final {
using inode = INode;
using n4 = Node4;
using n16 = Node16;
using n48 = Node48;
using n256 = Node256;
template <class Node>
[[nodiscard]] static constexpr bool is_inode() noexcept {
return std::is_same_v<Node, n4> || std::is_same_v<Node, n16> ||
std::is_same_v<Node, n48> || std::is_same_v<Node, n256>;
}
basic_inode_def() = delete;
};
// Implementation of things declared in art_internal.hpp
template <class Header, class Db>
inline void basic_db_leaf_deleter<Header, Db>::operator()(
leaf_type *to_delete) const noexcept {
const auto leaf_size = to_delete->get_size();
free_aligned(to_delete);
db.decrement_leaf_count(leaf_size);
}
template <class INode, class Db>
inline void basic_db_inode_deleter<INode, Db>::operator()(
INode *inode_ptr) noexcept {
static_assert(std::is_trivially_destructible_v<INode>);
free_aligned(inode_ptr);
db.template decrement_inode_count<INode>();
}
template <class Db, template <class> class CriticalSectionPolicy, class NodePtr,
class INodeDefs, template <class> class INodeReclamator,
template <class, class> class LeafReclamator>
struct basic_art_policy final {
using node_ptr = NodePtr;
using header_type = typename NodePtr::header_type;
using inode_defs = INodeDefs;
using inode = typename inode_defs::inode;
using inode4_type = typename inode_defs::n4;
using inode16_type = typename inode_defs::n16;
using inode48_type = typename inode_defs::n48;
using inode256_type = typename inode_defs::n256;
using leaf_type = basic_leaf<header_type>;
using db = Db;
private:
template <class INode>
using db_inode_deleter = basic_db_inode_deleter<INode, Db>;
using leaf_reclaimable_ptr =
std::unique_ptr<leaf_type, LeafReclamator<header_type, Db>>;
public:
template <typename T>
using critical_section_policy = CriticalSectionPolicy<T>;
template <class INode>
using db_inode_unique_ptr = std::unique_ptr<INode, db_inode_deleter<INode>>;
using db_inode4_unique_ptr = db_inode_unique_ptr<inode4_type>;
using db_inode16_unique_ptr = db_inode_unique_ptr<inode16_type>;
using db_inode48_unique_ptr = db_inode_unique_ptr<inode48_type>;
using db_inode256_unique_ptr = db_inode_unique_ptr<inode256_type>;
template <class INode>
using db_inode_reclaimable_ptr =
std::unique_ptr<INode, INodeReclamator<INode>>;
using db_leaf_unique_ptr = basic_db_leaf_unique_ptr<header_type, Db>;
[[nodiscard]] static auto make_db_leaf_ptr(
art_key k, value_view v, Db &db_instance UNODB_DETAIL_LIFETIMEBOUND) {
return ::unodb::detail::make_db_leaf_ptr<header_type, Db>(k, v,
db_instance);
}
[[nodiscard]] static auto reclaim_leaf_on_scope_exit(
leaf_type *leaf UNODB_DETAIL_LIFETIMEBOUND,
Db &db_instance UNODB_DETAIL_LIFETIMEBOUND) noexcept {
return leaf_reclaimable_ptr{leaf,
LeafReclamator<header_type, Db>{db_instance}};
}
UNODB_DETAIL_DISABLE_GCC_11_WARNING("-Wmismatched-new-delete")
UNODB_DETAIL_DISABLE_MSVC_WARNING(26409)
template <class INode, class... Args>
[[nodiscard]] static auto make_db_inode_unique_ptr(
Db &db_instance UNODB_DETAIL_LIFETIMEBOUND, Args &&...args) {
auto *const inode_mem = static_cast<std::byte *>(
allocate_aligned(sizeof(INode), alignment_for_new<INode>()));
db_instance.template increment_inode_count<INode>();
return db_inode_unique_ptr<INode>{
new (inode_mem) INode{db_instance, std::forward<Args>(args)...},
db_inode_deleter<INode>{db_instance}};
}
UNODB_DETAIL_RESTORE_MSVC_WARNINGS()
UNODB_DETAIL_RESTORE_GCC_11_WARNINGS()
template <class INode>
[[nodiscard]] static auto make_db_inode_unique_ptr(
INode *inode_ptr UNODB_DETAIL_LIFETIMEBOUND,
Db &db_instance UNODB_DETAIL_LIFETIMEBOUND) noexcept {
return db_inode_unique_ptr<INode>{inode_ptr,
db_inode_deleter<INode>{db_instance}};
}
template <class INode>
[[nodiscard]] static auto make_db_inode_reclaimable_ptr(
INode *inode_ptr UNODB_DETAIL_LIFETIMEBOUND,
Db &db_instance UNODB_DETAIL_LIFETIMEBOUND) noexcept {
return db_inode_reclaimable_ptr<INode>{inode_ptr,
INodeReclamator<INode>{db_instance}};
}
private:
[[nodiscard]] static auto make_db_leaf_ptr(
leaf_type *leaf UNODB_DETAIL_LIFETIMEBOUND,
Db &db_instance UNODB_DETAIL_LIFETIMEBOUND) noexcept {
return basic_db_leaf_unique_ptr<header_type, Db>{
leaf, basic_db_leaf_deleter<header_type, Db>{db_instance}};
}
struct delete_db_node_ptr_at_scope_exit final {
constexpr explicit delete_db_node_ptr_at_scope_exit(
NodePtr node_ptr_ UNODB_DETAIL_LIFETIMEBOUND,
Db &db_ UNODB_DETAIL_LIFETIMEBOUND) noexcept
: node_ptr{node_ptr_}, db{db_} {}
~delete_db_node_ptr_at_scope_exit() noexcept {
switch (node_ptr.type()) {
case node_type::LEAF: {
const auto r{
make_db_leaf_ptr(node_ptr.template ptr<leaf_type *>(), db)};
return;
}
case node_type::I4: {
const auto r{make_db_inode_unique_ptr(
node_ptr.template ptr<inode4_type *>(), db)};
return;
}
case node_type::I16: {
const auto r{make_db_inode_unique_ptr(
node_ptr.template ptr<inode16_type *>(), db)};
return;
}
case node_type::I48: {
const auto r{make_db_inode_unique_ptr(
node_ptr.template ptr<inode48_type *>(), db)};
return;
}
case node_type::I256: {
const auto r{make_db_inode_unique_ptr(
node_ptr.template ptr<inode256_type *>(), db)};
return;
}
}
UNODB_DETAIL_CANNOT_HAPPEN(); // LCOV_EXCL_LINE
}
delete_db_node_ptr_at_scope_exit(const delete_db_node_ptr_at_scope_exit &) =
delete;
delete_db_node_ptr_at_scope_exit(delete_db_node_ptr_at_scope_exit &&) =
delete;
auto &operator=(const delete_db_node_ptr_at_scope_exit &) = delete;
auto &operator=(delete_db_node_ptr_at_scope_exit &&) = delete;
private:
const NodePtr node_ptr;
Db &db;
};
public:
static void delete_subtree(NodePtr node, Db &db_instance) noexcept {
delete_db_node_ptr_at_scope_exit delete_on_scope_exit{node, db_instance};
switch (node.type()) {
case node_type::LEAF:
return;
case node_type::I4: {
auto *const subtree_ptr{node.template ptr<inode4_type *>()};
subtree_ptr->delete_subtree(db_instance);
return;
}
case node_type::I16: {
auto *const subtree_ptr{node.template ptr<inode16_type *>()};
subtree_ptr->delete_subtree(db_instance);
return;
}
case node_type::I48: {
auto *const subtree_ptr{node.template ptr<inode48_type *>()};
subtree_ptr->delete_subtree(db_instance);
return;
}
case node_type::I256: {
auto *const subtree_ptr{node.template ptr<inode256_type *>()};
subtree_ptr->delete_subtree(db_instance);
return;
}
}
}
[[gnu::cold]] UNODB_DETAIL_NOINLINE static void dump_node(
std::ostream &os, const NodePtr &node) {
os << "node at: " << node.template ptr<void *>() << ", tagged ptr = 0x"
<< std::hex << node.raw_val() << std::dec;
if (node == nullptr) {
os << '\n';
return;
}
os << ", type = ";
switch (node.type()) {
case node_type::LEAF:
os << "LEAF";
node.template ptr<leaf_type *>()->dump(os);
break;
case node_type::I4:
os << "I4";
node.template ptr<inode4_type *>()->dump(os);
break;
case node_type::I16:
os << "I16";
node.template ptr<inode16_type *>()->dump(os);
break;
case node_type::I48:
os << "I48";
node.template ptr<inode48_type *>()->dump(os);
break;
case node_type::I256:
os << "I256";
node.template ptr<inode256_type *>()->dump(os);
break;
}
}
basic_art_policy() = delete;
};
template <template <class> class CriticalSectionPolicy>
union [[nodiscard]] key_prefix {
private:
template <typename T>
using critical_section_policy = CriticalSectionPolicy<T>;
using key_prefix_size = std::uint8_t;
static constexpr key_prefix_size key_prefix_capacity = 7;
using key_prefix_data =
std::array<critical_section_policy<std::byte>, key_prefix_capacity>;
struct [[nodiscard]] inode_fields {
key_prefix_data key_prefix;
critical_section_policy<key_prefix_size> key_prefix_length;
} f;
critical_section_policy<std::uint64_t> u64;
public:
key_prefix(art_key k1, art_key shifted_k2, tree_depth depth) noexcept
: u64{make_u64(k1, shifted_k2, depth)} {}
key_prefix(unsigned key_prefix_len,
const key_prefix &source_key_prefix) noexcept
: u64{(source_key_prefix.u64 & key_bytes_mask) |
length_to_word(key_prefix_len)} {
UNODB_DETAIL_ASSERT(key_prefix_len <= key_prefix_capacity);
}
key_prefix(const key_prefix &other) noexcept : u64{other.u64.load()} {}
~key_prefix() noexcept = default;
[[nodiscard]] constexpr auto get_shared_length(
unodb::detail::art_key shifted_key) const noexcept {
return shared_len(static_cast<std::uint64_t>(shifted_key), u64, length());
}
[[nodiscard]] constexpr unsigned length() const noexcept {
const auto result = f.key_prefix_length.load();
UNODB_DETAIL_ASSERT(result <= key_prefix_capacity);
return result;
}
constexpr void cut(unsigned cut_len) noexcept {
UNODB_DETAIL_ASSERT(cut_len > 0);
UNODB_DETAIL_ASSERT(cut_len <= length());
u64 = ((u64 >> (cut_len * 8)) & key_bytes_mask) |
length_to_word(length() - cut_len);
UNODB_DETAIL_ASSERT(f.key_prefix_length.load() <= key_prefix_capacity);
}
constexpr void prepend(const key_prefix &prefix1,
std::byte prefix2) noexcept {
UNODB_DETAIL_ASSERT(length() + prefix1.length() < key_prefix_capacity);
const auto prefix1_bit_length = prefix1.length() * 8U;
const auto prefix1_mask = (1ULL << prefix1_bit_length) - 1;
const auto prefix3_bit_length = length() * 8U;
const auto prefix3_mask = (1ULL << prefix3_bit_length) - 1;
const auto prefix3 = u64 & prefix3_mask;
const auto shifted_prefix3 = prefix3 << (prefix1_bit_length + 8U);
const auto shifted_prefix2 = static_cast<std::uint64_t>(prefix2)
<< prefix1_bit_length;
const auto masked_prefix1 = prefix1.u64 & prefix1_mask;
u64 = shifted_prefix3 | shifted_prefix2 | masked_prefix1 |
length_to_word(length() + prefix1.length() + 1);
UNODB_DETAIL_ASSERT(f.key_prefix_length.load() <= key_prefix_capacity);
}
[[nodiscard]] constexpr auto byte_at(std::size_t i) const noexcept {
UNODB_DETAIL_ASSERT(i < length());
return f.key_prefix[i].load();
}
[[gnu::cold]] UNODB_DETAIL_NOINLINE void dump(std::ostream &os) const {
const auto len = length();
os << ", key prefix len = " << len;
if (len > 0) {
os << ", key prefix =";
for (std::size_t i = 0; i < len; ++i) dump_byte(os, f.key_prefix[i]);
}
}
key_prefix(key_prefix &&) = delete;
key_prefix &operator=(const key_prefix &) = delete;
key_prefix &operator=(key_prefix &&) = delete;
private:
static constexpr auto key_bytes_mask = 0x00FF'FFFF'FFFF'FFFFULL;
[[nodiscard, gnu::const]] static constexpr std::uint64_t length_to_word(
unsigned length) {
UNODB_DETAIL_ASSERT(length <= key_prefix_capacity);
return static_cast<std::uint64_t>(length) << 56U;
}
[[nodiscard, gnu::const]] static constexpr unsigned shared_len(
std::uint64_t k1, std::uint64_t k2, unsigned clamp_byte_pos) noexcept {
UNODB_DETAIL_ASSERT(clamp_byte_pos < 8);
const auto diff = k1 ^ k2;
const auto clamped = diff | (1ULL << (clamp_byte_pos * 8U));
return static_cast<unsigned>(detail::ctz(clamped) >> 3U);
}
[[nodiscard, gnu::const]] static constexpr std::uint64_t make_u64(
art_key k1, art_key shifted_k2, tree_depth depth) noexcept {
k1.shift_right(depth);
const auto k1_u64 = static_cast<std::uint64_t>(k1) & key_bytes_mask;
return k1_u64 | length_to_word(shared_len(
k1_u64, static_cast<std::uint64_t>(shifted_k2),
key_prefix_capacity));
}
};
// A class used as a sentinel for basic_inode template args: the
// larger node type for the largest node type and the smaller node type for
// the smallest node type.
class fake_inode final {
public:
fake_inode() = delete;
};
template <class ArtPolicy>
class basic_inode_impl : public ArtPolicy::header_type {
public:
using node_ptr = typename ArtPolicy::node_ptr;
template <typename T>
using critical_section_policy =
typename ArtPolicy::template critical_section_policy<T>;
using db_leaf_unique_ptr = typename ArtPolicy::db_leaf_unique_ptr;
using db = typename ArtPolicy::db;
// The first element is the child index in the node, the 2nd is pointer
// to the child. If not present, the pointer is nullptr, and the index
// is undefined
using find_result =
std::pair<std::uint8_t, critical_section_policy<node_ptr> *>;
protected:
using inode_type = typename ArtPolicy::inode;
using db_inode4_unique_ptr = typename ArtPolicy::db_inode4_unique_ptr;
using db_inode16_unique_ptr = typename ArtPolicy::db_inode16_unique_ptr;
using db_inode48_unique_ptr = typename ArtPolicy::db_inode48_unique_ptr;
private:
using header_type = typename ArtPolicy::header_type;
using inode4_type = typename ArtPolicy::inode4_type;
using inode16_type = typename ArtPolicy::inode16_type;
using inode48_type = typename ArtPolicy::inode48_type;
using inode256_type = typename ArtPolicy::inode256_type;
public:
[[nodiscard]] constexpr const auto &get_key_prefix() const noexcept {
return k_prefix;
}
[[nodiscard]] constexpr auto &get_key_prefix() noexcept { return k_prefix; }
// Only for unodb::detail use.
[[nodiscard]] constexpr auto get_children_count() const noexcept {
return children_count.load();
}
UNODB_DETAIL_DISABLE_MSVC_WARNING(26491)
template <typename ReturnType, typename... Args>
[[nodiscard]] ReturnType add_or_choose_subtree(node_type type,
Args &&...args) {
switch (type) {
case node_type::I4:
return static_cast<inode4_type *>(this)->add_or_choose_subtree(
std::forward<Args>(args)...);
case node_type::I16:
return static_cast<inode16_type *>(this)->add_or_choose_subtree(
std::forward<Args>(args)...);
case node_type::I48:
return static_cast<inode48_type *>(this)->add_or_choose_subtree(
std::forward<Args>(args)...);
case node_type::I256:
return static_cast<inode256_type *>(this)->add_or_choose_subtree(
std::forward<Args>(args)...);
// LCOV_EXCL_START
case node_type::LEAF:
UNODB_DETAIL_CANNOT_HAPPEN();
}
UNODB_DETAIL_CANNOT_HAPPEN();
// LCOV_EXCL_STOP
}
template <typename ReturnType, typename... Args>
[[nodiscard]] ReturnType remove_or_choose_subtree(node_type type,
Args &&...args) {
switch (type) {
case node_type::I4:
return static_cast<inode4_type *>(this)->remove_or_choose_subtree(
std::forward<Args>(args)...);
case node_type::I16:
return static_cast<inode16_type *>(this)->remove_or_choose_subtree(
std::forward<Args>(args)...);
case node_type::I48:
return static_cast<inode48_type *>(this)->remove_or_choose_subtree(
std::forward<Args>(args)...);
case node_type::I256:
return static_cast<inode256_type *>(this)->remove_or_choose_subtree(
std::forward<Args>(args)...);
case node_type::LEAF:
// LCOV_EXCL_START
UNODB_DETAIL_CANNOT_HAPPEN();
}
UNODB_DETAIL_CANNOT_HAPPEN();
// LCOV_EXCL_STOP
}
[[nodiscard]] constexpr find_result find_child(node_type type,
std::byte key_byte) noexcept {
UNODB_DETAIL_ASSERT(type != node_type::LEAF);
// Because of the parallel updates, the callees below may work on
// inconsistent nodes and must not assert, just produce results, which are
// OK to be incorrect/inconsistent as the node state will be checked before
// acting on them.
switch (type) {
case node_type::I4:
return static_cast<inode4_type *>(this)->find_child(key_byte);
case node_type::I16:
return static_cast<inode16_type *>(this)->find_child(key_byte);
case node_type::I48:
return static_cast<inode48_type *>(this)->find_child(key_byte);
case node_type::I256:
return static_cast<inode256_type *>(this)->find_child(key_byte);
// LCOV_EXCL_START
case node_type::LEAF:
UNODB_DETAIL_CANNOT_HAPPEN();
}
UNODB_DETAIL_CANNOT_HAPPEN();
// LCOV_EXCL_STOP
}
UNODB_DETAIL_RESTORE_MSVC_WARNINGS()
constexpr basic_inode_impl(unsigned children_count_, art_key k1,
art_key shifted_k2, tree_depth depth) noexcept
: k_prefix{k1, shifted_k2, depth},
children_count{gsl::narrow_cast<std::uint8_t>(children_count_)} {}
constexpr basic_inode_impl(unsigned children_count_, unsigned key_prefix_len,
const inode_type &key_prefix_source_node) noexcept
: k_prefix{key_prefix_len, key_prefix_source_node.get_key_prefix()},
children_count{gsl::narrow_cast<std::uint8_t>(children_count_)} {}
constexpr basic_inode_impl(unsigned children_count_,
const basic_inode_impl &other) noexcept
: k_prefix{other.k_prefix},
children_count{gsl::narrow_cast<std::uint8_t>(children_count_)} {}
protected:
[[gnu::cold]] UNODB_DETAIL_NOINLINE void dump(std::ostream &os) const {
k_prefix.dump(os);
const auto children_count_ = this->children_count.load();
os << ", # children = "
<< (children_count_ == 0 ? 256 : static_cast<unsigned>(children_count_));
}
private:
key_prefix<critical_section_policy> k_prefix;
static_assert(sizeof(k_prefix) == 8);
critical_section_policy<std::uint8_t> children_count;
static constexpr std::uint8_t child_not_found_i = 0xFFU;
protected:
static constexpr find_result child_not_found{child_not_found_i, nullptr};
using leaf_type = basic_leaf<header_type>;
friend class unodb::db;
friend class unodb::olc_db;
friend struct olc_inode_immediate_deleter;
template <class, unsigned, unsigned, node_type, class, class, class>
friend class basic_inode;
template <class>
friend class basic_inode_4;
template <class>
friend class basic_inode_16;
template <class>
friend class basic_inode_48;
template <class>
friend class basic_inode_256;
};
template <class ArtPolicy, unsigned MinSize, unsigned Capacity,
node_type NodeType, class SmallerDerived, class LargerDerived,
class Derived>
class [[nodiscard]] basic_inode : public basic_inode_impl<ArtPolicy> {
static_assert(NodeType != node_type::LEAF);
static_assert(!std::is_same_v<Derived, LargerDerived>);
static_assert(!std::is_same_v<SmallerDerived, Derived>);
static_assert(!std::is_same_v<SmallerDerived, LargerDerived>);
static_assert(MinSize < Capacity);
public:
using typename basic_inode_impl<ArtPolicy>::db_leaf_unique_ptr;
using typename basic_inode_impl<ArtPolicy>::db;
using typename basic_inode_impl<ArtPolicy>::node_ptr;
template <typename... Args>
[[nodiscard]] static constexpr auto create(db &db_instance, Args &&...args) {
return ArtPolicy::template make_db_inode_unique_ptr<Derived>(
db_instance, std::forward<Args>(args)...);
}
#ifndef NDEBUG
[[nodiscard]] constexpr bool is_full_for_add() const noexcept {
return this->children_count == capacity;
}
#endif
[[nodiscard]] constexpr bool is_min_size() const noexcept {
return this->children_count == min_size;
}
static constexpr auto min_size = MinSize;
static constexpr auto capacity = Capacity;
static constexpr auto type = NodeType;
using larger_derived_type = LargerDerived;
using smaller_derived_type = SmallerDerived;
using inode_type = typename basic_inode_impl<ArtPolicy>::inode_type;
protected:
constexpr basic_inode(art_key k1, art_key shifted_k2,
tree_depth depth) noexcept
: basic_inode_impl<ArtPolicy>{MinSize, k1, shifted_k2, depth} {
UNODB_DETAIL_ASSERT(is_min_size());
}
constexpr basic_inode(unsigned key_prefix_len,
const inode_type &key_prefix_source_node) noexcept
: basic_inode_impl<ArtPolicy>{MinSize, key_prefix_len,
key_prefix_source_node} {
UNODB_DETAIL_ASSERT(is_min_size());
}
explicit constexpr basic_inode(const SmallerDerived &source_node) noexcept
: basic_inode_impl<ArtPolicy>{MinSize, source_node} {
// Cannot assert that source_node.is_full_for_add because we are creating
// this node optimistically in the case of OLC.
UNODB_DETAIL_ASSERT(is_min_size());
}
explicit constexpr basic_inode(const LargerDerived &source_node) noexcept
: basic_inode_impl<ArtPolicy>{Capacity, source_node} {
// Cannot assert that source_node.is_min_size because we are creating this
// node optimistically in the case of OLC.
UNODB_DETAIL_ASSERT(is_full_for_add());
}
};
template <class ArtPolicy>
using basic_inode_4_parent =
basic_inode<ArtPolicy, 2, 4, node_type::I4, fake_inode,
typename ArtPolicy::inode16_type,
typename ArtPolicy::inode4_type>;
template <class ArtPolicy>
class basic_inode_4 : public basic_inode_4_parent<ArtPolicy> {
using parent_class = basic_inode_4_parent<ArtPolicy>;
using typename parent_class::inode16_type;
using typename parent_class::inode4_type;
using typename parent_class::inode_type;
template <typename T>
using critical_section_policy =
typename ArtPolicy::template critical_section_policy<T>;
public:
using typename parent_class::db;
using typename parent_class::db_inode4_unique_ptr;
using typename parent_class::db_leaf_unique_ptr;
using typename parent_class::find_result;
using typename parent_class::larger_derived_type;
using typename parent_class::leaf_type;
using typename parent_class::node_ptr;
UNODB_DETAIL_DISABLE_MSVC_WARNING(26434)
constexpr basic_inode_4(db &, art_key k1, art_key shifted_k2,
tree_depth depth) noexcept
: parent_class{k1, shifted_k2, depth} {}
constexpr basic_inode_4(db &, art_key k1, art_key shifted_k2,
tree_depth depth, leaf_type *child1,
db_leaf_unique_ptr &&child2) noexcept
: parent_class{k1, shifted_k2, depth} {
init(k1, shifted_k2, depth, child1, std::move(child2));
}
constexpr basic_inode_4(db &, node_ptr source_node, unsigned len) noexcept
: parent_class{len, *source_node.template ptr<inode_type *>()} {}
constexpr basic_inode_4(db &, node_ptr source_node, unsigned len,
tree_depth depth,
db_leaf_unique_ptr &&child1) noexcept
: parent_class{len, *source_node.template ptr<inode_type *>()} {
init(source_node, len, depth, std::move(child1));
}
constexpr basic_inode_4(db &, const inode16_type &source_node) noexcept
: parent_class{source_node} {}
constexpr basic_inode_4(db &db_instance, inode16_type &source_node,
std::uint8_t child_to_delete)
: parent_class{source_node} {
init(db_instance, source_node, child_to_delete);
}
UNODB_DETAIL_RESTORE_MSVC_WARNINGS()
constexpr void init(node_ptr source_node, unsigned len, tree_depth depth,
db_leaf_unique_ptr &&child1) {
auto *const source_inode{source_node.template ptr<inode_type *>()};
auto &source_key_prefix = source_inode->get_key_prefix();
UNODB_DETAIL_ASSERT(len < source_key_prefix.length());
const auto diff_key_byte_i =
static_cast<std::remove_cv_t<decltype(art_key::size)>>(depth) + len;
UNODB_DETAIL_ASSERT(diff_key_byte_i < art_key::size);
const auto source_node_key_byte = source_key_prefix.byte_at(len);
source_key_prefix.cut(len + 1);
const auto new_key_byte = child1->get_key()[diff_key_byte_i];
add_two_to_empty(source_node_key_byte, source_node, new_key_byte,
std::move(child1));
}
constexpr void init(db &db_instance, inode16_type &source_node,
std::uint8_t child_to_delete) {
const auto reclaim_source_node{
ArtPolicy::template make_db_inode_reclaimable_ptr<inode16_type>(
&source_node, db_instance)};
auto source_keys_itr = source_node.keys.byte_array.cbegin();
auto keys_itr = keys.byte_array.begin();
auto source_children_itr = source_node.children.cbegin();
auto children_itr = children.begin();
while (source_keys_itr !=
source_node.keys.byte_array.cbegin() + child_to_delete) {
*keys_itr++ = *source_keys_itr++;
*children_itr++ = *source_children_itr++;
}
const auto r{ArtPolicy::reclaim_leaf_on_scope_exit(
source_children_itr->load().template ptr<leaf_type *>(), db_instance)};
++source_keys_itr;
++source_children_itr;
while (source_keys_itr !=
source_node.keys.byte_array.cbegin() + inode16_type::min_size) {
*keys_itr++ = *source_keys_itr++;
*children_itr++ = *source_children_itr++;
}
UNODB_DETAIL_ASSERT(this->children_count == basic_inode_4::capacity);
UNODB_DETAIL_ASSERT(
std::is_sorted(keys.byte_array.cbegin(),
keys.byte_array.cbegin() + basic_inode_4::capacity));
}
constexpr void init(art_key k1, art_key shifted_k2, tree_depth depth,
leaf_type *child1, db_leaf_unique_ptr &&child2) noexcept {
const auto k2_next_byte_depth = this->get_key_prefix().length();
const auto k1_next_byte_depth = k2_next_byte_depth + depth;
add_two_to_empty(k1[k1_next_byte_depth], node_ptr{child1, node_type::LEAF},
shifted_k2[k2_next_byte_depth], std::move(child2));
}
constexpr void add_to_nonfull(db_leaf_unique_ptr &&child, tree_depth depth,
std::uint8_t children_count_) noexcept {
UNODB_DETAIL_ASSERT(children_count_ == this->children_count);
UNODB_DETAIL_ASSERT(children_count_ < parent_class::capacity);
UNODB_DETAIL_ASSERT(std::is_sorted(
keys.byte_array.cbegin(), keys.byte_array.cbegin() + children_count_));
const auto key_byte = static_cast<std::uint8_t>(child->get_key()[depth]);
#ifdef UNODB_DETAIL_X86_64
const auto mask = (1U << children_count_) - 1;
const auto insert_pos_index = get_insert_pos(key_byte, mask);
#else
// This is also currently the best ARM implementation.
const auto first_lt = ((keys.integer & 0xFFU) < key_byte) ? 1 : 0;
const auto second_lt = (((keys.integer >> 8U) & 0xFFU) < key_byte) ? 1 : 0;
const auto third_lt = ((keys.integer >> 16U) & 0xFFU) < key_byte ? 1 : 0;
const auto insert_pos_index =
static_cast<unsigned>(first_lt + second_lt + third_lt);
#endif
for (typename decltype(keys.byte_array)::size_type i = children_count_;
i > insert_pos_index; --i) {
keys.byte_array[i] = keys.byte_array[i - 1];
children[i] = children[i - 1];
}
keys.byte_array[insert_pos_index] = static_cast<std::byte>(key_byte);
children[insert_pos_index] = node_ptr{child.release(), node_type::LEAF};
++children_count_;
this->children_count = children_count_;
UNODB_DETAIL_ASSERT(std::is_sorted(
keys.byte_array.cbegin(), keys.byte_array.cbegin() + children_count_));
}
constexpr void remove(std::uint8_t child_index, db &db_instance) noexcept {
auto children_count_ = this->children_count.load();
UNODB_DETAIL_ASSERT(child_index < children_count_);
UNODB_DETAIL_ASSERT(std::is_sorted(
keys.byte_array.cbegin(), keys.byte_array.cbegin() + children_count_));
const auto r{ArtPolicy::reclaim_leaf_on_scope_exit(
children[child_index].load().template ptr<leaf_type *>(), db_instance)};
typename decltype(keys.byte_array)::size_type i = child_index;
for (; i < static_cast<unsigned>(children_count_ - 1); ++i) {
keys.byte_array[i] = keys.byte_array[i + 1];
children[i] = children[i + 1];
}
#ifndef UNODB_DETAIL_X86_64
keys.byte_array[i] = unused_key_byte;
#endif
--children_count_;
this->children_count = children_count_;
UNODB_DETAIL_ASSERT(std::is_sorted(
keys.byte_array.cbegin(), keys.byte_array.cbegin() + children_count_));
}
[[nodiscard]] constexpr auto leave_last_child(std::uint8_t child_to_delete,
db &db_instance) noexcept {
UNODB_DETAIL_ASSERT(this->is_min_size());
// NOLINTNEXTLINE(readability-simplify-boolean-expr)
UNODB_DETAIL_ASSERT(child_to_delete == 0 || child_to_delete == 1);
auto *const child_to_delete_ptr{
children[child_to_delete].load().template ptr<leaf_type *>()};
const auto r{ArtPolicy::reclaim_leaf_on_scope_exit(child_to_delete_ptr,
db_instance)};
const std::uint8_t child_to_leave = (child_to_delete == 0) ? 1U : 0U;
const auto child_to_leave_ptr = children[child_to_leave].load();
if (child_to_leave_ptr.type() != node_type::LEAF) {
auto *const inode_to_leave_ptr{
child_to_leave_ptr.template ptr<inode_type *>()};
inode_to_leave_ptr->get_key_prefix().prepend(
this->get_key_prefix(), keys.byte_array[child_to_leave]);
}
return child_to_leave_ptr;
}
UNODB_DETAIL_DISABLE_MSVC_WARNING(26434)
[[nodiscard]] find_result find_child(std::byte key_byte) noexcept {
#ifdef UNODB_DETAIL_X86_64
const auto replicated_search_key =
_mm_set1_epi8(static_cast<char>(key_byte));
const auto keys_in_sse_reg =
_mm_cvtsi32_si128(static_cast<std::int32_t>(keys.integer.load()));
const auto matching_key_positions =
_mm_cmpeq_epi8(replicated_search_key, keys_in_sse_reg);
const auto mask = (1U << this->children_count.load()) - 1;
const auto bit_field =
static_cast<unsigned>(_mm_movemask_epi8(matching_key_positions)) & mask;
if (bit_field != 0) {
const auto i = detail::ctz(bit_field);
return std::make_pair(
i, static_cast<critical_section_policy<node_ptr> *>(&children[i]));
}
return parent_class::child_not_found;
#elif defined(__aarch64__)
const auto replicated_search_key =