I needed to create this thread, since I haven’t found too much dicussions on that topic.
SSO is an important optimization in modern languages. To give an example: More than half of all allocations in the chromium browser are std::string allocations . I don’t have a study or article around to prove the statement, but: You can easily imagine that many strings in applications won’t exceed a certain length. Storing those strings without allocating in the stack-object itself would be a huge gain in performance for applications using large numbers of short strings.
On 64-Bit architectures it’s possible to store strings with a length up to 23 bytes inside the object. 23 bytes is a whole lot when you consider what types of strings are shorter than that.
Therefore I think that Rust needs this optimization or at least the library should be able to use the optimization. I was told that optimization stuff doesn’t matter too much in this time, in which the language devs are trying to stabilize the language.
BUT: The current interface of
std::string::String prevents the usage of that implementation.
For example: The method
as_mut_vec would prevent SSO. It returns a mutable reference to a Vec which means that this Vec has to be stored somewhere. This is not possible when using SSO. Every solution to this problem involves a heap allocation which is NOT what you want.
I’m not sure if there are some other methods that would prevent SSO, but I will find out.
TL;DR: I propose to rethink the interface of
std::string::String with SSO in mind. We can’t afford to prevent ourselfs from using this optimization.
And btw: I have a working implementation of
std::string::String that uses SSO. It is missing a few methods and implementations but most functionality is ready.