Scala Programming Language
  1. Scala Programming Language
  2. SI-6508

no null-check and straight to eq when matching on constant pattern with synthetic equals

    Details

      Description

      from https://groups.google.com/d/topic/scala-internals/ysNnBpYQwCY/discussion

      We can special-treat some common patterns however. If the scrutinee is a List and the
      pattern is a Nil then we know that we can get by with an instanceof or eq comparison.
      Same for None in Option. Same again for any case object that gets the synthesized
      equals instead of a user-defined one.
      Because these cases should be quite common it's probably worth to do it.

      when a constant pattern is statically known to have a synthetic equals method that simply calls `eq`, do not perform a null check and call `eq` directly.

      similarly, an instanceof check implies non-nullness, so don't re-check null

      https://issues.scala-lang.org/browse/SI-6508

      Yes, or instanceof. - Martin

        Issue Links

          Activity

          Hide
          Adriaan Moors added a comment -

          however, https://wikis.oracle.com/display/HotSpotInternals/PerformanceTechniques

          Low-level safety checks
          Null checks are cheap. They usually fold straight into a related memory access instruction, and use the CPU bus logic to catch nulls. (Deoptimization follows, with regenerated code containing an explicit check.)
          User-written null checks are in most cases functionally identical to those inserted by the JVM.
          Null checks can be hoisted manually, and suppress implicit null checks in dominated blocks.
          Similar points can be made about other simple predicates, like class checks and range checks.
          All such checks, whether implicit or manually written, are aggressively folded to constants.

          Show
          Adriaan Moors added a comment - however, https://wikis.oracle.com/display/HotSpotInternals/PerformanceTechniques Low-level safety checks Null checks are cheap. They usually fold straight into a related memory access instruction, and use the CPU bus logic to catch nulls. (Deoptimization follows, with regenerated code containing an explicit check.) User-written null checks are in most cases functionally identical to those inserted by the JVM. Null checks can be hoisted manually, and suppress implicit null checks in dominated blocks. Similar points can be made about other simple predicates, like class checks and range checks. All such checks, whether implicit or manually written, are aggressively folded to constants.
          Hide
          Adriaan Moors added a comment -

          Greg rightfully points out that we should still try to reduce the amount of bytecode we emit
          also, cutting extraneous checks means the JIT doesn't have to spend time optimizing them

          Show
          Adriaan Moors added a comment - Greg rightfully points out that we should still try to reduce the amount of bytecode we emit also, cutting extraneous checks means the JIT doesn't have to spend time optimizing them
          Hide
          Adriaan Moors added a comment -

          note that we must still test outer pointers, so this optimization probably does not make sense for nested objects (that appear as constant patterns)

          Show
          Adriaan Moors added a comment - note that we must still test outer pointers, so this optimization probably does not make sense for nested objects (that appear as constant patterns)
          Hide
          Adriaan Moors added a comment -

          unfortunately, I can't reproduce without the original source code

          I've been looking at possible inefficiencies and the only one I could find (without changing the spec)
          is reported in SI-6941, so I'm going to close this one as a duplicate unless evidence appears it's something else

          Show
          Adriaan Moors added a comment - unfortunately, I can't reproduce without the original source code I've been looking at possible inefficiencies and the only one I could find (without changing the spec) is reported in SI-6941 , so I'm going to close this one as a duplicate unless evidence appears it's something else

            People

            • Assignee:
              Adriaan Moors
              Reporter:
              Martin Odersky
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Due:
                Created:
                Updated:
                Resolved:

                Development