# Feature request: make every String a smartstring

**URL:** <https://internals.rust-lang.org/t/feature-request-make-every-string-a-smartstring/19184>\
**Category:** libs\
**Created:** [July 20, 2023, 11:02am UTC](https://internals.rust-lang.org/t/feature-request-make-every-string-a-smartstring/19184 "2023-07-20T11:02:32Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![newpavlov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/newpavlov/32/3290_2.png) [@newpavlov](https://internals.rust-lang.org/u/newpavlov)\
**Post date:** [July 20, 2023, 12:35pm UTC](https://internals.rust-lang.org/t/feature-request-make-every-string-a-smartstring/19184/4 "2023-07-20T12:35:33Z")

</div>

I am not sure whether implementing the small string optimization in `std` is a good idea or not, but I think it should be possible to do in a backwards compatible way. Unfortunately, breaking of a bunch of crates which rely on unsound assumptions about `String` internals along the way.

According to my vague memories, `std` does not have the optimization because:

- It simplifies the code base.
- Behavior and performance of strings does not depend on compilation target.
- Code does not have branching during string data access.

> [@Nokel81](#):
>
> I don't think this is possible because the layout of `String` is already guaranteed and thus cannot be changed due to backwards compatibility guarantees.

Can you link documentation which specifies those guaranties? I don't remember anything like this. If anything, the [IP address debacle](https://github.com/rust-lang/rust/pull/78802) would point in the other direction. The closest thing is the [representation section](https://doc.rust-lang.org/std/string/struct.String.html#representation), but it only says that `String` has a pointer, a length, and a capacity, it has no restrictions on potentially implementing the small string optimization.

> [@steffahn](#):
>
> E. g. there's a good amount of crates relying on `String` having a stable address. [stable\_deref\_trait::StableDeref - Rust](https://docs.rs/stable_deref_trait/1.2.0/stable_deref_trait/trait.StableDeref.html)

I think such reliance is quite suspicious and AFAIK, strictly speaking, is unsound.

Relevant older topic:

> [@Short String optimization](https://internals.rust-lang.org/t/8436):
>
> Has there been any discussion of having String live entirely on the stack for len \< size\_of::\<usize\>() \* n? Imagine enum StringInner { Stack { len: u8, bytes: [u8; N], }, Heap(Vec\<u8\>), } Of course, off the top of my head I don’t know how well this plays with existing APIs, but I wanted to put down the idea for discussion before I mull it over more. Note that this is the same size of the current String, if we use StringInner::Heap.0's RawVec's Unique as a niche. I …

---

_[View the full topic](https://internals.rust-lang.org/t/feature-request-make-every-string-a-smartstring/19184)._
