Descriptionstrings: Optimized Replace to be 2x faster.
The main idea is to avoid replace count calculation.
If len(old) <= len(new) , we make a string with 0 length and a cap of len(s), otherwise we make a string with 0 length and a cap of len(s)*1.5.
The downside is a small proportion of memory will be wasted. but if the len(old) == len(new), there would be no tradeoff at all.
benchmark old ns/op new ns/op delta
BenchmarkByteByteReplaces 8480 1364 -83.92%
BenchmarkReplaceByteByte 10015 3806 -62.00%
BenchmarkReplaceDefault 13495 7356 -45.49%
BenchmarkReplaceOldEmpty 101105 45022 -55.47%
benchmark old MB/s new MB/s speedup
BenchmarkReplaceByteByte 149.77 394.09 2.63x
BenchmarkReplaceDefault 111.15 203.90 1.83x
BenchmarkReplaceOldEmpty 14.84 33.32 2.25x
Patch Set 1 #Patch Set 2 : diff -r 131f8c8eb722 https://code.google.com/p/go #
MessagesTotal messages: 13
|