Skip to content

Is String allowed to switch to a small-string optimization? #499

Open
@Manishearth

Description

@Manishearth

In theory, String could implement a small-string optimization where in certain cases the string is stored inline and Deref returns a pointer to the flat in-memory representation.

The only thing that really changes here would be the behavior of into_raw_parts(), it would be forced to perform a last minute allocation.

And that would potentially break Rust code that was calling this function with an older pointer.

It also of course breaks StableDeref, though there are already open questions about that.

Concretely, I have the following questions:

  • Would it make sense to be explicit about this on the invariants of String, such that people maintaining a copy of the stdlib can make such a change, and/or potentially contribute it upstream (making no judgement on whether SSOs are a good idea for Rust in general, just whether such a change should be allowed by Rust's rules)
  • Would it make sense to have a miri check that ensures that pointers obtained via Deref are not valid for from_raw_parts, only ones from into_raw_parts()?

even if we decide that StableDeref is an important property to maintain here, it might be worth getting unsafe code to not rely on into_raw_parts()' behavior, so that people tweaking the stdlib can still have some hope of having this work (e.g. "as long as we patch out impl StableDeref for String, we are fine")

Anyway, curious about thoughts.

cc @gribozavr

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions