Skip to content

Out of memory error compiling macro example #7618

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
machisuji opened this issue Nov 26, 2019 · 4 comments
Closed

Out of memory error compiling macro example #7618

machisuji opened this issue Nov 26, 2019 · 4 comments

Comments

@machisuji
Copy link
Contributor

machisuji commented Nov 26, 2019

environment

OS: Linux 5.0.0-36-generic / Ubuntu 19.04 and debian 9 (stretch)
Java: 11.0.5
SBT: 1.3.4
Dotty: 0.20.0-RC1

minimized code

The code is mostly taken from the documentation about macros.

package macros

import scala.quoted.{given, _}

enum Exp {
  case Num(n: Int)
  case Plus(e1: Exp, e2: Exp)
  case Var(x: String)
  case Let(x: String, e: Exp, in: Exp)
}

object Compiler {
  import Exp._

  inline def compile(e: Exp, env: Map[String, Expr[Int]])(given ctx: QuoteContext): Expr[Int] = e match {
    case Num(n) =>
      Expr(n)
    case Plus(e1, e2) =>
      '{ ${ compile(e1, env) } + ${ compile(e2, env) } }
    case Var(x) =>
      env(x)
    case Let(x, e, body) =>
      '{ val y = ${ compile(e, env) }; ${ compile(body, env + (x -> 'y)) } }
  }
}

object Example {
  def run(): Unit = {
    import Exp._

    val exp = Plus(Plus(Num(2), Var("x")), Num(4))
    val letExp = Let("x", Num(3), exp)

    Compiler.compile(letExp, Map.empty)(given QuoteContext.macroContext)
  }
}

the problem

I have the code in a SBT project created using the g8 template. Trying to compile the code results in the the compiler getting stuck and running out of memory. The process never finishes. I have to kill it.

Obviously I have not the slightest idea what I'm doing with regard to macros but either way I think the compiler should be able to handle it.

sbt:macro-example> compile
[info] Compiling 1 Scala source to /usr/src/dotty-project/target/scala-0.20/classes ...

Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "classloader-cache-cleanup-0"

Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "task-progress-report-thread"

Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "pool-11-thread-3"

Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "pool-11-thread-7"

Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "pool-11-thread-9"

Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "pool-11-thread-2"

reproduction

If you happen to have docker installed you should be able to reproduce the issue with this one line:

docker run --rm -it machisuji/dotty-project:latest sbt compile
@smarter
Copy link
Member

smarter commented Nov 26, 2019

How much ram do you have ? Have you tried forcing sbt to use a specific amount ? (Run sbt -J-Xmx2g to get 2 GB)

@machisuji
Copy link
Contributor Author

machisuji commented Nov 26, 2019

I have 16gb RAM. I don't know how much sbt uses by default but increasing it to 2GB as you suggested yields a useful stack trace:

stack-overflow.txt

@nicolasstucki
Copy link
Contributor

nicolasstucki commented Dec 4, 2019

It is probably the use of given QuoteContext.macroContext that breaks it. Only inline def could use that at the time. Furthermore macroContext is not actually used anymore and should be removed. Removing in #7681.

@nicolasstucki
Copy link
Contributor

The out of memory is happening because there is a missing inline before the match

inline def compile(e: Exp, env: Map[String, Expr[Int]])(given ctx: QuoteContext): Expr[Int] = inline e match {

this implies that when the pattern does not match, the full pattern is kept and all branches trigger new inlines. Therefore inlining an exponentially amount of irreducible inlines each turn.

nicolasstucki added a commit that referenced this issue Dec 5, 2019
…-context

Fix #7618: Remove QuoteContext.macroContext
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants