diff --git a/benches/owned.rs b/benches/owned.rs index 564ef43d..7da05863 100644 --- a/benches/owned.rs +++ b/benches/owned.rs @@ -30,30 +30,60 @@ fn collect_primitive_string(b: &mut Bencher) { } #[bench] -fn collect_complex(b: &mut Bencher) { - struct Map; +fn clone_primitive(b: &mut Bencher) { + let value = value::OwnedValue::collect(1); + + b.iter(|| { + let value = value.clone(); + black_box(value); + }) +} + +#[bench] +fn clone_primitive_string(b: &mut Bencher) { + let value = value::OwnedValue::collect("A string"); + + b.iter(|| { + let value = value.clone(); + black_box(value); + }) +} + +struct Map; - impl value::Value for Map { - fn stream(&self, stream: &mut value::Stream) -> value::Result { - stream.map_begin(None)?; +impl value::Value for Map { + fn stream(&self, stream: &mut value::Stream) -> value::Result { + stream.map_begin(None)?; - stream.map_key(1)?; + stream.map_key(1)?; - stream.map_value_begin()?.map_begin(None)?; + stream.map_value_begin()?.map_begin(None)?; - stream.map_key(2)?; + stream.map_key(2)?; - stream.map_value(42)?; + stream.map_value(42)?; - stream.map_end()?; + stream.map_end()?; - stream.map_end() - } + stream.map_end() } +} +#[bench] +fn collect_complex(b: &mut Bencher) { b.iter(|| { let value = value::OwnedValue::collect(Map); black_box(value); }); } + +#[bench] +fn clone_complex(b: &mut Bencher) { + let value = value::OwnedValue::collect(Map); + + b.iter(|| { + let value = value.clone(); + black_box(value); + }) +} diff --git a/src/test.rs b/src/test.rs index 8f9b0cc9..cdb249bd 100644 --- a/src/test.rs +++ b/src/test.rs @@ -67,7 +67,7 @@ mod std_support { Kind::Float(v) => Some(Token::Float(v)), Kind::Bool(v) => Some(Token::Bool(v)), Kind::Char(v) => Some(Token::Char(v)), - Kind::Str(ref v) => Some(Token::Str((*v).clone())), + Kind::Str(ref v) => Some(Token::Str((**v).into())), Kind::None => Some(Token::None), _ => None, }) diff --git a/src/value/owned.rs b/src/value/owned.rs index d4bbc09e..9b37209b 100644 --- a/src/value/owned.rs +++ b/src/value/owned.rs @@ -273,7 +273,7 @@ impl From for OwnedValue { fn from(v: String) -> Self { OwnedValue(ValueInner::Primitive(Token { depth: stack::Depth::root(), - kind: Kind::Str(v), + kind: Kind::Str(v.into()), })) } } @@ -293,7 +293,7 @@ pub(crate) enum Kind { BigSigned(i128), BigUnsigned(u128), Bool(bool), - Str(String), + Str(Arc), Char(char), None, } @@ -365,7 +365,7 @@ impl Stream for Buf { fn fmt(&mut self, f: stream::Arguments) -> stream::Result { let depth = self.stack.primitive()?.depth(); - self.push(Kind::Str(f.to_string()), depth); + self.push(Kind::Str(Arc::from(f.to_string())), depth); Ok(()) } @@ -429,7 +429,7 @@ impl Stream for Buf { fn str(&mut self, v: &str) -> stream::Result { let depth = self.stack.primitive()?.depth(); - self.push(Kind::Str(v.to_string()), depth); + self.push(Kind::Str(Arc::from(v)), depth); Ok(()) } @@ -527,7 +527,7 @@ impl Stream for Primitive { fn fmt(&mut self, f: stream::Arguments) -> stream::Result { let depth = self.stack.primitive()?.depth(); - self.set(Kind::Str(f.to_string()), depth); + self.set(Kind::Str(Arc::from(f.to_string())), depth); Ok(()) } @@ -591,7 +591,7 @@ impl Stream for Primitive { fn str(&mut self, v: &str) -> stream::Result { let depth = self.stack.primitive()?.depth(); - self.set(Kind::Str(v.to_string()), depth); + self.set(Kind::Str(Arc::from(v)), depth); Ok(()) }