From f7c3d6a60ac38a2882e9214f63e5dd8b26de9b1d Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Mon, 28 Oct 2024 14:52:46 -0700 Subject: [PATCH] BestFirstSearch: don't recurse if isOpt --- .../org/scalafmt/internal/BestFirstSearch.scala | 13 +++++++------ .../shared/src/test/resources/default/Advanced.stat | 2 +- .../shared/src/test/resources/default/Apply.stat | 2 +- .../src/test/scala/org/scalafmt/FormatTests.scala | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/BestFirstSearch.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/BestFirstSearch.scala index e6660fa7c..4fa4b59e7 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/BestFirstSearch.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/BestFirstSearch.scala @@ -55,12 +55,13 @@ private class BestFirstSearch private (range: Set[Range])(implicit ): Option[Option[State]] = { val key = (start.indentation & 0xffL) | (start.column & 0xffffffL) << 8 | (start.depth & 0xffffffffL) << 32 - def orElse = { - val nextState = shortestPath(start, stop, depth, isOpt).toOption - if (nextState.isDefined || !isOpt && !start.hasSlb()) memo - .update(key, nextState) - Some(nextState) - } + def orElse = + if (isOpt) Some(None) // we wouldn't recurse unless the span was large + else { + val nextState = shortestPath(start, stop, depth, isOpt).toOption + if (nextState.isDefined || !start.hasSlb()) memo.update(key, nextState) + Some(nextState) + } memo.get(key).orElse(orElse) } diff --git a/scalafmt-tests/shared/src/test/resources/default/Advanced.stat b/scalafmt-tests/shared/src/test/resources/default/Advanced.stat index 6af7e2688..3cc215059 100644 --- a/scalafmt-tests/shared/src/test/resources/default/Advanced.stat +++ b/scalafmt-tests/shared/src/test/resources/default/Advanced.stat @@ -377,7 +377,7 @@ private def withNewLocalDefs = { })) })) } ->>> { stateVisits = 3779 } +>>> { stateVisits = 3750 } val createIsArrayOfStat = { envFieldDef( "isArrayOf", diff --git a/scalafmt-tests/shared/src/test/resources/default/Apply.stat b/scalafmt-tests/shared/src/test/resources/default/Apply.stat index 3da1aee55..94492c7b2 100644 --- a/scalafmt-tests/shared/src/test/resources/default/Apply.stat +++ b/scalafmt-tests/shared/src/test/resources/default/Apply.stat @@ -273,7 +273,7 @@ class ResolutionCopier(x: Int) { toNode.rightBracket)); } } ->>> { stateVisits = 57714 } +>>> { stateVisits = 57624 } class ResolutionCopier(x: Int) { def visitClassDeclaration(node: ClassDeclaration): Boolean = { diff --git a/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala b/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala index 83d72b054..08c9b16d9 100644 --- a/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala +++ b/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala @@ -137,7 +137,7 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions { val explored = Debug.explored.get() logger.debug(s"Total explored: $explored") if (!onlyUnit && !onlyManual) - assertEquals(explored, 1487762, "total explored") + assertEquals(explored, 1484918, "total explored") val results = debugResults.result() // TODO(olafur) don't block printing out test results. // I don't want to deal with scalaz's Tasks :'(