Skip to content

Commit a174fb1

Browse files
authored
Replaced hashmap by b-tree map in large boc serializer (#1686)
1 parent eb45c48 commit a174fb1

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

crypto/test/test-db.cpp

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,6 +1691,31 @@ TEST(TonDb, BocDeserializerSimpleThreads) {
16911691
test_boc_deserializer_threads<StaticBagOfCellsDbLazy>();
16921692
}
16931693

1694+
class RandomTree {
1695+
public:
1696+
RandomTree(size_t size, td::Random::Xorshift128plus rnd) : rnd_(rnd) {
1697+
root_ = create(size);
1698+
}
1699+
Ref<Cell> root() const {
1700+
return root_;
1701+
}
1702+
private:
1703+
Ref<Cell> root_;
1704+
td::Random::Xorshift128plus rnd_;
1705+
Ref<DataCell> create(size_t size) {
1706+
CellBuilder cb;
1707+
cb.store_long(rnd_(), rnd_() % 63 + 1);
1708+
if (size > 0) {
1709+
td::uint64 rc = (rnd_() % 4) + 1;
1710+
for (td::uint64 i = 0; i < rc; i++) {
1711+
auto ref = create(size / rc);
1712+
cb.store_ref(std::move(ref));
1713+
}
1714+
}
1715+
return cb.finalize();
1716+
}
1717+
};
1718+
16941719
class CompactArray {
16951720
public:
16961721
CompactArray(size_t size) {
@@ -2987,11 +3012,8 @@ TEST(TonDb, DynamicBocRespectsUsageCell) {
29873012

29883013
TEST(TonDb, LargeBocSerializer) {
29893014
td::Random::Xorshift128plus rnd{123};
2990-
size_t n = 1000000;
2991-
std::vector<td::uint64> data(n);
2992-
std::iota(data.begin(), data.end(), 0);
2993-
vm::CompactArray arr(data);
2994-
auto root = arr.root();
3015+
vm::RandomTree tree(100000, rnd);
3016+
auto root = tree.root();
29953017
std::string path = "serialization";
29963018
td::unlink(path).ignore();
29973019
auto fd = td::FileFd::open(path, td::FileFd::Flags::Create | td::FileFd::Flags::Truncate | td::FileFd::Flags::Write)
@@ -3009,15 +3031,20 @@ TEST(TonDb, LargeBocSerializer) {
30093031
dboc->commit(cell_storer);
30103032
dboc->set_loader(std::make_unique<vm::CellLoader>(kv));
30113033
td::unlink(path).ignore();
3012-
fd = td::FileFd::open(path, td::FileFd::Flags::Create | td::FileFd::Flags::Truncate | td::FileFd::Flags::Write)
3013-
.move_as_ok();
3014-
boc_serialize_to_file_large(dboc->get_cell_db_reader(), root->get_hash(), fd, 31);
3015-
fd.close();
3016-
auto b = td::read_file_str(path).move_as_ok();
3017-
3034+
td::string prev_b("");
30183035
auto a_cell = vm::deserialize_boc(td::BufferSlice(a));
3019-
auto b_cell = vm::deserialize_boc(td::BufferSlice(b));
3020-
ASSERT_EQ(a_cell->get_hash(), b_cell->get_hash());
3036+
for (int i = 0; i < 4; i++) {
3037+
fd = td::FileFd::open(path, td::FileFd::Flags::Create | td::FileFd::Flags::Truncate | td::FileFd::Flags::Write)
3038+
.move_as_ok();
3039+
boc_serialize_to_file_large(dboc->get_cell_db_reader(), root->get_hash(), fd, 31);
3040+
fd.close();
3041+
auto b = td::read_file_str(path).move_as_ok();
3042+
3043+
auto b_cell = vm::deserialize_boc(td::BufferSlice(b));
3044+
ASSERT_EQ(a_cell->get_hash(), b_cell->get_hash());
3045+
if (i > 0) ASSERT_EQ(prev_b, b);
3046+
prev_b = b;
3047+
}
30213048
}
30223049

30233050
TEST(TonDb, DoNotMakeListsPrunned) {

crypto/vm/large-boc-serializer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ td::Status LargeBocSerializer::import_cells() {
115115

116116
td::Result<int> LargeBocSerializer::import_cell(Hash root_hash, int root_depth) {
117117
const int start_ind = cell_count;
118-
td::HashMap<Hash, std::pair<int, bool>> current_depth_hashes;
118+
td::BTreeMap<Hash, std::pair<int, bool>> current_depth_hashes;
119119

120120
auto existing_it = cells.find(root_hash);
121121
if (existing_it != cells.end()) {
@@ -131,7 +131,7 @@ td::Result<int> LargeBocSerializer::import_cell(Hash root_hash, int root_depth)
131131
}
132132

133133
cell_list.resize(cell_list.size() + current_depth_hashes.size());
134-
td::HashMap<Hash, std::pair<int, bool>> next_depth_hashes;
134+
td::BTreeMap<Hash, std::pair<int, bool>> next_depth_hashes;
135135
auto batch_start = current_depth_hashes.begin();
136136
while (batch_start != current_depth_hashes.end()) {
137137
std::vector<td::Slice> batch_hashes;

tdutils/td/utils/HashMap.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
#if TD_HAVE_ABSL
2424
#include <absl/container/flat_hash_map.h>
2525
#include <absl/container/node_hash_map.h>
26+
#include <absl/container/btree_map.h>
2627
#else
2728
#include <unordered_map>
29+
#include <map>
2830
#endif
2931

3032
namespace td {
@@ -34,11 +36,15 @@ template <class Key, class Value, class H = Hash<Key>>
3436
using HashMap = absl::flat_hash_map<Key, Value, H>;
3537
template <class Key, class Value, class H = Hash<Key>, class E = std::equal_to<>>
3638
using NodeHashMap = absl::node_hash_map<Key, Value, H, E>;
39+
template <class Key, class Value>
40+
using BTreeMap = absl::btree_map<Key, Value>;
3741
#else
3842
template <class Key, class Value, class H = Hash<Key>>
3943
using HashMap = std::unordered_map<Key, Value, H>;
4044
template <class Key, class Value, class H = Hash<Key>>
4145
using NodeHashMap = std::unordered_map<Key, Value, H>;
46+
template <class Key, class Value>
47+
using BTreeMap = std::map<Key, Value>;
4248
#endif
4349

4450
} // namespace td

0 commit comments

Comments
 (0)