-
-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A new stack implementation that should be faster and less space-consu…
…ming (#905) * Benchmark and test Stack * A new implementation of Stack. * Implement Default for Stack. To make clippy happy. * Remove redundant clone.
- Loading branch information
1 parent
fe53557
commit 734ba66
Showing
4 changed files
with
233 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// pest. The Elegant Parser | ||
// Copyright (c) 2018 Dragoș Tiselice | ||
// | ||
// Licensed under the Apache License, Version 2.0 | ||
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT | ||
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. All files in the project carrying such notice may not be copied, | ||
// modified, or distributed except according to those terms. | ||
|
||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use pest::Stack; | ||
|
||
fn snapshot_push_restore<T: Clone>(elements: impl Iterator<Item = T> + Clone) { | ||
let mut stack = Stack::<T>::new(); | ||
for elem in elements { | ||
stack.snapshot(); | ||
stack.push(elem.clone()); | ||
stack.restore(); | ||
stack.push(elem); | ||
} | ||
} | ||
|
||
fn snapshot_push_clear_snapshot<T: Clone>(elements: impl Iterator<Item = T> + Clone) { | ||
let mut stack = Stack::<T>::new(); | ||
for elem in elements { | ||
stack.snapshot(); | ||
stack.push(elem); | ||
stack.clear_snapshot(); | ||
} | ||
} | ||
|
||
fn snapshot_pop_restore<T: Clone>(elements: impl Iterator<Item = T>) { | ||
let mut stack = Stack::<T>::new(); | ||
for elem in elements { | ||
stack.push(elem); | ||
} | ||
while !stack.is_empty() { | ||
stack.snapshot(); | ||
stack.pop(); | ||
stack.restore(); | ||
stack.pop(); | ||
} | ||
} | ||
|
||
fn snapshot_pop_clear<T: Clone>(elements: impl Iterator<Item = T>) { | ||
let mut stack = Stack::<T>::new(); | ||
for elem in elements { | ||
stack.push(elem); | ||
} | ||
while !stack.is_empty() { | ||
stack.snapshot(); | ||
stack.pop(); | ||
stack.clear_snapshot(); | ||
} | ||
} | ||
|
||
fn benchmark(b: &mut Criterion) { | ||
use core::iter::repeat; | ||
// use criterion::black_box; | ||
let times = 10000usize; | ||
let small = 0..times; | ||
let medium = ("", 0usize, 1usize); | ||
let medium = repeat(medium).take(times); | ||
let large = [""; 64]; | ||
let large = repeat(large).take(times); | ||
macro_rules! test_series { | ||
($kind:ident) => { | ||
b.bench_function(stringify!(push - restore - $kind), |b| { | ||
b.iter(|| snapshot_push_restore($kind.clone())) | ||
}) | ||
.bench_function(stringify!(push - clear - $kind), |b| { | ||
b.iter(|| snapshot_push_clear_snapshot($kind.clone())) | ||
}) | ||
.bench_function(stringify!(pop - restore - $kind), |b| { | ||
b.iter(|| snapshot_pop_restore($kind.clone())) | ||
}) | ||
.bench_function(stringify!(pop - clear - $kind), |b| { | ||
b.iter(|| snapshot_pop_clear($kind.clone())) | ||
}) | ||
}; | ||
} | ||
test_series!(small); | ||
test_series!(medium); | ||
test_series!(large); | ||
} | ||
|
||
criterion_group!(benchmarks, benchmark); | ||
criterion_main!(benchmarks); |
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