Skip to content

Commit e8be55f

Browse files
committed
buffers test performance improvement
1 parent 38de36a commit e8be55f

File tree

2 files changed

+58
-106
lines changed

2 files changed

+58
-106
lines changed

test/unit/slice.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ struct slice_test
117117
return;
118118
if(! BOOST_TEST_EQ(core::string_view(buf, n), s))
119119
return;
120-
test::check_iterators(b, s);
120+
121+
std::string tmp;
122+
test::check_iterators(b, s, tmp);
121123
}
122124

123125
// Use a vector so that iterator invalidation is observable during testing.
@@ -180,7 +182,7 @@ struct slice_test
180182
std::string s;
181183
auto a = make_buffers(s, "boost.", "buffers.", "slice_");
182184
seq_type bs(a.begin(), a.end());
183-
test::check_sequence(bs, s);
185+
test::check_sequence(bs, s, true);
184186
//check(bs, s);
185187
//grind(bs, s);
186188
}

test/unit/test_buffers.hpp

Lines changed: 54 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <boost/core/detail/string_view.hpp>
1818
#include <boost/assert.hpp>
1919
#include <boost/static_assert.hpp>
20-
2120
#include <string>
2221

2322
// Trick boostdep into requiring URL
@@ -100,16 +99,21 @@ template<class ConstBufferSequence>
10099
void
101100
check_iterators(
102101
ConstBufferSequence bs,
103-
core::string_view pat)
102+
core::string_view pat,
103+
std::string& s)
104104
{
105105
BOOST_ASSERT(is_const_buffer_sequence<ConstBufferSequence>::value);
106106
BOOST_TEST_EQ(size(bs), pat.size());
107107

108108
auto const& ct = bs;
109109

110+
//std::string s;
111+
s.reserve(pat.size() + 1);
112+
110113
// operator++()
111114
{
112-
std::string s;
115+
s.clear();
116+
s.reserve(pat.size() + 1);
113117
auto it = begin(bs);
114118
auto const end_ = end(bs);
115119
while(it != end_)
@@ -125,7 +129,8 @@ check_iterators(
125129

126130
// operator++(int)
127131
{
128-
std::string s;
132+
s.clear();
133+
s.reserve(pat.size() + 1);
129134
auto it = begin(bs);
130135
auto const end_ = end(bs);
131136
while(it != end_)
@@ -141,7 +146,8 @@ check_iterators(
141146

142147
// operator++() const
143148
{
144-
std::string s;
149+
s.clear();
150+
s.reserve(pat.size() + 1);
145151
auto it = begin(ct);
146152
auto const end_ = end(ct);
147153
while(it != end_)
@@ -157,7 +163,8 @@ check_iterators(
157163

158164
// operator++(int) const
159165
{
160-
std::string s;
166+
s.clear();
167+
s.reserve(pat.size() + 1);
161168
auto it = begin(ct);
162169
auto const end_ = end(ct);
163170
while(it != end_)
@@ -173,7 +180,8 @@ check_iterators(
173180

174181
// operator--()
175182
{
176-
std::string s;
183+
s.clear();
184+
s.reserve(pat.size() + 1);
177185
auto it = end(bs);
178186
auto const begin_ = begin(bs);
179187
while(it != begin_)
@@ -189,7 +197,8 @@ check_iterators(
189197

190198
// operator--(int)
191199
{
192-
std::string s;
200+
s.clear();
201+
s.reserve(pat.size() + 1);
193202
auto it = end(bs);
194203
auto const begin_ = begin(bs);
195204
while(it != begin_)
@@ -205,7 +214,8 @@ check_iterators(
205214

206215
// operator--() const
207216
{
208-
std::string s;
217+
s.clear();
218+
s.reserve(pat.size() + 1);
209219
auto it = end(ct);
210220
auto const begin_ = begin(ct);
211221
while(it != begin_)
@@ -221,7 +231,8 @@ check_iterators(
221231

222232
// operator--(int) const
223233
{
224-
std::string s;
234+
s.clear();
235+
s.reserve(pat.size() + 1);
225236
auto it = end(ct);
226237
auto const begin_ = begin(ct);
227238
while(it != begin_)
@@ -257,18 +268,20 @@ void
257268
grind_front(
258269
ConstBufferSequence const& bs0,
259270
core::string_view pat0,
260-
int levels)
271+
bool deep)
261272
{
273+
std::string tmp;
274+
262275
for(std::size_t n = 0; n <= pat0.size() + 1; ++n)
263276
{
264277
{
265278
auto pat = trimmed_front(pat0, n);
266279
slice_type<ConstBufferSequence> bs(bs0);
267280
remove_prefix(bs, n);
268281
check_eq(bs, pat);
269-
check_iterators(bs, pat);
282+
check_iterators(bs, pat, tmp);
270283

271-
if(levels)
284+
if(deep)
272285
{
273286
// Take a copy, blank out the original to invalidate any
274287
// iterators, and redo the test
@@ -277,27 +290,21 @@ grind_front(
277290
slice_type<ConstBufferSequence> dummy{};
278291
std::swap(bs, dummy);
279292
}
280-
grind_front(bsc, pat, levels-1);
293+
for(std::size_t m = 0; m <= pat.size() + 1; ++m)
294+
{
295+
auto pat2 = trimmed_front(pat, m);
296+
slice_type<ConstBufferSequence> bs2(bsc);
297+
remove_prefix(bs2, m);
298+
check_eq(bs2, pat2);
299+
}
281300
}
282301
}
283302
{
284303
auto pat = kept_front(pat0, n);
285304
slice_type<ConstBufferSequence> bs(bs0);
286305
keep_prefix(bs, n);
287306
check_eq(bs, pat);
288-
check_iterators(bs, pat);
289-
290-
if(levels)
291-
{
292-
// Take a copy, blank out the original to invalidate any
293-
// iterators, and redo the test
294-
slice_type<ConstBufferSequence> bsc(bs);
295-
{
296-
slice_type<ConstBufferSequence> dummy{};
297-
std::swap(bs, dummy);
298-
}
299-
grind_front(bsc, pat, levels-1);
300-
}
307+
check_iterators(bs, pat, tmp);
301308
}
302309
}
303310
}
@@ -307,18 +314,19 @@ void
307314
grind_back(
308315
ConstBufferSequence const& bs0,
309316
core::string_view pat0,
310-
int levels)
317+
bool deep)
311318
{
319+
std::string tmp;
320+
312321
for(std::size_t n = 0; n <= pat0.size() + 1; ++n)
313322
{
314323
{
315324
auto pat = trimmed_back(pat0, n);
316325
slice_type<ConstBufferSequence> bs(bs0);
317326
remove_suffix(bs, n);
318327
check_eq(bs, pat);
319-
check_iterators(bs, pat);
320-
321-
if(levels)
328+
check_iterators(bs, pat, tmp);
329+
if(deep)
322330
{
323331
// Take a copy, blank out the original to invalidate any
324332
// iterators, and redo the test
@@ -327,79 +335,21 @@ grind_back(
327335
slice_type<ConstBufferSequence> dummy{};
328336
std::swap(bs, dummy);
329337
}
330-
grind_back(bsc, pat, levels-1);
331-
}
332-
}
333-
{
334-
auto pat = kept_back(pat0, n);
335-
slice_type<ConstBufferSequence> bs(bs0);
336-
keep_suffix(bs, n);
337-
check_eq(bs, pat);
338-
check_iterators(bs, pat);
339-
340-
if(levels)
341-
{
342-
// Take a copy, blank out the original to invalidate any
343-
// iterators, and redo the test
344-
slice_type<ConstBufferSequence> bsc(bs);
338+
for(std::size_t m = 0; m <= pat.size() + 1; ++m)
345339
{
346-
slice_type<ConstBufferSequence> dummy{};
347-
std::swap(bs, dummy);
340+
auto pat2 = trimmed_back(pat, m);
341+
slice_type<ConstBufferSequence> bs2(bsc);
342+
remove_suffix(bs2, m);
343+
check_eq(bs2, pat2);
348344
}
349-
grind_back(bsc, pat, levels-1);
350345
}
351346
}
352-
}
353-
}
354-
355-
template<class ConstBufferSequence>
356-
void
357-
grind_both(
358-
ConstBufferSequence const& bs0,
359-
core::string_view pat0,
360-
int levels)
361-
{
362-
for(std::size_t n = 0; n <= pat0.size() / 2 + 2; ++n)
363-
{
364347
{
365-
auto pat = trimmed_back(trimmed_front(pat0, n), n);
366-
slice_type<ConstBufferSequence> bs(bs0);
367-
remove_prefix(bs, n);
368-
remove_suffix(bs, n);
369-
check_eq(bs, pat);
370-
check_iterators(bs, pat);
371-
372-
if(levels)
373-
{
374-
// Take a copy, blank out the original to invalidate any
375-
// iterators, and redo the test
376-
slice_type<ConstBufferSequence> bsc(bs);
377-
{
378-
slice_type<ConstBufferSequence> dummy{};
379-
std::swap(bs, dummy);
380-
}
381-
grind_both(bsc, pat, levels - 1);
382-
}
383-
}
384-
{
385-
auto pat = kept_back(kept_front(pat0, n), n);
348+
auto pat = kept_back(pat0, n);
386349
slice_type<ConstBufferSequence> bs(bs0);
387-
keep_prefix(bs, n);
388350
keep_suffix(bs, n);
389351
check_eq(bs, pat);
390-
check_iterators(bs, pat);
391-
392-
if(levels)
393-
{
394-
// Take a copy, blank out the original to invalidate any
395-
// iterators, and redo the test
396-
slice_type<ConstBufferSequence> bsc(bs);
397-
{
398-
slice_type<ConstBufferSequence> dummy{};
399-
std::swap(bs, dummy);
400-
}
401-
grind_both(bsc, pat, levels - 1);
402-
}
352+
check_iterators(bs, pat, tmp);
403353
}
404354
}
405355
}
@@ -408,24 +358,24 @@ template<class ConstBufferSequence>
408358
void
409359
check_slice(
410360
ConstBufferSequence const& bs,
411-
core::string_view pat)
361+
core::string_view pat,
362+
bool deep)
412363
{
413-
grind_front(bs, pat, 1);
414-
grind_back(bs, pat, 1);
415-
grind_both(bs, pat, 1);
364+
grind_front(bs, pat, deep);
365+
grind_back(bs, pat, deep);
416366
}
417367

418368
// Test API and behavior of a BufferSequence
419369
template<class T>
420370
void
421371
check_sequence(
422-
T const& t, core::string_view pat)
372+
T const& t, core::string_view pat, bool deep = false)
423373
{
424374
BOOST_STATIC_ASSERT(is_const_buffer_sequence<T>::value);
425375

426-
check_iterators(t, pat);
427-
428-
check_slice(t, pat);
376+
std::string tmp;
377+
check_iterators(t, pat, tmp);
378+
check_slice(t, pat, deep);
429379
}
430380

431381
} // test

0 commit comments

Comments
 (0)