summaryrefslogtreecommitdiffstats
path: root/third_party/rust/itertools/benches/fold_specialization.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /third_party/rust/itertools/benches/fold_specialization.rs
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/itertools/benches/fold_specialization.rs')
-rw-r--r--third_party/rust/itertools/benches/fold_specialization.rs73
1 files changed, 73 insertions, 0 deletions
diff --git a/third_party/rust/itertools/benches/fold_specialization.rs b/third_party/rust/itertools/benches/fold_specialization.rs
new file mode 100644
index 0000000000..5de4671e98
--- /dev/null
+++ b/third_party/rust/itertools/benches/fold_specialization.rs
@@ -0,0 +1,73 @@
+use criterion::{criterion_group, criterion_main, Criterion};
+use itertools::Itertools;
+
+struct Unspecialized<I>(I);
+
+impl<I> Iterator for Unspecialized<I>
+where I: Iterator
+{
+ type Item = I::Item;
+
+ #[inline(always)]
+ fn next(&mut self) -> Option<Self::Item> {
+ self.0.next()
+ }
+
+ #[inline(always)]
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ self.0.size_hint()
+ }
+}
+
+mod specialization {
+ use super::*;
+
+ pub mod intersperse {
+ use super::*;
+
+ pub fn external(c: &mut Criterion)
+ {
+ let arr = [1; 1024];
+
+ c.bench_function("external", move |b| {
+ b.iter(|| {
+ let mut sum = 0;
+ for &x in arr.iter().intersperse(&0) {
+ sum += x;
+ }
+ sum
+ })
+ });
+ }
+
+ pub fn internal_specialized(c: &mut Criterion)
+ {
+ let arr = [1; 1024];
+
+ c.bench_function("internal specialized", move |b| {
+ b.iter(|| {
+ arr.iter().intersperse(&0).fold(0, |acc, x| acc + x)
+ })
+ });
+ }
+
+ pub fn internal_unspecialized(c: &mut Criterion)
+ {
+ let arr = [1; 1024];
+
+ c.bench_function("internal unspecialized", move |b| {
+ b.iter(|| {
+ Unspecialized(arr.iter().intersperse(&0)).fold(0, |acc, x| acc + x)
+ })
+ });
+ }
+ }
+}
+
+criterion_group!(
+ benches,
+ specialization::intersperse::external,
+ specialization::intersperse::internal_specialized,
+ specialization::intersperse::internal_unspecialized,
+);
+criterion_main!(benches);