Unnamed repository; edit this file 'description' to name the repository.
Merge rust-analyzer/smol_str#16
16: Improve `Arc<str>` creation r=matklad a=sinkuu While using `Into<String>` could avoid an allocation in `str` -> `String`, converting `Box<str>` into `Arc<str>` [deallocates it and allocates new](https://doc.rust-lang.org/1.40.0/src/alloc/sync.rs.html#778-796) anyway. The 1st commit is a drive-by simplification. Co-authored-by: Shotaro Yamada <[email protected]>
bors[bot] 2020-01-09
parent 9142ee1 · parent f7821f5 · commit 402fbec
-rw-r--r--lib/smol_str/src/lib.rs14
1 files changed, 8 insertions, 6 deletions
diff --git a/lib/smol_str/src/lib.rs b/lib/smol_str/src/lib.rs
index 9e35158177..e4cca47176 100644
--- a/lib/smol_str/src/lib.rs
+++ b/lib/smol_str/src/lib.rs
@@ -104,7 +104,7 @@ impl SmolStr {
pub fn new<T>(text: T) -> SmolStr
where
- T: Into<String> + AsRef<str>,
+ T: AsRef<str>,
{
SmolStr(Repr::new(text))
}
@@ -343,7 +343,7 @@ enum Repr {
impl Repr {
fn new<T>(text: T) -> Self
where
- T: Into<String> + AsRef<str>,
+ T: AsRef<str>,
{
{
let text = text.as_ref();
@@ -359,13 +359,15 @@ impl Repr {
}
let newlines = text.bytes().take_while(|&b| b == b'\n').count();
- let spaces = text[newlines..].bytes().take_while(|&b| b == b' ').count();
- if newlines + spaces == len && newlines <= N_NEWLINES && spaces <= N_SPACES {
- return Repr::Substring { newlines, spaces };
+ if text[newlines..].bytes().all(|b| b == b' ') {
+ let spaces = len - newlines;
+ if newlines <= N_NEWLINES && spaces <= N_SPACES {
+ return Repr::Substring { newlines, spaces };
+ }
}
}
- Repr::Heap(text.into().into_boxed_str().into())
+ Repr::Heap(text.as_ref().into())
}
#[inline(always)]