From cded3d2dbf7a8eeff8095adf29b0e0132b01ef74 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Sun, 3 Oct 2021 19:38:41 +0200 Subject: [PATCH] Make `distinctRefs` in ImportSuggestions use constant stackspace Fixes #12876 (hopefully) --- .../dotty/tools/dotc/typer/ImportSuggestions.scala | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/typer/ImportSuggestions.scala b/compiler/src/dotty/tools/dotc/typer/ImportSuggestions.scala index 8232a64fe747..bc82c9501c76 100644 --- a/compiler/src/dotty/tools/dotc/typer/ImportSuggestions.scala +++ b/compiler/src/dotty/tools/dotc/typer/ImportSuggestions.scala @@ -273,11 +273,14 @@ trait ImportSuggestions: /** The `ref` parts of this list of pairs, discarding subsequent elements that * have the same String part. Elements are sorted by their String parts. */ - extension (refs: List[(TermRef, String)]) def distinctRefs(using Context): List[TermRef] = refs match - case (ref, str) :: refs1 => - ref :: refs1.dropWhile(_._2 == str).distinctRefs - case Nil => - Nil + extension (refs: List[(TermRef, String)]) def distinctRefs(using Context): List[TermRef] = + val buf = new mutable.ListBuffer[TermRef] + var last = "" + for (ref, str) <- refs do + if last != str then + buf += ref + last = str + buf.toList /** The best `n` references in `refs`, according to `compare` * `compare` is a partial order. If there's a tie, we take elements