Skip to content

Performance optimization: only compute stack traces when needed #16616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/CompilationUnit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import core.Decorators._
import config.{SourceVersion, Feature}
import StdNames.nme
import scala.annotation.internal.sharable
import scala.util.control.NoStackTrace
import transform.MacroAnnotations

class CompilationUnit protected (val source: SourceFile) {
Expand Down Expand Up @@ -105,7 +106,7 @@ class CompilationUnit protected (val source: SourceFile) {

object CompilationUnit {

class SuspendException extends Exception
class SuspendException extends Exception with NoStackTrace

/** Make a compilation unit for top class `clsd` with the contents of the `unpickled` tree */
def apply(clsd: ClassDenotation, unpickled: Tree, forceTrees: Boolean)(using Context): CompilationUnit =
Expand Down
16 changes: 14 additions & 2 deletions compiler/src/dotty/tools/dotc/core/TypeErrors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,22 @@ import Denotations._
import Decorators._
import reporting._
import ast.untpd
import config.Printers.cyclicErrors
import config.Printers.{cyclicErrors, noPrinter}

import scala.annotation.constructorOnly

abstract class TypeError(using creationContext: Context) extends Exception(""):

/** Will the stack trace of this exception be filled in?
* This is expensive and only useful for debugging purposes.
*/
def computeStackTrace: Boolean =
ctx.debug || (cyclicErrors != noPrinter && this.isInstanceOf[CyclicReference] && !(ctx.mode is Mode.CheckCyclic))

override def fillInStackTrace(): Throwable =
if computeStackTrace then super.fillInStackTrace().nn
else this

/** Convert to message. This takes an additional Context, so that we
* use the context when the message is first produced, i.e. when the TypeError
* is handled. This makes a difference for CyclicErrors since we need to know
Expand Down Expand Up @@ -164,7 +176,7 @@ class CyclicReference private (val denot: SymDenotation)(using Context) extends
object CyclicReference:
def apply(denot: SymDenotation)(using Context): CyclicReference =
val ex = new CyclicReference(denot)
if !(ctx.mode is Mode.CheckCyclic) || ctx.settings.Ydebug.value then
if ex.computeStackTrace then
cyclicErrors.println(s"Cyclic reference involving! $denot")
val sts = ex.getStackTrace.asInstanceOf[Array[StackTraceElement]]
for (elem <- sts take 200)
Expand Down