Uploaded image for project: 'Spigot'
  1. Spigot
  2. SPIGOT-7146

ItemMeta implementations switch over Material, unnecessarily inflating jar size

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Minor Minor
    • None
    • None
    • None
    • 1.18.2, or basically all versions
    • Yes

      Java compiler expands 'switch of a enum type' expression to a synthetic method with name starting with '$SWITCH_TABLE$' which generates an int array to match with the ordinal of enum entries in 'case' expression. Even if you only use one 'case' expression, the compiler will generate method that maps ALL entries in that enum. The int array and method are generated for ALL the classes who 'switched' that enum. When the enum has hundreds of entries, the byte size of that method expand rapidly, and the large method kills decompiler and IDE, increase jar file size, and it's product int array waste runtime memory.

      For example, in 1.18.2-R0.1 spigot jar with craftbukkit, CraftMetaBook class, which 'switches' Material enum class with 2 'cases', takes 115,045 bytes, while another class CraftMetaCompass which have no 'switch' to Material enum, takes only 14,541 bytes. The decompilation of CraftMetaBook class takes more than 15s on my computer using FernFlower. Intellij IDEA also lags for that long of time when opening this class.

      Be honest, this isn't a big issue when writing code, and it doesn't hit performance a lot. But I think abuse 'switch' expression on large enum class such as Material should not be encouraged and it's considered a bad coding behavior. To reduce such behaviors, we can create some new classes that similar to Items or EntityTypes class in NMS to encourage developers to use 'if' expression. 

      At least, I think craftbukkit internal should eliminate these 'switch' expressions as much as possible (as they always give me a suprise when I'm checking implementations)

            2008Choco Parker Hawke
            HenryRenYz Yz Ren
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: