Skip to content

Commit

Permalink
Use Cow<'a, str> to reduce clones sile#52
Browse files Browse the repository at this point in the history
  • Loading branch information
Luro02 committed Apr 22, 2020
1 parent 3b0017a commit 6020cd5
Show file tree
Hide file tree
Showing 40 changed files with 1,045 additions and 578 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ thiserror = "1.0"
derive_more = "0.99"
shorthand = "0.1"
strum = { version = "0.17", features = ["derive"] }

stable-vec = { version = "0.4" }

[dev-dependencies]
Expand Down
19 changes: 17 additions & 2 deletions benches/benchmarks/media_playlist.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::convert::TryFrom;
use std::str::FromStr;
use std::time::Duration;

Expand Down Expand Up @@ -58,7 +59,7 @@ fn create_manifest_data() -> Vec<u8> {
builder.build().unwrap().to_string().into_bytes()
}

fn criterion_benchmark(c: &mut Criterion) {
fn media_playlist_from_str(c: &mut Criterion) {
let data = String::from_utf8(create_manifest_data()).unwrap();

let mut group = c.benchmark_group("MediaPlaylist::from_str");
Expand All @@ -72,4 +73,18 @@ fn criterion_benchmark(c: &mut Criterion) {
group.finish();
}

criterion_group!(benches, criterion_benchmark);
fn media_playlist_try_from(c: &mut Criterion) {
let data = String::from_utf8(create_manifest_data()).unwrap();

let mut group = c.benchmark_group("MediaPlaylist::try_from");

group.throughput(Throughput::Bytes(data.len() as u64));

group.bench_function("MediaPlaylist::try_from", |b| {
b.iter(|| MediaPlaylist::try_from(black_box(data.as_str())).unwrap());
});

group.finish();
}

criterion_group!(benches, media_playlist_from_str, media_playlist_try_from);
4 changes: 2 additions & 2 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<'a> Iterator for AttributePairs<'a> {
// NOTE: it is okay to add 1 to the index, because an `=` is exactly 1 byte.
self.index = end + 1;

::core::str::from_utf8(&self.string.as_bytes()[start..end]).unwrap()
&self.string[start..end]
};

let value = {
Expand Down Expand Up @@ -66,7 +66,7 @@ impl<'a> Iterator for AttributePairs<'a> {
self.index += end;
self.index -= start;

::core::str::from_utf8(&self.string.as_bytes()[start..end]).unwrap()
&self.string[start..end]
};

Some((key, value))
Expand Down
25 changes: 14 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,22 @@
//!
//! ```
//! use hls_m3u8::MediaPlaylist;
//! use std::convert::TryFrom;
//!
//! let m3u8 = "#EXTM3U
//! #EXT-X-TARGETDURATION:10
//! #EXT-X-VERSION:3
//! #EXTINF:9.009,
//! http://media.example.com/first.ts
//! #EXTINF:9.009,
//! http://media.example.com/second.ts
//! #EXTINF:3.003,
//! http://media.example.com/third.ts
//! #EXT-X-ENDLIST";
//! let m3u8 = MediaPlaylist::try_from(concat!(
//! "#EXTM3U\n",
//! "#EXT-X-TARGETDURATION:10\n",
//! "#EXT-X-VERSION:3\n",
//! "#EXTINF:9.009,\n",
//! "http://media.example.com/first.ts\n",
//! "#EXTINF:9.009,\n",
//! "http://media.example.com/second.ts\n",
//! "#EXTINF:3.003,\n",
//! "http://media.example.com/third.ts\n",
//! "#EXT-X-ENDLIST",
//! ));
//!
//! assert!(m3u8.parse::<MediaPlaylist>().is_ok());
//! assert!(m3u8.is_ok());
//! ```
//!
//! ## Crate Feature Flags
Expand Down
62 changes: 31 additions & 31 deletions src/line.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use core::convert::TryFrom;
use core::iter::FusedIterator;
use core::str::FromStr;

use derive_more::Display;

Expand All @@ -23,7 +22,8 @@ impl<'a> Iterator for Lines<'a> {
let uri = self.lines.next()?;

Some(
tags::VariantStream::from_str(&format!("{}\n{}", line, uri))
tags::VariantStream::try_from(format!("{}\n{}", line, uri).as_str())
.map(|v| v.into_owned())
.map(|v| Line::Tag(Tag::VariantStream(v))),
)
} else if line.starts_with("#EXT") {
Expand Down Expand Up @@ -60,25 +60,25 @@ pub(crate) enum Line<'a> {
#[display(fmt = "{}")]
pub(crate) enum Tag<'a> {
ExtXVersion(tags::ExtXVersion),
ExtInf(tags::ExtInf),
ExtInf(tags::ExtInf<'a>),
ExtXByteRange(tags::ExtXByteRange),
ExtXDiscontinuity(tags::ExtXDiscontinuity),
ExtXKey(tags::ExtXKey),
ExtXMap(tags::ExtXMap),
ExtXProgramDateTime(tags::ExtXProgramDateTime),
ExtXDateRange(tags::ExtXDateRange),
ExtXKey(tags::ExtXKey<'a>),
ExtXMap(tags::ExtXMap<'a>),
ExtXProgramDateTime(tags::ExtXProgramDateTime<'a>),
ExtXDateRange(tags::ExtXDateRange<'a>),
ExtXTargetDuration(tags::ExtXTargetDuration),
ExtXMediaSequence(tags::ExtXMediaSequence),
ExtXDiscontinuitySequence(tags::ExtXDiscontinuitySequence),
ExtXEndList(tags::ExtXEndList),
PlaylistType(PlaylistType),
ExtXIFramesOnly(tags::ExtXIFramesOnly),
ExtXMedia(tags::ExtXMedia),
ExtXSessionData(tags::ExtXSessionData),
ExtXSessionKey(tags::ExtXSessionKey),
ExtXMedia(tags::ExtXMedia<'a>),
ExtXSessionData(tags::ExtXSessionData<'a>),
ExtXSessionKey(tags::ExtXSessionKey<'a>),
ExtXIndependentSegments(tags::ExtXIndependentSegments),
ExtXStart(tags::ExtXStart),
VariantStream(tags::VariantStream),
VariantStream(tags::VariantStream<'a>),
Unknown(&'a str),
}

Expand All @@ -87,47 +87,47 @@ impl<'a> TryFrom<&'a str> for Tag<'a> {

fn try_from(input: &'a str) -> Result<Self, Self::Error> {
if input.starts_with(tags::ExtXVersion::PREFIX) {
input.parse().map(Self::ExtXVersion)
TryFrom::try_from(input).map(Self::ExtXVersion)
} else if input.starts_with(tags::ExtInf::PREFIX) {
input.parse().map(Self::ExtInf)
TryFrom::try_from(input).map(Self::ExtInf)
} else if input.starts_with(tags::ExtXByteRange::PREFIX) {
input.parse().map(Self::ExtXByteRange)
TryFrom::try_from(input).map(Self::ExtXByteRange)
} else if input.starts_with(tags::ExtXDiscontinuity::PREFIX) {
input.parse().map(Self::ExtXDiscontinuity)
TryFrom::try_from(input).map(Self::ExtXDiscontinuity)
} else if input.starts_with(tags::ExtXKey::PREFIX) {
input.parse().map(Self::ExtXKey)
TryFrom::try_from(input).map(Self::ExtXKey)
} else if input.starts_with(tags::ExtXMap::PREFIX) {
input.parse().map(Self::ExtXMap)
TryFrom::try_from(input).map(Self::ExtXMap)
} else if input.starts_with(tags::ExtXProgramDateTime::PREFIX) {
input.parse().map(Self::ExtXProgramDateTime)
TryFrom::try_from(input).map(Self::ExtXProgramDateTime)
} else if input.starts_with(tags::ExtXTargetDuration::PREFIX) {
input.parse().map(Self::ExtXTargetDuration)
TryFrom::try_from(input).map(Self::ExtXTargetDuration)
} else if input.starts_with(tags::ExtXDateRange::PREFIX) {
input.parse().map(Self::ExtXDateRange)
TryFrom::try_from(input).map(Self::ExtXDateRange)
} else if input.starts_with(tags::ExtXMediaSequence::PREFIX) {
input.parse().map(Self::ExtXMediaSequence)
TryFrom::try_from(input).map(Self::ExtXMediaSequence)
} else if input.starts_with(tags::ExtXDiscontinuitySequence::PREFIX) {
input.parse().map(Self::ExtXDiscontinuitySequence)
TryFrom::try_from(input).map(Self::ExtXDiscontinuitySequence)
} else if input.starts_with(tags::ExtXEndList::PREFIX) {
input.parse().map(Self::ExtXEndList)
TryFrom::try_from(input).map(Self::ExtXEndList)
} else if input.starts_with(PlaylistType::PREFIX) {
input.parse().map(Self::PlaylistType)
TryFrom::try_from(input).map(Self::PlaylistType)
} else if input.starts_with(tags::ExtXIFramesOnly::PREFIX) {
input.parse().map(Self::ExtXIFramesOnly)
TryFrom::try_from(input).map(Self::ExtXIFramesOnly)
} else if input.starts_with(tags::ExtXMedia::PREFIX) {
input.parse().map(Self::ExtXMedia)
TryFrom::try_from(input).map(Self::ExtXMedia)
} else if input.starts_with(tags::VariantStream::PREFIX_EXTXIFRAME)
|| input.starts_with(tags::VariantStream::PREFIX_EXTXSTREAMINF)
{
input.parse().map(Self::VariantStream)
TryFrom::try_from(input).map(Self::VariantStream)
} else if input.starts_with(tags::ExtXSessionData::PREFIX) {
input.parse().map(Self::ExtXSessionData)
TryFrom::try_from(input).map(Self::ExtXSessionData)
} else if input.starts_with(tags::ExtXSessionKey::PREFIX) {
input.parse().map(Self::ExtXSessionKey)
TryFrom::try_from(input).map(Self::ExtXSessionKey)
} else if input.starts_with(tags::ExtXIndependentSegments::PREFIX) {
input.parse().map(Self::ExtXIndependentSegments)
TryFrom::try_from(input).map(Self::ExtXIndependentSegments)
} else if input.starts_with(tags::ExtXStart::PREFIX) {
input.parse().map(Self::ExtXStart)
TryFrom::try_from(input).map(Self::ExtXStart)
} else {
Ok(Self::Unknown(input))
}
Expand Down
Loading

0 comments on commit 6020cd5

Please sign in to comment.