Skip to content

Commit

Permalink
ensure cloning a string in OwnedValue is cheap (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus authored Sep 30, 2019
1 parent 021dc9d commit 1968324
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
54 changes: 42 additions & 12 deletions benches/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
}
2 changes: 1 addition & 1 deletion src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down
12 changes: 6 additions & 6 deletions src/value/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ impl From<String> for OwnedValue {
fn from(v: String) -> Self {
OwnedValue(ValueInner::Primitive(Token {
depth: stack::Depth::root(),
kind: Kind::Str(v),
kind: Kind::Str(v.into()),
}))
}
}
Expand All @@ -293,7 +293,7 @@ pub(crate) enum Kind {
BigSigned(i128),
BigUnsigned(u128),
Bool(bool),
Str(String),
Str(Arc<str>),
Char(char),
None,
}
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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(())
}
Expand Down

0 comments on commit 1968324

Please sign in to comment.