Uploaded image for project: 'ZooKeeper'
  1. ZooKeeper
  2. ZOOKEEPER-3709

Pre-Size Buffer in Learner Request Method

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • None
    • None
    • None

    Description

      Learner.java
          void request(Request request) throws IOException {
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              DataOutputStream oa = new DataOutputStream(baos);
              oa.writeLong(request.sessionId);
              oa.writeInt(request.cxid);
              oa.writeInt(request.type);
              if (request.request != null) {
                  request.request.rewind();
                  int len = request.request.remaining();
                  byte[] b = new byte[len];
                  request.request.get(b);
                  request.request.rewind();
                  oa.write(b);
              }
              oa.close();
              QuorumPacket qp = new QuorumPacket(Leader.REQUEST, -1, baos.toByteArray(), request.authInfo);
              writePacket(qp, true);
          }
      

      The default internal array size of ByteArrayOutputStream is 32 bytes. It will be expanded as required but this operation is not optimal. Since the exact size of the buffer can be pre-determined (long, int, int, request buffer size), it would be better to specify the array size in ByteArrayOutputStream before writing to it.

      Attachments

        Activity

          People

            Unassigned Unassigned
            belugabehr David Mollitor
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 4h 20m
                4h 20m