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

(bf) Buffer.checkIndex(int, int) should use Preconditions.checkIndex(int, int, BiFunction)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 21
    • 21
    • core-libs
    • None
    • b08

      This came up in the benchmarks for this PR:
      https://github.com/openjdk/panama-foreign/pull/762

      I changed Buffer.checkIndex(int i, int nb) to rely on Objects.checkIndex() (which has a c2 intrinsic):

      https://github.com/rwestrel/panama-foreign/blob/268a254e2be477fca82cb14ebcecd9ffb699ca19/src/java.base/share/classes/java/nio/Buffer.java#L749

      and, I'm seeing a ~20% performance improvement for the MemorySegmentVsBits.byteBuffer benchmark (this also needs some unrelated c2 tweaks).

      Note the Buffer.checkIndex() implementation above is an experiment, I don't claim it's correct in all cases.

      I discussed this with Maurizio and he mentioned the MemorySegment has a similar range check method that's implemented with Objects.checkIndex().

      One thing to consider is that Objects.checkFromIndexSize() is functionally close to what's needed for Buffer.checkIndex() (and the MemorySegment range checking method) but has no c2 intrinsic. One possible path is to implement Buffer.checkIndex(int i, int nb) with Objects.checkFromIndexSize() and then rework Objects.checkFromIndexSize() so it calls Objects.checkIndex().

            bpb Brian Burkhalter
            roland Roland Westrelin
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: