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
|
#include <library/cpp/testing/benchmark/bench.h>
#include <library/cpp/containers/comptrie/comptrie_trie.h>
#include <library/cpp/containers/comptrie/comptrie_builder.h>
#include <library/cpp/containers/comptrie/search_iterator.h>
#include <library/cpp/containers/comptrie/pattern_searcher.h>
#include <library/cpp/on_disk/aho_corasick/writer.h>
#include <library/cpp/on_disk/aho_corasick/reader.h>
#include <library/cpp/on_disk/aho_corasick/helpers.h>
#include <library/cpp/containers/dense_hash/dense_hash.h>
#include <util/stream/file.h>
#include <util/generic/algorithm.h>
#include <util/random/fast.h>
#include <util/random/shuffle.h>
/////////////////
// COMMON DATA //
/////////////////
const size_t MAX_PATTERN_LENGTH = 11;
TVector<TString> letters = {
"а", "б", "в", "г", "д", "е", "ё", "ж", "з", "и", "й",
"к", "л", "м", "н", "о", "п", "р", "с", "т", "у", "ф",
"х", "ц", "ч", "ж", "щ", "ъ", "ы", "ь", "э", "ю", "я"
};
TString GenerateOneString(
TFastRng<ui64>& rng,
size_t maxLength,
const TVector<TString>& sequences
) {
size_t length = rng.GenRand() % maxLength + 1;
TString result;
while (result.size() < length) {
result += sequences[rng.GenRand() % sequences.size()];
}
return result;
}
TVector<TString> GenerateStrings(
TFastRng<ui64>& rng,
size_t num,
size_t maxLength,
const TVector<TString>& sequences
) {
TVector<TString> strings;
while (strings.size() < num) {
strings.push_back(GenerateOneString(rng, maxLength, sequences));
}
return strings;
}
struct TDatasetInstance {
TDatasetInstance(const TVector<TString>& sequences) {
TFastRng<ui64> rng(0);
TVector<TString> prefixes = GenerateStrings(rng, /*num*/10, /*maxLength*/3, sequences);
prefixes.push_back("");
TVector<TString> roots = GenerateStrings(rng, /*num*/1000, /*maxLength*/5, sequences);
TVector<TString> suffixes = GenerateStrings(rng, /*num*/10, /*maxLength*/3, sequences);
suffixes.push_back("");
TVector<TString> dictionary;
for (const auto& root : roots) {
for (const auto& prefix : prefixes) {
for (const auto& suffix : suffixes) {
dictionary.push_back(prefix + root + suffix);
Y_ASSERT(dictionary.back().size() < MAX_PATTERN_LENGTH);
}
}
}
Shuffle(dictionary.begin(), dictionary.end());
Patterns.assign(dictionary.begin(), dictionary.begin() + 10'000);
for (size_t sampleIdx = 0; sampleIdx < /*samplesNum*/1'000'000; ++sampleIdx) {
Samples.emplace_back();
size_t wordsNum = rng.GenRand() % 10;
for (size_t wordIdx = 0; wordIdx < wordsNum; ++wordIdx) {
if (wordIdx > 0) {
Samples.back() += " ";
}
Samples.back() += dictionary[rng.GenRand() % dictionary.size()];
}
}
}
TString GetSample(size_t iteration) const {
TFastRng<ui64> rng(iteration);
return Samples[rng.GenRand() % Samples.size()];
}
TVector<TString> Patterns;
TVector<TString> Samples;
};
static const TDatasetInstance dataset(letters);
//////////////////////////
// NEW PATTERN SEARCHER //
//////////////////////////
struct TPatternSearcherInstance {
TPatternSearcherInstance() {
TCompactPatternSearcherBuilder<char, ui32> builder;
for (ui32 patternId = 0; patternId < dataset.Patterns.size(); ++patternId) {
builder.Add(dataset.Patterns[patternId], patternId);
}
TBufferOutput buffer;
builder.Save(buffer);
Instance.Reset(
new TCompactPatternSearcher<char, ui32>(
buffer.Buffer().Data(),
buffer.Buffer().Size()
)
);
}
THolder<TCompactPatternSearcher<char, ui32>> Instance;
};
static const TPatternSearcherInstance patternSearcherInstance;
Y_CPU_BENCHMARK(PatternSearcher, iface) {
TVector<TVector<std::pair<ui32, ui32>>> result;
for (size_t iteration = 0; iteration < iface.Iterations(); ++iteration) {
result.emplace_back();
TString testString = dataset.GetSample(iteration);
auto matches = patternSearcherInstance.Instance->SearchMatches(testString);
for (auto& match : matches) {
result.back().emplace_back(match.End, match.Data);
}
}
}
//////////////////////
// OLD AHO CORASICK //
//////////////////////
struct TAhoCorasickInstance {
TAhoCorasickInstance() {
TAhoCorasickBuilder<TString, ui32> builder;
for (ui32 patternId = 0; patternId < dataset.Patterns.size(); ++patternId) {
builder.AddString(dataset.Patterns[patternId], patternId);
}
TBufferOutput buffer;
builder.SaveToStream(&buffer);
Instance.Reset(new TDefaultMappedAhoCorasick(TBlob::FromBuffer(buffer.Buffer())));
}
THolder<TDefaultMappedAhoCorasick> Instance;
};
static const TAhoCorasickInstance ahoCorasickInstance;
Y_CPU_BENCHMARK(AhoCorasick, iface) {
TVector<TDeque<std::pair<ui32, ui32>>> result;
for (size_t iteration = 0; iteration < iface.Iterations(); ++iteration) {
result.emplace_back();
TString testString = dataset.GetSample(iteration);
auto matches = ahoCorasickInstance.Instance->AhoSearch(testString);
result.push_back(matches);
}
}
////////////////////////////////
// COMPTRIE + SIMPLE MATCHING //
////////////////////////////////
struct TCompactTrieInstance {
TCompactTrieInstance() {
TCompactTrieBuilder<char, ui32> builder;
for (ui32 patternId = 0; patternId < dataset.Patterns.size(); ++patternId) {
builder.Add(dataset.Patterns[patternId], patternId);
}
TBufferOutput buffer;
CompactTrieMinimizeAndMakeFastLayout(buffer, builder);
Instance.Reset(new TCompactTrie<char, ui32>(
buffer.Buffer().Data(),
buffer.Buffer().Size()
));
}
THolder<TCompactTrie<char, ui32>> Instance;
};
static const TCompactTrieInstance compactTrieInstance;
Y_CPU_BENCHMARK(ComptrieSimple, iface) {
TVector<TVector<std::pair<ui32, ui32>>> result;
for (size_t iteration = 0; iteration < iface.Iterations(); ++iteration) {
result.emplace_back();
TString testString = dataset.GetSample(iteration);
for (ui32 startPos = 0; startPos < testString.size(); ++startPos) {
TSearchIterator<TCompactTrie<char, ui32>> iter(*(compactTrieInstance.Instance));
for (ui32 position = startPos; position < testString.size(); ++position) {
if (!iter.Advance(testString[position])) {
break;
}
ui32 answer;
if (iter.GetValue(&answer)) {
result.back().emplace_back(position, answer);
}
}
}
}
}
////////////////
// DENSE_HASH //
////////////////
struct TDenseHashInstance {
TDenseHashInstance() {
for (ui32 patternId = 0; patternId < dataset.Patterns.size(); ++patternId) {
Instance[dataset.Patterns[patternId]] = patternId;
}
}
TDenseHash<TString, ui32> Instance;
};
static const TDenseHashInstance denseHashInstance;
Y_CPU_BENCHMARK(DenseHash, iface) {
TVector<TVector<std::pair<ui32, ui32>>> result;
for (size_t iteration = 0; iteration < iface.Iterations(); ++iteration) {
result.emplace_back();
TString testString = dataset.GetSample(iteration);
for (size_t start = 0; start < testString.size(); ++start) {
for (
size_t length = 1;
length <= MAX_PATTERN_LENGTH && start + length <= testString.size();
++length
) {
auto value = denseHashInstance.Instance.find(testString.substr(start, length));
if (value != denseHashInstance.Instance.end()) {
result.back().emplace_back(start + length - 1, value->second);
}
}
}
}
}
|