Sunday, September 23, 2012

ch.epfl.lamp.fjbg.JCode$$OffsetTooBigException

I've hit a OffsetTooBigException (java.lang.Error: ch.epfl.lamp.fjbg.JCode$$OffsetTooBigException: offset too big to fit in 16 bits: 36580) when trying to compile a long list of case. It is easily reproducable with this code:

def testCompile(test: String) {
  val PatternA = """a""".r
  val PatternB = """b""".r
  val PatternC = """c""".r
  val PatternD = """d""".r
  val PatternE = """e""".r
  val PatternF = """f""".r
  val PatternG = """g""".r
  val PatternH = """h""".r
  val PatternI = """i""".r
  val PatternJ = """j""".r
  test match {
    case PatternA(a) => a
    case PatternB(b) => b
    case PatternC(c) => c
    case PatternD(d) => d
    case PatternE(e) => e
    case PatternF(f) => f
    case PatternG(g) => g
    case PatternH(h) => h
    case PatternI(i) => i
    case PatternJ(j) => j
  }
}

The problem comes from the compiler creating for nested extractor huge sized bytecode. Apparently it is fixed for the 2.10 release. If you're not using it, the work around is easy, just cut the cases into pieces:

def testCompile(test: String) {
  val PatternA = """a""".r
  val PatternB = """b""".r
  val PatternC = """c""".r
  val PatternD = """d""".r
  val PatternE = """e""".r
  val PatternF = """f""".r
  val PatternG = """g""".r
  val PatternH = """h""".r
  val PatternI = """i""".r
  val PatternJ = """j""".r

  val firstPatternSet : PartialFunction[String, String] = {
    case PatternA(a) => a
    case PatternB(b) => b
    case PatternC(c) => c
    case PatternD(d) => d
    case PatternE(e) => e
  }

  val secondPatternSet: PartialFunction[String, String] = {
    case PatternF(f) => f
    case PatternG(g) => g
    case PatternH(h) => h
    case PatternI(i) => i
    case PatternJ(j) => j
  }

 firstPatternSet(test) orElse secondPatternSet(test)
}

No comments:

Post a Comment