gosimple / slug

URL-friendly slugify with multiple languages support.
Mozilla Public License 2.0
1.17k stars 109 forks source link

Use a buffer instead of "naive" concatenation in SubstituteRune #3

Closed hectorj closed 9 years ago

hectorj commented 9 years ago

Current string concatenation makes a lot of memory allocations. Using a buffer should be more efficient.

See : http://herman.asia/efficient-string-concatenation-in-go

hectorj commented 9 years ago

Benchmarks :

BenchmarkMakeShortAscii   100000         19571 ns/op        3601 B/op        131 allocs/op
BenchmarkMakeShort    100000         20324 ns/op        3809 B/op        129 allocs/op
BenchmarkMakeShortSymbols      50000         27025 ns/op        4306 B/op        168 allocs/op
BenchmarkMakeMediumAscii       20000         76320 ns/op       13072 B/op        479 allocs/op
BenchmarkMakeMedium    10000        100320 ns/op       24240 B/op        481 allocs/op
BenchmarkMakeLongAscii       500       3205607 ns/op     1630292 B/op       6414 allocs/op
BenchmarkSubstituteRuneShort     1000000          2225 ns/op         272 B/op         24 allocs/op
BenchmarkSubstituteRuneLong     1000       1483111 ns/op      892000 B/op       1757 allocs/op
BenchmarkSmartTruncateShort  3000000           412 ns/op          64 B/op          3 allocs/op
BenchmarkSmartTruncateLong    100000         19948 ns/op        7664 B/op         39 allocs/op
BenchmarkMakeShortAscii   100000         15589 ns/op        3553 B/op         74 allocs/op
BenchmarkMakeShort    100000         18684 ns/op        3569 B/op         72 allocs/op
BenchmarkMakeShortSymbols      50000         26587 ns/op        3890 B/op         86 allocs/op
BenchmarkMakeMediumAscii       50000         39589 ns/op        5632 B/op        101 allocs/op
BenchmarkMakeMedium    30000         55434 ns/op        7232 B/op        106 allocs/op
BenchmarkMakeLongAscii      3000        410055 ns/op       34049 B/op        429 allocs/op
BenchmarkSubstituteRuneShort     2000000           751 ns/op         144 B/op          2 allocs/op
BenchmarkSubstituteRuneLong    30000         43010 ns/op        6624 B/op          7 allocs/op
BenchmarkSmartTruncateShort  3000000           464 ns/op          64 B/op          3 allocs/op
BenchmarkSmartTruncateLong    100000         20254 ns/op        7664 B/op         39 allocs/op
matrixik commented 9 years ago

Thank you for your contributions! :grinning: