Seems that taking a reference is actually faster

pull/585/head
Isaac Mills 2 years ago
parent 3a4590daec
commit 5364b49959
No known key found for this signature in database
GPG Key ID: B67D7410F33A0F61

@ -256,7 +256,7 @@ impl<'a> Layout<'a> {
/// );
/// ```
pub fn split(self, area: Rect) -> Vec<Rect> {
pub fn split(&self, area: Rect) -> Vec<Rect> {
// TODO: Maybe use a fixed size cache ?
LAYOUT_CACHE.with(|c| {
c.borrow_mut()
@ -266,7 +266,9 @@ impl<'a> Layout<'a> {
})
}
pub fn split_ref(self, area: Rect) -> &'static Vec<Rect> {
/// Much faster than
pub fn split_ref(&self, area: Rect) -> &'static Vec<Rect> {
LAYOUT_CACHE.with(|c| {
// SAFETY:
// "c" is stored in a static variable
@ -282,7 +284,7 @@ impl<'a> Layout<'a> {
}
}
fn split(area: Rect, layout: Layout) -> Vec<Rect> {
fn split(area: Rect, layout: &Layout) -> Vec<Rect> {
let mut solver = Solver::new();
let mut vars: HashMap<Variable, (usize, usize)> = HashMap::new();
let elements = layout
@ -597,6 +599,29 @@ mod tests {
chunks.windows(2).for_each(|w| assert!(w[0].y <= w[1].y));
}
#[bench]
fn bench_vertical_split_by_height(b: &mut test::Bencher) {
let target = Rect {
x: 2,
y: 2,
width: 10,
height: 10,
};
let layout = Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Percentage(10),
Constraint::Max(5),
Constraint::Min(1),
]
.as_ref(),
);
b.iter(|| layout.split_ref(target));
}
#[test]
fn test_vertical_split_by_height_ref() {
let target = Rect {

@ -160,6 +160,8 @@
//! default the computed layout tries to fill the available space completely. So if for any reason
//! you might need a blank space somewhere, try to pass an additional constraint and don't use the
//! corresponding area.
#![feature(test)]
extern crate test;
pub mod backend;
pub mod buffer;

Loading…
Cancel
Save