@@ -27,24 +27,79 @@ const size_t MAX_BATCH_SIZE = 64;
27
27
template <typename TreeType> void add_values (TreeType& tree, const std::vector<NullifierLeafValue>& values)
28
28
{
29
29
Signal signal (1 );
30
- typename TreeType::AddCompletionCallback completion = [&](const auto &) -> void { signal .signal_level (0 ); };
30
+ bool success = true ;
31
+ std::string error_message;
32
+ typename TreeType::AddCompletionCallback completion = [&](const auto & result) -> void {
33
+ success = result.success ;
34
+ error_message = result.message ;
35
+ signal .signal_level (0 );
36
+ };
31
37
32
38
tree.add_or_update_values (values, completion);
33
39
signal .wait_for_level (0 );
40
+ if (!success) {
41
+ throw std::runtime_error (format (" Failed to add values: " , error_message));
42
+ }
34
43
}
35
44
36
45
template <typename TreeType> void add_values_with_witness (TreeType& tree, const std::vector<NullifierLeafValue>& values)
37
46
{
47
+ bool success = true ;
48
+ std::string error_message;
38
49
Signal signal (1 );
39
- typename TreeType::AddCompletionCallbackWithWitness completion = [&](const auto &) -> void {
50
+ typename TreeType::AddCompletionCallbackWithWitness completion = [&](const auto & result) -> void {
51
+ success = result.success ;
52
+ error_message = result.message ;
40
53
signal .signal_level (0 );
41
54
};
42
55
43
56
tree.add_or_update_values (values, completion);
44
57
signal .wait_for_level (0 );
58
+ if (!success) {
59
+ throw std::runtime_error (format (" Failed to add values with witness: " , error_message));
60
+ }
61
+ }
62
+
63
+ template <typename TreeType> void add_values_sequentially (TreeType& tree, const std::vector<NullifierLeafValue>& values)
64
+ {
65
+ bool success = true ;
66
+ std::string error_message;
67
+ Signal signal (1 );
68
+ typename TreeType::AddCompletionCallback completion = [&](const auto & result) -> void {
69
+ success = result.success ;
70
+ error_message = result.message ;
71
+ signal .signal_level (0 );
72
+ };
73
+
74
+ tree.add_or_update_values_sequentially (values, completion);
75
+ signal .wait_for_level (0 );
76
+ if (!success) {
77
+ throw std::runtime_error (format (" Failed to add values sequentially: " , error_message));
78
+ }
79
+ }
80
+
81
+ template <typename TreeType>
82
+ void add_values_sequentially_with_witness (TreeType& tree, const std::vector<NullifierLeafValue>& values)
83
+ {
84
+ bool success = true ;
85
+ std::string error_message;
86
+ Signal signal (1 );
87
+ typename TreeType::AddSequentiallyCompletionCallbackWithWitness completion = [&](const auto & result) -> void {
88
+ success = result.success ;
89
+ error_message = result.message ;
90
+ signal .signal_level (0 );
91
+ };
92
+
93
+ tree.add_or_update_values_sequentially (values, completion);
94
+ signal .wait_for_level (0 );
95
+ if (!success) {
96
+ throw std::runtime_error (format (" Failed to add values sequentially with witness: " , error_message));
97
+ }
45
98
}
46
99
47
- template <typename TreeType> void multi_thread_indexed_tree_bench (State& state) noexcept
100
+ enum InsertionStrategy { SEQUENTIAL, BATCH };
101
+
102
+ template <typename TreeType, InsertionStrategy strategy> void multi_thread_indexed_tree_bench (State& state) noexcept
48
103
{
49
104
const size_t batch_size = size_t (state.range (0 ));
50
105
const size_t depth = TREE_DEPTH;
@@ -61,10 +116,14 @@ template <typename TreeType> void multi_thread_indexed_tree_bench(State& state)
61
116
62
117
const size_t initial_size = 1024 * 16 ;
63
118
std::vector<NullifierLeafValue> initial_batch (initial_size);
64
- for (size_t i = 0 ; i < batch_size ; ++i) {
119
+ for (size_t i = 0 ; i < initial_size ; ++i) {
65
120
initial_batch[i] = fr (random_engine.get_random_uint256 ());
66
121
}
67
- add_values (tree, initial_batch);
122
+ if (strategy == SEQUENTIAL) {
123
+ add_values_sequentially (tree, initial_batch);
124
+ } else {
125
+ add_values (tree, initial_batch);
126
+ }
68
127
69
128
for (auto _ : state) {
70
129
state.PauseTiming ();
@@ -73,11 +132,15 @@ template <typename TreeType> void multi_thread_indexed_tree_bench(State& state)
73
132
values[i] = fr (random_engine.get_random_uint256 ());
74
133
}
75
134
state.ResumeTiming ();
76
- add_values (tree, values);
135
+ if (strategy == SEQUENTIAL) {
136
+ add_values_sequentially (tree, values);
137
+ } else {
138
+ add_values (tree, values);
139
+ }
77
140
}
78
141
}
79
142
80
- template <typename TreeType> void single_thread_indexed_tree_bench (State& state) noexcept
143
+ template <typename TreeType, InsertionStrategy strategy > void single_thread_indexed_tree_bench (State& state) noexcept
81
144
{
82
145
const size_t batch_size = size_t (state.range (0 ));
83
146
const size_t depth = TREE_DEPTH;
@@ -94,10 +157,14 @@ template <typename TreeType> void single_thread_indexed_tree_bench(State& state)
94
157
95
158
const size_t initial_size = 1024 * 16 ;
96
159
std::vector<NullifierLeafValue> initial_batch (initial_size);
97
- for (size_t i = 0 ; i < batch_size ; ++i) {
160
+ for (size_t i = 0 ; i < initial_size ; ++i) {
98
161
initial_batch[i] = fr (random_engine.get_random_uint256 ());
99
162
}
100
- add_values (tree, initial_batch);
163
+ if (strategy == SEQUENTIAL) {
164
+ add_values_sequentially (tree, initial_batch);
165
+ } else {
166
+ add_values (tree, initial_batch);
167
+ }
101
168
102
169
for (auto _ : state) {
103
170
state.PauseTiming ();
@@ -106,11 +173,16 @@ template <typename TreeType> void single_thread_indexed_tree_bench(State& state)
106
173
values[i] = fr (random_engine.get_random_uint256 ());
107
174
}
108
175
state.ResumeTiming ();
109
- add_values (tree, values);
176
+ if (strategy == SEQUENTIAL) {
177
+ add_values_sequentially (tree, values);
178
+ } else {
179
+ add_values (tree, values);
180
+ }
110
181
}
111
182
}
112
183
113
- template <typename TreeType> void multi_thread_indexed_tree_with_witness_bench (State& state) noexcept
184
+ template <typename TreeType, InsertionStrategy strategy>
185
+ void multi_thread_indexed_tree_with_witness_bench (State& state) noexcept
114
186
{
115
187
const size_t batch_size = size_t (state.range (0 ));
116
188
const size_t depth = TREE_DEPTH;
@@ -127,10 +199,14 @@ template <typename TreeType> void multi_thread_indexed_tree_with_witness_bench(S
127
199
128
200
const size_t initial_size = 1024 * 16 ;
129
201
std::vector<NullifierLeafValue> initial_batch (initial_size);
130
- for (size_t i = 0 ; i < batch_size ; ++i) {
202
+ for (size_t i = 0 ; i < initial_size ; ++i) {
131
203
initial_batch[i] = fr (random_engine.get_random_uint256 ());
132
204
}
133
- add_values (tree, initial_batch);
205
+ if (strategy == SEQUENTIAL) {
206
+ add_values_sequentially (tree, initial_batch);
207
+ } else {
208
+ add_values (tree, initial_batch);
209
+ }
134
210
135
211
for (auto _ : state) {
136
212
state.PauseTiming ();
@@ -139,11 +215,16 @@ template <typename TreeType> void multi_thread_indexed_tree_with_witness_bench(S
139
215
values[i] = fr (random_engine.get_random_uint256 ());
140
216
}
141
217
state.ResumeTiming ();
142
- add_values_with_witness (tree, values);
218
+ if (strategy == SEQUENTIAL) {
219
+ add_values_sequentially_with_witness (tree, values);
220
+ } else {
221
+ add_values_with_witness (tree, values);
222
+ }
143
223
}
144
224
}
145
225
146
- template <typename TreeType> void single_thread_indexed_tree_with_witness_bench (State& state) noexcept
226
+ template <typename TreeType, InsertionStrategy strategy>
227
+ void single_thread_indexed_tree_with_witness_bench (State& state) noexcept
147
228
{
148
229
const size_t batch_size = size_t (state.range (0 ));
149
230
const size_t depth = TREE_DEPTH;
@@ -160,10 +241,14 @@ template <typename TreeType> void single_thread_indexed_tree_with_witness_bench(
160
241
161
242
const size_t initial_size = 1024 * 16 ;
162
243
std::vector<NullifierLeafValue> initial_batch (initial_size);
163
- for (size_t i = 0 ; i < batch_size ; ++i) {
244
+ for (size_t i = 0 ; i < initial_size ; ++i) {
164
245
initial_batch[i] = fr (random_engine.get_random_uint256 ());
165
246
}
166
- add_values (tree, initial_batch);
247
+ if (strategy == SEQUENTIAL) {
248
+ add_values_sequentially (tree, initial_batch);
249
+ } else {
250
+ add_values (tree, initial_batch);
251
+ }
167
252
168
253
for (auto _ : state) {
169
254
state.PauseTiming ();
@@ -172,53 +257,105 @@ template <typename TreeType> void single_thread_indexed_tree_with_witness_bench(
172
257
values[i] = fr (random_engine.get_random_uint256 ());
173
258
}
174
259
state.ResumeTiming ();
175
- add_values_with_witness (tree, values);
260
+ if (strategy == SEQUENTIAL) {
261
+ add_values_sequentially_with_witness (tree, values);
262
+ } else {
263
+ add_values_with_witness (tree, values);
264
+ }
176
265
}
177
266
}
178
267
179
- BENCHMARK (single_thread_indexed_tree_with_witness_bench<Poseidon2>)
268
+ BENCHMARK (single_thread_indexed_tree_with_witness_bench<Poseidon2, BATCH >)
180
269
->Unit(benchmark::kMillisecond )
181
270
->RangeMultiplier(2 )
182
271
->Range(2 , MAX_BATCH_SIZE)
183
272
->Iterations(1000 );
184
273
185
- BENCHMARK (single_thread_indexed_tree_with_witness_bench<Poseidon2>)
274
+ BENCHMARK (single_thread_indexed_tree_with_witness_bench<Poseidon2, BATCH >)
186
275
->Unit(benchmark::kMillisecond )
187
276
->RangeMultiplier(2 )
188
277
->Range(512 , 8192 )
189
278
->Iterations(10 );
190
279
191
- BENCHMARK (multi_thread_indexed_tree_with_witness_bench <Poseidon2>)
280
+ BENCHMARK (single_thread_indexed_tree_with_witness_bench <Poseidon2, SEQUENTIAL >)
192
281
->Unit(benchmark::kMillisecond )
193
282
->RangeMultiplier(2 )
194
283
->Range(2 , MAX_BATCH_SIZE)
195
284
->Iterations(1000 );
196
285
197
- BENCHMARK (multi_thread_indexed_tree_with_witness_bench <Poseidon2>)
286
+ BENCHMARK (single_thread_indexed_tree_with_witness_bench <Poseidon2, SEQUENTIAL >)
198
287
->Unit(benchmark::kMillisecond )
199
288
->RangeMultiplier(2 )
200
289
->Range(512 , 8192 )
201
290
->Iterations(10 );
202
291
203
- BENCHMARK (single_thread_indexed_tree_bench <Poseidon2>)
292
+ BENCHMARK (multi_thread_indexed_tree_with_witness_bench <Poseidon2, BATCH >)
204
293
->Unit(benchmark::kMillisecond )
205
294
->RangeMultiplier(2 )
206
295
->Range(2 , MAX_BATCH_SIZE)
207
296
->Iterations(1000 );
208
297
209
- BENCHMARK (single_thread_indexed_tree_bench <Poseidon2>)
298
+ BENCHMARK (multi_thread_indexed_tree_with_witness_bench <Poseidon2, BATCH >)
210
299
->Unit(benchmark::kMillisecond )
211
300
->RangeMultiplier(2 )
212
301
->Range(512 , 8192 )
213
302
->Iterations(10 );
214
303
215
- BENCHMARK (multi_thread_indexed_tree_bench<Poseidon2>)
304
+ BENCHMARK (multi_thread_indexed_tree_with_witness_bench<Poseidon2, SEQUENTIAL>)
305
+ ->Unit(benchmark::kMillisecond )
306
+ ->RangeMultiplier(2 )
307
+ ->Range(2 , MAX_BATCH_SIZE)
308
+ ->Iterations(1000 );
309
+
310
+ BENCHMARK (multi_thread_indexed_tree_with_witness_bench<Poseidon2, SEQUENTIAL>)
311
+ ->Unit(benchmark::kMillisecond )
312
+ ->RangeMultiplier(2 )
313
+ ->Range(512 , 8192 )
314
+ ->Iterations(10 );
315
+
316
+ BENCHMARK (single_thread_indexed_tree_bench<Poseidon2, BATCH>)
317
+ ->Unit(benchmark::kMillisecond )
318
+ ->RangeMultiplier(2 )
319
+ ->Range(2 , MAX_BATCH_SIZE)
320
+ ->Iterations(1000 );
321
+
322
+ BENCHMARK (single_thread_indexed_tree_bench<Poseidon2, BATCH>)
323
+ ->Unit(benchmark::kMillisecond )
324
+ ->RangeMultiplier(2 )
325
+ ->Range(512 , 8192 )
326
+ ->Iterations(10 );
327
+
328
+ BENCHMARK (single_thread_indexed_tree_bench<Poseidon2, SEQUENTIAL>)
329
+ ->Unit(benchmark::kMillisecond )
330
+ ->RangeMultiplier(2 )
331
+ ->Range(2 , MAX_BATCH_SIZE)
332
+ ->Iterations(1000 );
333
+
334
+ BENCHMARK (single_thread_indexed_tree_bench<Poseidon2, SEQUENTIAL>)
335
+ ->Unit(benchmark::kMillisecond )
336
+ ->RangeMultiplier(2 )
337
+ ->Range(512 , 8192 )
338
+ ->Iterations(10 );
339
+
340
+ BENCHMARK (multi_thread_indexed_tree_bench<Poseidon2, BATCH>)
341
+ ->Unit(benchmark::kMillisecond )
342
+ ->RangeMultiplier(2 )
343
+ ->Range(2 , MAX_BATCH_SIZE)
344
+ ->Iterations(1000 );
345
+
346
+ BENCHMARK (multi_thread_indexed_tree_bench<Poseidon2, BATCH>)
347
+ ->Unit(benchmark::kMillisecond )
348
+ ->RangeMultiplier(2 )
349
+ ->Range(512 , 8192 )
350
+ ->Iterations(100 );
351
+
352
+ BENCHMARK (multi_thread_indexed_tree_bench<Poseidon2, SEQUENTIAL>)
216
353
->Unit(benchmark::kMillisecond )
217
354
->RangeMultiplier(2 )
218
355
->Range(2 , MAX_BATCH_SIZE)
219
356
->Iterations(1000 );
220
357
221
- BENCHMARK (multi_thread_indexed_tree_bench<Poseidon2>)
358
+ BENCHMARK (multi_thread_indexed_tree_bench<Poseidon2, SEQUENTIAL >)
222
359
->Unit(benchmark::kMillisecond )
223
360
->RangeMultiplier(2 )
224
361
->Range(512 , 8192 )
0 commit comments