News for hackers. Stuff that matters.
Re:Choosing a more optimal `String` type by mhutter on Friday September 22, 2023 @06:05AM (#37610097)
Aaah, `Cow`, the unloved child ...
Re:Choosing a more optimal `String` type by queuebert on Friday September 22, 2023 @09:16PM (#37619773)
I love Cow, except when it goes mut.
Re:Choosing a more optimal `String` type by schneems on Friday September 22, 2023 @11:12PM (#37620305)
&mut Cow<Disease>
Re:Choosing a more optimal `String` type by wavemode on Saturday September 23, 2023 @09:58AM (#37623263)
I presume they're trying to avoid having to worry about the lifetime, so they want something that owns its contents.
Re:Choosing a more optimal `String` type by cratermoon on Friday September 22, 2023 @07:41PM (#37619186)
Why use strings and pass them around in the first place? Why not limit the use of String types to system boundaries where data must be serialized into or from character-oriented streams?
Re:Choosing a more optimal `String` type by amalcon on Friday September 22, 2023 @08:02PM (#37619327)
The typical strategy for doing that in a context like this is string interning. That's literally what most of the types described in the post are doing behind the scenes, in one form or another. The post is literally about doing your suggestion -- just using a library to do so, and leveraging the type system to reduce some of the boilerplate.
Re:Choosing a more optimal `String` type by eynsham on Friday September 22, 2023 @07:53PM (#37619266)
‘More optimal' is oxymoronic; ‘better’ is just that.
Re:Choosing a more optimal `String` type by kibwen on Friday September 22, 2023 @08:32PM (#37619517)
From 1983:

Although "optimum" is an absolute term, like "unique", it became common verbal practice to make it relative: "not quite optimum" or "less optimum" or "not very optimum". Mel called the maximum time-delay locations the "most pessimum".

https://users.cs.utah.edu/~elb/folklore/mel-annotated/node1....

Re:Choosing a more optimal `String` type by eynsham on Friday September 22, 2023 @08:47PM (#37619624)
Even worse! ‘Optimum’ in English is a noun, not an adjective.
Re:Choosing a more optimal `String` type by kibwen on Saturday September 23, 2023 @12:54AM (#37620667)
From 1993:

Verbing weirds language.

https://www.gocomics.com/calvinandhobbes/1993/01/25

Re:Choosing a more optimal `String` type by swader999 on Friday September 22, 2023 @09:05PM (#37619717)
I prefer "more betterer".
Re:Choosing a more optimal `String` type by paulddraper on Saturday September 23, 2023 @12:21AM (#37620555)
And the superlative, most betterest.
Re:Choosing a more optimal `String` type by epage on Friday September 22, 2023 @08:57PM (#37619680)
These leaves off several relevant string types, including ecow which was encouraged over smol_str [0]

See https://github.com/rosetta-rs/string-rosetta-rs for some more analysis. HipStr also has a decent table [1]

[0] https://www.reddit.com/r/rust/comments/117ksvr/ecow_compact_...

[1] https://crates.io/crates/hipstr

Re:Choosing a more optimal `String` type by nmilo on Friday September 22, 2023 @09:15PM (#37619767)
There should be a from_raw_parts-like interface for Arc that can construct one out of a pointer to a T + control block. That way you can shift the string data over and stick the control block inline (which is how I assume it's laid out in memory) and convert String to Arc<str> for free (well not free, shifting takes O(n), but so does the copying in Arc::new, so in the end you do end up avoiding an allocation for free)
Re:Choosing a more optimal `String` type by LegionMammal978 on Saturday September 23, 2023 @12:35AM (#37620603)
Unfortunately, this can't really be done with how allocation works in Rust. A String is always allocated with alignment 1, but an Arc's control block requires usize alignment. An allocated pointer can't be deallocated with a different alignment, nor can its alognment be changed when reallocating. Thus, the from_raw_parts() idea would be unusable for all types with less than usize alignment.

(And the need for atomic operations on the control block precludes the idea of simply letting it be unaligned.)

Re:Choosing a more optimal `String` type by Gibbon1 on Saturday September 23, 2023 @01:00AM (#37620682)
> A String is always allocated with alignment 1, but an Arc's control block requires usize alignment.

I'll add this to my list of reasons why alignment is terrible.

Re:Choosing a more optimal `String` type by nmilo on Saturday September 23, 2023 @01:42PM (#37625502)
Wow that is annoying, especially since any malloc is going to give you 8-byte aligned allocations anyways.
Re:Choosing a more optimal `String` type by hestefisk on Saturday September 23, 2023 @01:40AM (#37620839)
Optimal is optimal. There is such thing as “more optimal”. It’s either optimal or it isn’t.
Re:Choosing a more optimal `String` type by thfuran on Saturday September 23, 2023 @05:31AM (#37621798)
Something that is optimal under some set of assumptions and under some specific conditions is likely not optimal under all conditions. Does that mean it's not optimal under any conditions? If not, is something that is optimal under more conditions not "more optimal"?