Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove SmallStr and recommend Cow<str> #1417

Merged
merged 1 commit into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions components/datetime/src/skeleton/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use smallvec::SmallVec;
/// The `Field`s are only sorted in the [`Skeleton`] in order to provide a deterministic
/// serialization strategy, and to provide a faster [`Skeleton`] matching operation.
#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd)]
// TODO(#876): Use ZeroVec instead of SmallVec
pub struct Skeleton(pub(crate) SmallVec<[fields::Field; 5]>);

impl Skeleton {
Expand Down
14 changes: 5 additions & 9 deletions docs/process/style_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -634,18 +634,14 @@ In addition to supporting zero-copy deserialization, data structs should also su

### Conventions for strings in structs :: suggested

Main issue: [#113](https://github.com/unicode-org/icu4x/issues/113)
Main issue: [#113](https://github.com/unicode-org/icu4x/issues/113), [#571](https://github.com/unicode-org/icu4x/issues/571)

When structs with public string fields contain strings, use the following type conventions:
When structs with public fields contain strings, use the following type conventions:

- `&'data str` if the struct does not need to own the string.
- `Cow<'data, str>` if the string could be borrowed or owned.
- `Cow<'data, str>` for data provider structs (those with `'data`).
- `String` for source data structs (with no lifetime).
- `&str` if the struct does not need to own the string.
- [TinyStr](https://github.com/zbraniecki/tinystr) if the string is ASCII-only.
- [SmallString](https://crates.io/crates/smallstr) for shorter strings, with a stack size ∈ {8, 12, 16, 20} that fits a large majority of cases.

For `SmallString`, when determining what constitutes a "large majority of cases", consider 99% as a rule of thumb. If in doubt, start with `Cow<'data, str>`; `SmallString` is an additional optimization when the strings are almost always short. Another advantage of `SmallString` is the lack of a lifetime parameter. The stack sizes of 8, 12, 16, and 20 for `SmallString` were chosen because these are the sizes that cause SmallString to have a round number of 32-bit-aligned stack bytes.

In general, use `Cow<'data, str>` instead of `String` in structs if the struct can often use zero-cost construction. Use `String` if the struct always owns its data, such as when the string is always dynamically generated at runtime, or if a lifetime parameter cannot be used ergonomically.

### Pre-validation of options :: suggested

Expand Down