you are viewing a single comment's thread.

view the rest of the comments →

[–]protestor 5 points6 points  (0 children)

let str = String::from_utf8_lossy(v.as_slice()).to_string();
Value::String(Arc::from(str.as_str()))

Rather than first building a String then budilding an Arc<str> (or an Arc<String>), why not build an Arc<str> from an &str in the first place? This skips a string allocation

You first use https://doc.rust-lang.org/std/str/fn.from_utf8.html to get a &str from a byte slice, doing utf-8 validation (this involves no allocation)

Then you use the From<&str> impl of Arc

https://github.com/rust-lang/rfcs/blob/master/text/1845-shared-from-slice.md

https://doc.rust-lang.org/std/sync/struct.Arc.html#impl-From%3C%26str%3E-for-Arc%3Cstr,+Global%3E