@@ -4,15 +4,16 @@ use test::{Bencher, black_box};
44
55#[ bench]
66fn bench_with_capacity ( b : & mut Bencher ) {
7- b. iter ( || String :: with_capacity ( 100 ) ) ;
7+ b. iter ( || String :: with_capacity ( black_box ( 100 ) ) ) ;
88}
99
1010#[ bench]
1111fn bench_push_str ( b : & mut Bencher ) {
1212 let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb" ;
1313 b. iter ( || {
1414 let mut r = String :: new ( ) ;
15- r. push_str ( s) ;
15+ black_box ( & mut r) . push_str ( black_box ( s) ) ;
16+ r
1617 } ) ;
1718}
1819
@@ -24,8 +25,9 @@ fn bench_push_str_one_byte(b: &mut Bencher) {
2425 b. iter ( || {
2526 let mut r = String :: new ( ) ;
2627 for _ in 0 ..REPETITIONS {
27- r . push_str ( "a" )
28+ black_box ( & mut r ) . push_str ( black_box ( "a" ) ) ;
2829 }
30+ r
2931 } ) ;
3032}
3133
@@ -35,8 +37,9 @@ fn bench_push_char_one_byte(b: &mut Bencher) {
3537 b. iter ( || {
3638 let mut r = String :: new ( ) ;
3739 for _ in 0 ..REPETITIONS {
38- r . push ( 'a' )
40+ black_box ( & mut r ) . push ( black_box ( 'a' ) ) ;
3941 }
42+ r
4043 } ) ;
4144}
4245
@@ -46,8 +49,9 @@ fn bench_push_char_two_bytes(b: &mut Bencher) {
4649 b. iter ( || {
4750 let mut r = String :: new ( ) ;
4851 for _ in 0 ..REPETITIONS {
49- r . push ( 'â' )
52+ black_box ( & mut r ) . push ( black_box ( 'â' ) ) ;
5053 }
54+ r
5155 } ) ;
5256}
5357
@@ -57,34 +61,26 @@ fn from_utf8_lossy_100_ascii(b: &mut Bencher) {
5761 Lorem ipsum dolor sit amet, consectetur. ";
5862
5963 assert_eq ! ( 100 , s. len( ) ) ;
60- b. iter ( || {
61- let _ = String :: from_utf8_lossy ( s) ;
62- } ) ;
64+ b. iter ( || String :: from_utf8_lossy ( black_box ( s) ) ) ;
6365}
6466
6567#[ bench]
6668fn from_utf8_lossy_100_multibyte ( b : & mut Bencher ) {
6769 let s = "𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰" . as_bytes ( ) ;
6870 assert_eq ! ( 100 , s. len( ) ) ;
69- b. iter ( || {
70- let _ = String :: from_utf8_lossy ( s) ;
71- } ) ;
71+ b. iter ( || String :: from_utf8_lossy ( black_box ( s) ) ) ;
7272}
7373
7474#[ bench]
7575fn from_utf8_lossy_invalid ( b : & mut Bencher ) {
7676 let s = b"Hello\xC0 \x80 There\xE6 \x83 Goodbye" ;
77- b. iter ( || {
78- let _ = String :: from_utf8_lossy ( s) ;
79- } ) ;
77+ b. iter ( || String :: from_utf8_lossy ( black_box ( s) ) ) ;
8078}
8179
8280#[ bench]
8381fn from_utf8_lossy_100_invalid ( b : & mut Bencher ) {
8482 let s = repeat ( 0xf5 ) . take ( 100 ) . collect :: < Vec < _ > > ( ) ;
85- b. iter ( || {
86- let _ = String :: from_utf8_lossy ( & s) ;
87- } ) ;
83+ b. iter ( || String :: from_utf8_lossy ( black_box ( & s) ) ) ;
8884}
8985
9086#[ bench]
@@ -96,8 +92,8 @@ fn bench_exact_size_shrink_to_fit(b: &mut Bencher) {
9692 r. push_str ( s) ;
9793 assert_eq ! ( r. len( ) , r. capacity( ) ) ;
9894 b. iter ( || {
99- let mut r = String :: with_capacity ( s. len ( ) ) ;
100- r. push_str ( s ) ;
95+ let mut r = String :: with_capacity ( black_box ( s. len ( ) ) ) ;
96+ r. push_str ( black_box ( s ) ) ;
10197 r. shrink_to_fit ( ) ;
10298 r
10399 } ) ;
@@ -107,29 +103,29 @@ fn bench_exact_size_shrink_to_fit(b: &mut Bencher) {
107103fn bench_from_str ( b : & mut Bencher ) {
108104 let s = "Hello there, the quick brown fox jumped over the lazy dog! \
109105 Lorem ipsum dolor sit amet, consectetur. ";
110- b. iter ( || String :: from ( s ) )
106+ b. iter ( || String :: from ( black_box ( s ) ) )
111107}
112108
113109#[ bench]
114110fn bench_from ( b : & mut Bencher ) {
115111 let s = "Hello there, the quick brown fox jumped over the lazy dog! \
116112 Lorem ipsum dolor sit amet, consectetur. ";
117- b. iter ( || String :: from ( s ) )
113+ b. iter ( || String :: from ( black_box ( s ) ) )
118114}
119115
120116#[ bench]
121117fn bench_to_string ( b : & mut Bencher ) {
122118 let s = "Hello there, the quick brown fox jumped over the lazy dog! \
123119 Lorem ipsum dolor sit amet, consectetur. ";
124- b. iter ( || s . to_string ( ) )
120+ b. iter ( || black_box ( s ) . to_string ( ) )
125121}
126122
127123#[ bench]
128124fn bench_insert_char_short ( b : & mut Bencher ) {
129125 let s = "Hello, World!" ;
130126 b. iter ( || {
131127 let mut x = String :: from ( s) ;
132- black_box ( & mut x) . insert ( 6 , black_box ( ' ' ) ) ;
128+ black_box ( & mut x) . insert ( black_box ( 6 ) , black_box ( ' ' ) ) ;
133129 x
134130 } )
135131}
@@ -139,7 +135,7 @@ fn bench_insert_char_long(b: &mut Bencher) {
139135 let s = "Hello, World!" ;
140136 b. iter ( || {
141137 let mut x = String :: from ( s) ;
142- black_box ( & mut x) . insert ( 6 , black_box ( '❤' ) ) ;
138+ black_box ( & mut x) . insert ( black_box ( 6 ) , black_box ( '❤' ) ) ;
143139 x
144140 } )
145141}
@@ -149,7 +145,7 @@ fn bench_insert_str_short(b: &mut Bencher) {
149145 let s = "Hello, World!" ;
150146 b. iter ( || {
151147 let mut x = String :: from ( s) ;
152- black_box ( & mut x) . insert_str ( 6 , black_box ( " " ) ) ;
148+ black_box ( & mut x) . insert_str ( black_box ( 6 ) , black_box ( " " ) ) ;
153149 x
154150 } )
155151}
@@ -159,7 +155,7 @@ fn bench_insert_str_long(b: &mut Bencher) {
159155 let s = "Hello, World!" ;
160156 b. iter ( || {
161157 let mut x = String :: from ( s) ;
162- black_box ( & mut x) . insert_str ( 6 , black_box ( " rustic " ) ) ;
158+ black_box ( & mut x) . insert_str ( black_box ( 6 ) , black_box ( " rustic " ) ) ;
163159 x
164160 } )
165161}
0 commit comments