Harmony
  1. Harmony
  2. HARMONY-6328

[classlib][nio] optimize SocketChannel.write(ByteBuffer[], int, int) by writev

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 5.0M11
    • Fix Version/s: 5.0M12
    • Component/s: Classlib
    • Labels:
      None

      Description

      SocketChannel.write(ByteBuffer[], int, int) can be optimized by using writev, which can write an array of buffer to socket without any stage buffer. If all passed in ByteBuffer is direct buffer, they can be passed to system call directly without any copies.

      1. HARMONY-6328.diff
        8 kB
        Regis Xu
      2. HARMONY-6328.v2.diff
        10 kB
        Regis Xu
      3. HARMONY-6328.v3.diff
        13 kB
        Regis Xu
      4. test.diff
        3 kB
        Regis Xu
      5. HARMONY-6328.final.diff
        16 kB
        Regis Xu

        Issue Links

          Activity

          Hide
          Regis Xu added a comment -

          This patch add a new method writevDirect to INetworkSystem, and only implement native code on Linux, Windows side code will implement soon.

          Show
          Regis Xu added a comment - This patch add a new method writevDirect to INetworkSystem, and only implement native code on Linux, Windows side code will implement soon.
          Hide
          Regis Xu added a comment -

          HARMONY-6328.v2.diff add Windows implementation, it includes previous one.

          Show
          Regis Xu added a comment - HARMONY-6328 .v2.diff add Windows implementation, it includes previous one.
          Hide
          Tim Ellison added a comment -

          Good to see this enhancement!

          A minor comment first Please check return values for :

          + vect = (struct iovec*) hymem_allocate_memory(sizeof(struct iovec) * length);
          +
          + message = (*env)->GetPrimitiveArrayCritical(env, addrs, &isCopyMessage);
          + cts = (*env)->GetPrimitiveArrayCritical(env, counts, &isCopyCts);

          A further enhancement is to have two versions of primitives, one that deals with direct buffers and one that deals with java heap buffers, so that there is (potentially) no data copying required for the java-heap buffers version that you have got today:

          + if (!buffer.isDirect()) {
          + src[i] = ByteBuffer.allocateDirect(buffer.remaining());
          + int oldPosition = buffer.position();
          + src[i].put(buffer);

          some VMs will pin the java heap memory accessed in JNI (GetByteArrayElements), so if you can get hold of the ByteBuffer#array() you can pass a set of pointers to the backing arrays and maybe send the data directly.

          Looking forward to seeing the readv impl too

          Show
          Tim Ellison added a comment - Good to see this enhancement! A minor comment first Please check return values for : + vect = (struct iovec*) hymem_allocate_memory(sizeof(struct iovec) * length); + + message = (*env)->GetPrimitiveArrayCritical(env, addrs, &isCopyMessage); + cts = (*env)->GetPrimitiveArrayCritical(env, counts, &isCopyCts); A further enhancement is to have two versions of primitives, one that deals with direct buffers and one that deals with java heap buffers, so that there is (potentially) no data copying required for the java-heap buffers version that you have got today: + if (!buffer.isDirect()) { + src [i] = ByteBuffer.allocateDirect(buffer.remaining()); + int oldPosition = buffer.position(); + src [i] .put(buffer); some VMs will pin the java heap memory accessed in JNI (GetByteArrayElements), so if you can get hold of the ByteBuffer#array() you can pass a set of pointers to the backing arrays and maybe send the data directly. Looking forward to seeing the readv impl too
          Hide
          Regis Xu added a comment -

          HARMONY-6328.v3.diff try to utilize "pin feature" (if vm support)

          Show
          Regis Xu added a comment - HARMONY-6328 .v3.diff try to utilize "pin feature" (if vm support)
          Hide
          Regis Xu added a comment -

          test case and document based on HARMONY-6328.v3.diff

          Show
          Regis Xu added a comment - test case and document based on HARMONY-6328 .v3.diff
          Hide
          Regis Xu added a comment -

          Final patch for this issue, combines all above patches and improvements discussed on dev list.

          Show
          Regis Xu added a comment - Final patch for this issue, combines all above patches and improvements discussed on dev list.
          Hide
          Regis Xu added a comment -

          patch applied at r816508.

          Show
          Regis Xu added a comment - patch applied at r816508.

            People

            • Assignee:
              Regis Xu
              Reporter:
              Regis Xu
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development