Skip to content

Commit d5b86d5

Browse files
authored
Rollup merge of #101690 - kadiwa4:avoid_iterator_last, r=oli-obk
Avoid `Iterator::last` Adapters like `Filter` and `Map` use the default implementation of `Iterator::last` which is not short-circuiting (and so does `core::str::Split`). The predicate function will be run for every single item of the underlying iterator. I hope that removing those calls to `last` results in slight performance improvements.
2 parents db75d7e + 66211d8 commit d5b86d5

File tree

7 files changed

+11
-15
lines changed

7 files changed

+11
-15
lines changed

compiler/rustc_borrowck/src/location.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ impl LocationTable {
8686
let (block, &first_index) = self
8787
.statements_before_block
8888
.iter_enumerated()
89-
.filter(|(_, first_index)| **first_index <= point_index)
90-
.last()
89+
.rfind(|&(_, &first_index)| first_index <= point_index)
9190
.unwrap();
9291

9392
let statement_index = (point_index - first_index) / 2;

compiler/rustc_hir/src/hir.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -576,8 +576,7 @@ impl<'hir> Generics<'hir> {
576576
if self.has_where_clause_predicates {
577577
self.predicates
578578
.iter()
579-
.filter(|p| p.in_where_clause())
580-
.last()
579+
.rfind(|&p| p.in_where_clause())
581580
.map_or(end, |p| p.span())
582581
.shrink_to_hi()
583582
.to(end)

compiler/rustc_lint/src/types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ fn get_nullable_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'t
719719
Some(match *ty.kind() {
720720
ty::Adt(field_def, field_substs) => {
721721
let inner_field_ty = {
722-
let first_non_zst_ty = field_def
722+
let mut first_non_zst_ty = field_def
723723
.variants()
724724
.iter()
725725
.filter_map(|v| transparent_newtype_field(cx.tcx, v));
@@ -729,7 +729,7 @@ fn get_nullable_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'t
729729
"Wrong number of fields for transparent type"
730730
);
731731
first_non_zst_ty
732-
.last()
732+
.next_back()
733733
.expect("No non-zst fields in transparent type.")
734734
.ty(tcx, field_substs)
735735
};

compiler/rustc_resolve/src/late/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -617,9 +617,9 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
617617
.filter(|&sp| sp != base_error.span)
618618
.collect();
619619

620-
let start_span = bounds.iter().map(|bound| bound.span()).next().unwrap();
620+
let start_span = bounds[0].span();
621621
// `end_span` is the end of the poly trait ref (Foo + 'baz + Bar><)
622-
let end_span = bounds.iter().map(|bound| bound.span()).last().unwrap();
622+
let end_span = bounds.last().unwrap().span();
623623
// `last_bound_span` is the last bound of the poly trait ref (Foo + >'baz< + Bar)
624624
let last_bound_span = spans.last().cloned().unwrap();
625625
let mut multi_span: MultiSpan = spans.clone().into();

compiler/rustc_session/src/session.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1309,10 +1309,8 @@ pub fn build_session(
13091309
let warnings_allow = sopts
13101310
.lint_opts
13111311
.iter()
1312-
.filter(|&&(ref key, _)| *key == "warnings")
1313-
.map(|&(_, ref level)| *level == lint::Allow)
1314-
.last()
1315-
.unwrap_or(false);
1312+
.rfind(|&&(ref key, _)| *key == "warnings")
1313+
.map_or(false, |&(_, level)| level == lint::Allow);
13161314
let cap_lints_allow = sopts.lint_cap.map_or(false, |cap| cap == lint::Allow);
13171315
let can_emit_warnings = !(warnings_allow || cap_lints_allow);
13181316

src/librustdoc/html/format.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ fn generate_macro_def_id_path(
587587
}
588588
})
589589
.collect();
590-
let relative = fqp.iter().map(|elem| elem.to_string());
590+
let mut relative = fqp.iter().map(|elem| elem.to_string());
591591
let cstore = CStore::from_tcx(tcx);
592592
// We need this to prevent a `panic` when this function is used from intra doc links...
593593
if !cstore.has_crate_data(def_id.krate) {
@@ -607,7 +607,7 @@ fn generate_macro_def_id_path(
607607
let mut path = if is_macro_2 {
608608
once(crate_name.clone()).chain(relative).collect()
609609
} else {
610-
vec![crate_name.clone(), relative.last().unwrap()]
610+
vec![crate_name.clone(), relative.next_back().unwrap()]
611611
};
612612
if path.len() < 2 {
613613
// The minimum we can have is the crate name followed by the macro name. If shorter, then

src/tools/compiletest/src/runtest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2594,7 +2594,7 @@ impl<'test> TestCx<'test> {
25942594
}
25952595
None
25962596
} else {
2597-
let sline = line.split("///").last().unwrap_or("");
2597+
let sline = line.rsplit("///").next().unwrap();
25982598
let line = sline.trim_start();
25992599
if line.starts_with("```") {
26002600
if ignore {

0 commit comments

Comments
 (0)