question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deep nested structure results in "Method too large" error during scalac compilation

See original GitHub issue

Hi,

I’ve got this error message

scalac: Error while emitting io/univalence/avro_serde/Main$
Method too large: io/univalence/avro_serde/Main$.main ([Ljava/lang/String;)V

For the following IDL: https://gist.github.com/fsarradin/da8049e388e39de8e34ba17d24b54457

And for this code

package io.univalence.avro_serde

import com.sksamuel.avro4s.JsonFormat
import com.sksamuel.avro4s.kafka.GenericSerde
import test.entity.Entity

object Main {
  def main(args: Array[String]): Unit = {
    val entitySerde = new GenericSerde[Entity](JsonFormat)
    println(entitySerde)
  }
}

Do you know why I got this message and how to avoid it?

I’m using

  • Avro4s 4.0.0
  • Scala 2.13.3
  • SBT 1.3.13
  • SBT-AvroHugger 2.0.0-RC22

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
sksamuelcommented, Dec 24, 2020

Hi I’m having this problem as well but the above suggested method still doesn’t work. Here’s dilemma:

  • We wrap all implicits in one single object AvroSchemas, and during compilation it throws Method too large: xxx/AvroSchemas$.<init> ()V

like this:

object AvroSchemas {
  implicit val gschema = AvroSchema[Goo]
  implicit val wschema = AvroSchema[Woo]
  implicit val fschema = AvroSchema[Foo]
}
  • We then tried separating the implicits and put each in a different object
object gs {
  implicit val gschema = AvroSchema[Goo]
}
object ws {
  implicit val wschema = AvroSchema[Woo]
}
object fs {
  implicit val fschema = AvroSchema[Foo]
}

but then object fs is not benefiting from the above 2 smaller objects.

If try importing gs and ws into fs, then it will say ‘not used’ which will fail compilation because we don’t allow unused import.

What can we do in this case?

avro4s builds schemas using SchemaFor so you could split out the three types, and then add an implicit schema for where you need it.

object gs {
  implicit val gschema = AvroSchema[Goo]
}
object ws {
  implicit val wschema = AvroSchema[Woo]
}
object fs {
  implicit val useG = new SchemaFor[Goo] { .... }
  implicit val useW = new SchemaFor[Woo] { .... }
  implicit val fschema = AvroSchema[Foo]
}
1reaction
sksamuelcommented, Dec 21, 2020

If you split up the macro by introducing intermediate schemas it should work.

Eg, if you had

case class Goo(a: String)
case class Woo(a: Int)
case class Foo(a: Goo, b: Woo)

then you can do something like:

implicit val gschema = AvroSchema[Goo]
implicit val wschema = AvroSchema[Woo]
implicit val fschema = AvroSchema[Foo]

This should force the compiler to generate smaller macro methods.

You can try by picking 2 or 3 large nested types and pulling out the implicits. You need to do it for schemas and encoders / decoders.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to circumvent the "Method too large" error in Java ...
In this case, the problem is that a single method can consist of at most 65536 bytes of bytecodes. (See the JVM spec)....
Read more >
java.lang.RuntimeException: Method code too large!
I am working on a scala-internal DSL that "serialize" its data structures into scala code that get's interpreted back from files to generate...
Read more >
Speeding Up Compilation Time with scalac-profiling
I use the plugin to speed up the compile times of a Bloop module by 8x. The analysis and optimizations here presented can...
Read more >
“Code too large” Compilation Error in Java | Baeldung
When a Java method exceeds 65535 bytes, we get the compilation error, “code too large”. In this article, we'll discuss why this error...
Read more >
Chapter 5. Using implicits to write expressive code - Scala in ...
The implicit system in Scala allows the compiler to adjust code using a well-defined lookup mechanism ... We then define the tmp method...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found