You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@zookeeper.apache.org by "Jono Morris (Jira)" <ji...@apache.org> on 2020/02/25 09:26:00 UTC
[jira] [Commented] (ZOOKEEPER-3709) Pre-Size Buffer in Learner
Request Method
[ https://issues.apache.org/jira/browse/ZOOKEEPER-3709?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17044281#comment-17044281 ]
Jono Morris commented on ZOOKEEPER-3709:
----------------------------------------
I can manage this one if you're happy with that.
> Pre-Size Buffer in Learner Request Method
> -----------------------------------------
>
> Key: ZOOKEEPER-3709
> URL: https://issues.apache.org/jira/browse/ZOOKEEPER-3709
> Project: ZooKeeper
> Issue Type: Improvement
> Reporter: David Mollitor
> Priority: Minor
> Labels: newbie, noob
>
> {code:java|title=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);
> }
> {code}
> 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.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)