Skip to content

Conversation

alexheretic
Copy link
Contributor

@alexheretic alexheretic commented Sep 13, 2025

I was curious to quantify the performance of SmolStr inline strings vs naive Strings. I wrote some criterion benchmarks for lengths 12, 50, 1000 to see inline vs heap perf differences.

I would expect inline SmolStrs to generally perform better than Strings, and heap SmolStrs to perform a little worse. However, the numbers seem generally worse almost everywhere except SmolStr::clone. Can anyone comment on this and help me understand why this would be?

Update: It seems the microbenches can be an easy place for the allocator to optimise, so I'll take a look elsewhere at improving a bench comparing allocating with non-allocating. Here we can just have some benches for this crate. I've stripped the benches to only being for this crate here.

Results

format_smolstr! len=12  time:   [27.925 ns 27.977 ns 27.990 ns]

format_smolstr! len=50  time:   [66.376 ns 66.394 ns 66.399 ns]

format_smolstr! len=1000
                        time:   [95.812 ns 95.867 ns 95.880 ns]

SmolStr::from len=12    time:   [14.482 ns 14.483 ns 14.486 ns]

SmolStr::from len=50    time:   [9.7307 ns 9.7373 ns 9.7390 ns]

SmolStr::from len=1000  time:   [16.582 ns 16.582 ns 16.582 ns]

SmolStr::clone len=12   time:   [4.1175 ns 4.1258 ns 4.1278 ns]

SmolStr::clone len=50   time:   [2.8261 ns 2.8313 ns 2.8523 ns]

SmolStr::clone len=1000 time:   [2.8571 ns 2.8640 ns 2.8916 ns]

SmolStr::eq len=12      time:   [2.3383 ns 2.3391 ns 2.3423 ns]

SmolStr::eq len=50      time:   [2.5612 ns 2.5624 ns 2.5668 ns]

SmolStr::eq len=1000    time:   [9.1096 ns 9.1241 ns 9.1820 ns]

to_lowercase_smolstr len=12
                        time:   [66.165 ns 66.192 ns 66.300 ns]

to_lowercase_smolstr len=50
                        time:   [347.27 ns 348.20 ns 348.43 ns]

to_lowercase_smolstr len=1000
                        time:   [5.6151 µs 5.6191 µs 5.6201 µs]

to_ascii_lowercase_smolstr len=12
                        time:   [13.539 ns 13.587 ns 13.599 ns]

to_ascii_lowercase_smolstr len=50
                        time:   [99.236 ns 99.382 ns 99.966 ns]

to_ascii_lowercase_smolstr len=1000
                        time:   [1.0966 µs 1.0988 µs 1.0993 µs]

replace_smolstr len=12  time:   [23.562 ns 23.815 ns 23.878 ns]

replace_smolstr len=50  time:   [81.027 ns 81.274 ns 81.336 ns]

replace_smolstr len=1000
                        time:   [640.14 ns 640.54 ns 642.17 ns]

@alexheretic
Copy link
Contributor Author

I'll probably strip the std-string benches out of here so these are just for this crate fns.

@alexheretic alexheretic changed the title Add SmolStr vs String criterion benchmarks Add criterion benchmarks Sep 13, 2025
@alexheretic
Copy link
Contributor Author

FYI I moved my comparison with std to https://github.com/alexheretic/rust-string-bench.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant