-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Benches * Bench as_ref() only * Crank up bench profile * Split the fat pointer * as_ptr is unnecessary * Skinny and Fat Cow * Fat and skinny * Recursifying 1 * Generic cursed beef! * Missing Eq derive * Working on docs * Fixed PartialEq impls with macros * Docs, comments, fixing cursed borrows * More readme * Add const_fn back * skinny -> lean * Cursed -> Lean * Removed commented code * Doc comment links * Test for soundness of beef::lean::Cow * Minor doc tweaks * Better illustrate beef::lean::Cow in readme
- Loading branch information
1 parent
64b7812
commit c1197ee
Showing
9 changed files
with
891 additions
and
397 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
#![feature(test)] | ||
|
||
extern crate beef; | ||
extern crate test; | ||
|
||
use std::borrow::{Cow as StdCow, ToOwned}; | ||
use test::{Bencher, black_box}; | ||
|
||
const NTH_WORD: usize = 4; | ||
static TEXT: &str = "In less than a half-hour, Joe had distributed ninety-two paper cups of tomato juice containing AUM, the drug that promised to turn neophobes into neophiles. He stood in Pioneer Court, just north of the Michigan Avenue Bridge, at a table from which hung a poster reading FREE TOMATO JUICE. Each person who took a cupful was invited to fill out a short questionnaire and leave it in a box on Joe's table. However, Joe explained, the questionnaire was optional, and anyone who wanted to drink the tomato juice and run was welcome to do so."; | ||
|
||
#[bench] | ||
fn beef_create(b: &mut Bencher) { | ||
use beef::Cow; | ||
|
||
let words: Vec<_> = TEXT.split_whitespace().collect(); | ||
|
||
b.iter(|| { | ||
let cow_words: Vec<Cow<str>> = words.iter().copied().map(Cow::borrowed).collect(); | ||
|
||
black_box(cow_words) | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn beef_create_mixed(b: &mut Bencher) { | ||
use beef::Cow; | ||
|
||
let words: Vec<_> = TEXT.split_whitespace().collect(); | ||
|
||
b.iter(|| { | ||
let cow_words: Vec<Cow<str>> = words.iter().copied().map(|word| { | ||
if word.len() % NTH_WORD == 0 { | ||
Cow::owned(word.to_owned()) | ||
} else { | ||
Cow::borrowed(word) | ||
} | ||
}).collect(); | ||
|
||
black_box(cow_words) | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn beef_as_ref(b: &mut Bencher) { | ||
use beef::Cow; | ||
|
||
let cow_words: Vec<_> = TEXT.split_whitespace().map(|word| { | ||
if word.len() % NTH_WORD == 0 { | ||
Cow::owned(word.to_owned()) | ||
} else { | ||
Cow::borrowed(word) | ||
} | ||
}).collect(); | ||
|
||
b.iter(|| { | ||
for word in cow_words.iter() { | ||
let word: &str = word.as_ref(); | ||
black_box(word); | ||
} | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn lean_beef_create(b: &mut Bencher) { | ||
use beef::lean::Cow; | ||
|
||
let words: Vec<_> = TEXT.split_whitespace().collect(); | ||
|
||
b.iter(|| { | ||
let cow_words: Vec<Cow<str>> = words.iter().copied().map(Cow::borrowed).collect(); | ||
|
||
black_box(cow_words) | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn lean_beef_create_mixed(b: &mut Bencher) { | ||
use beef::lean::Cow; | ||
|
||
let words: Vec<_> = TEXT.split_whitespace().collect(); | ||
|
||
b.iter(|| { | ||
let cow_words: Vec<Cow<str>> = words.iter().copied().map(|word| { | ||
if word.len() % NTH_WORD == 0 { | ||
Cow::owned(word.to_owned()) | ||
} else { | ||
Cow::borrowed(word) | ||
} | ||
}).collect(); | ||
|
||
black_box(cow_words) | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn lean_beef_as_ref(b: &mut Bencher) { | ||
use beef::lean::Cow; | ||
|
||
let cow_words: Vec<_> = TEXT.split_whitespace().map(|word| { | ||
if word.len() % NTH_WORD == 0 { | ||
Cow::owned(word.to_owned()) | ||
} else { | ||
Cow::borrowed(word) | ||
} | ||
}).collect(); | ||
|
||
b.iter(|| { | ||
for word in cow_words.iter() { | ||
let word: &str = word.as_ref(); | ||
black_box(word); | ||
} | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn std_create(b: &mut Bencher) { | ||
let words: Vec<_> = TEXT.split_whitespace().collect(); | ||
|
||
b.iter(|| { | ||
let stdcow_words: Vec<StdCow<str>> = words.iter().copied().map(StdCow::Borrowed).collect(); | ||
|
||
black_box(stdcow_words) | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn std_create_mixed(b: &mut Bencher) { | ||
let words: Vec<_> = TEXT.split_whitespace().collect(); | ||
|
||
b.iter(|| { | ||
let stdcow_words: Vec<StdCow<str>> = words.iter().copied().map(|word| { | ||
if word.len() % NTH_WORD == 0 { | ||
StdCow::Owned(word.to_owned()) | ||
} else { | ||
StdCow::Borrowed(word) | ||
} | ||
}).collect(); | ||
|
||
black_box(stdcow_words) | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn std_as_ref(b: &mut Bencher) { | ||
let stdcow_words: Vec<_> = TEXT.split_whitespace().map(|word| { | ||
if word.len() % NTH_WORD == 0 { | ||
StdCow::Owned(word.to_owned()) | ||
} else { | ||
StdCow::Borrowed(word) | ||
} | ||
}).collect(); | ||
|
||
b.iter(|| { | ||
for word in stdcow_words.iter() { | ||
let word: &str = word.as_ref(); | ||
black_box(word); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use core::num::NonZeroUsize; | ||
use core::ptr::slice_from_raw_parts_mut; | ||
use crate::traits::Capacity; | ||
|
||
/// Compact three word `Cow` that puts the ownership tag in capacity. | ||
/// This is a type alias, for documentation see [`beef::generic::Cow`](./generic/struct.Cow.html). | ||
pub type Cow<'a, T> = crate::generic::Cow<'a, T, Option<NonZeroUsize>>; | ||
|
||
impl Capacity for Option<NonZeroUsize> { | ||
type NonZero = NonZeroUsize; | ||
|
||
#[inline] | ||
fn as_ref<T>(ptr: *const [T]) -> *const [T] { | ||
ptr | ||
} | ||
|
||
#[inline] | ||
fn empty<T>(ptr: *mut T, len: usize) -> (*mut [T], Self) { | ||
(slice_from_raw_parts_mut(ptr, len), None) | ||
} | ||
|
||
#[inline] | ||
fn store<T>(ptr: *mut T, len: usize, capacity: usize) -> (*mut [T], Self) { | ||
(slice_from_raw_parts_mut(ptr, len), NonZeroUsize::new(capacity)) | ||
} | ||
|
||
#[inline] | ||
fn unpack(len: usize, capacity: NonZeroUsize) -> (usize, usize) { | ||
(len, capacity.get()) | ||
} | ||
|
||
#[inline] | ||
fn maybe(_: usize, capacity: Option<NonZeroUsize>) -> Option<NonZeroUsize> { | ||
capacity | ||
} | ||
} |
Oops, something went wrong.