Skip to content

Add space complexity tracker for #9203 #9237

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

Closed
Closed
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
34 changes: 33 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2861,7 +2861,39 @@ class Typer extends Namer
final def adapt(tree: Tree, pt: Type)(using Context): Tree =
adapt(tree, pt, ctx.typerState.ownedVars)

private def adapt1(tree: Tree, pt: Type, locked: TypeVars, tryGadtHealing: Boolean)(using Context): Tree = {
// See https://github.com/lampepfl/dotty/issues/9203 for why this is needed
// So far it is guarding only `adapt1` since that's the highest known entry point
// stack call where the detection for the debugging of #9203 is possible.
private inline def spaceComplexityTracker[T](tree: Tree)(task: => T)(using Context) =
def size = ctx.base.uniquesSizes.values
.map { case (size, _, _) => size }.sum
val before = size
val res = task
val after = size
val delta = after - before
if delta > 100000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably not be a constant. We should have a setting that allows us to modify this limit. We should probably use XmaxInlinedTrees (and maybe rename it).

println( // ctx.warning does not output the message in this place, probably waits
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not emit an error as we do in Inliner.scala#L132? There we return an errorTree to avoid further expansion, this may also work here.

// for the checking to finish – which never happens under the conditions
// `spaceComplexityTracker` was designed to track and report.
s"""
|[${Console.YELLOW}warn${Console.RESET}] Created ${delta} unique type objects on typecheck
|of the following tree in ${ctx.source}:
|
|${tree.show}
|
|The meaning of this is that the compiler is doing an unreasonable amount
|of work while compiling your code. The compilation may take a lot of time
|and heap space. This situation can happen, e.g., when the lookup space for
|givens is too large. To resolve the problem, try to eliminate the offending
|code snippet from your program and rephrase your program using fewer advanced
|features.
""".stripMargin)
ctx.reporter.flush()
end if
res
end spaceComplexityTracker

private def adapt1(tree: Tree, pt: Type, locked: TypeVars, tryGadtHealing: Boolean)(using Context): Tree = spaceComplexityTracker(tree) {
assert(pt.exists && !pt.isInstanceOf[ExprType] || ctx.reporter.errorsReported)
def methodStr = err.refStr(methPart(tree).tpe)

Expand Down