Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8319220

Pattern matching switch with a lot of cases is unduly slow

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P3
    • 22
    • 21, 22
    • tools
    • b26

    Description

      Consider code like:
      ```
      void test(Object o) {
          switch (o) {
              case X1 -> {}
              case X2 -> {}
      ...(about 100 cases)
      ```

      this is much slower that writing this as:
      ``
      void test(Object o) {
          if (o instanceof X1) {}
          else if (o instanceof X2) {}
      ...(about 100 cases)
      ```

      Attached is a JMH test, which gives these results:
      MyBenchmark.testIfElse100 thrpt 5 512697.015 ± 19581.020 ops/s
      MyBenchmark.testSwitch100 thrpt 5 31815.371 ± 261.291 ops/s

      I.e. the switch is 15+ times slower than the if-else cascade.

      Attachments

        Issue Links

          Activity

            People

              jlahoda Jan Lahoda
              jlahoda Jan Lahoda
              Votes:
              1 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: