Skip to content

Commit 5dec9ce

Browse files
committed
Defer initial call to next + lots of inline tags
1 parent d8aebea commit 5dec9ce

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

library/core/src/iter/adapters/dedup.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub struct ByKey<F> {
1919
}
2020

2121
impl<F> ByKey<F> {
22+
#[inline]
2223
pub(crate) fn new(key: F) -> Self {
2324
Self { key }
2425
}
@@ -31,7 +32,7 @@ where
3132
K: PartialEq,
3233
{
3334
type Output = bool;
34-
35+
#[inline]
3536
extern "rust-call" fn call_once(mut self, args: (&T, &T)) -> Self::Output {
3637
(self.key)(args.0) == (self.key)(args.1)
3738
}
@@ -43,6 +44,7 @@ where
4344
F: FnMut(&T) -> K,
4445
K: PartialEq,
4546
{
47+
#[inline]
4648
extern "rust-call" fn call_mut(&mut self, args: (&T, &T)) -> Self::Output {
4749
(self.key)(args.0) == (self.key)(args.1)
4850
}
@@ -62,6 +64,7 @@ where
6264
pub struct ByPartialEq;
6365

6466
impl ByPartialEq {
67+
#[inline]
6568
pub(crate) fn new() -> Self {
6669
Self
6770
}
@@ -70,14 +73,15 @@ impl ByPartialEq {
7073
#[unstable(feature = "iter_dedup", reason = "recently added", issue = "83748")]
7174
impl<T: PartialEq> FnOnce<(&T, &T)> for ByPartialEq {
7275
type Output = bool;
73-
76+
#[inline]
7477
extern "rust-call" fn call_once(self, args: (&T, &T)) -> Self::Output {
7578
args.0 == args.1
7679
}
7780
}
7881

7982
#[unstable(feature = "iter_dedup", reason = "recently added", issue = "83748")]
8083
impl<T: PartialEq> FnMut<(&T, &T)> for ByPartialEq {
84+
#[inline]
8185
extern "rust-call" fn call_mut(&mut self, args: (&T, &T)) -> Self::Output {
8286
args.0 == args.1
8387
}
@@ -93,23 +97,23 @@ impl<T: PartialEq> FnMut<(&T, &T)> for ByPartialEq {
9397
/// [`Iterator::dedup_by`]: Iterator::dedup_by
9498
/// [`Iterator::dedup_by_key`]: Iterator::dedup_by_key
9599
#[unstable(feature = "iter_dedup", reason = "recently added", issue = "83748")]
96-
#[derive(Debug, Clone, Copy)]
100+
#[derive(Debug, Clone)]
97101
pub struct Dedup<I, F>
98102
where
99103
I: Iterator,
100104
{
101105
inner: I,
102106
same_bucket: F,
103-
last: Option<I::Item>,
107+
last: Option<Option<I::Item>>,
104108
}
105109

106110
impl<I, F> Dedup<I, F>
107111
where
108112
I: Iterator,
109113
{
114+
#[inline]
110115
pub(crate) fn new(inner: I, same_bucket: F) -> Self {
111-
let mut inner = inner;
112-
Self { last: inner.next(), inner, same_bucket }
116+
Self { inner, same_bucket, last: None }
113117
}
114118
}
115119

@@ -121,8 +125,15 @@ where
121125
{
122126
type Item = I::Item;
123127

128+
#[inline]
124129
fn next(&mut self) -> Option<Self::Item> {
125-
let last_item = self.last.as_ref()?;
130+
if self.last.is_none() {
131+
self.last = Some(self.inner.next())
132+
}
133+
134+
let last = self.last.as_mut().unwrap();
135+
let last_item = last.as_ref()?;
136+
126137
let mut next = loop {
127138
let curr = self.inner.next();
128139
if let Some(curr_item) = &curr {
@@ -134,10 +145,11 @@ where
134145
}
135146
};
136147

137-
swap(&mut self.last, &mut next);
148+
swap(last, &mut next);
138149
next
139150
}
140151

152+
#[inline]
141153
fn size_hint(&self) -> (usize, Option<usize>) {
142154
let min = self.last.as_ref().map(|_| 1).unwrap_or(0);
143155
let max = self.inner.size_hint().1;
@@ -153,6 +165,7 @@ where
153165
{
154166
type Source = S;
155167

168+
#[inline]
156169
unsafe fn as_inner(&mut self) -> &mut Self::Source {
157170
// SAFETY: unsafe function forwarding to unsafe function with the same requirements
158171
unsafe { SourceIter::as_inner(&mut self.inner) }

0 commit comments

Comments
 (0)