[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] packet: elide two buffer_prepend calls into one


Attached is a patch which should reduce the amount of
work done by packet_send2.


-Jon
From 2103b133b0bb9ce5a8494494ca6eea5c2f6d7029 Mon Sep 17 00:00:00 2001
From: Jon Simons <jon@xxxxxxxxxxxxx>
Date: Tue, 4 Feb 2014 22:17:55 -0800
Subject: [PATCH] packet: elide two buffer_prepend calls into one

In packet_send2, rather that issue two separate buffer_prepend_data calls
(each of which may entail realloc + memmove + memcpy), elide the prepend
work into a single buffer_prepend_data: the header information is computed
locally, and a single 5 byte prepend operation is now done instead of
prepending 1, then 4 bytes.
---
 src/packet.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/src/packet.c b/src/packet.c
index 75f6b58..d60ee08 100644
--- a/src/packet.c
+++ b/src/packet.c
@@ -498,11 +498,13 @@ static int packet_send2(ssh_session session) {
       session->current_crypto->out_cipher->blocksize : 8);
   uint32_t currentlen = buffer_get_rest_len(session->out_buffer);
   unsigned char *hmac = NULL;
-  char padstring[32] = {0};
+  char padstring[32] = { 0 };
   int rc = SSH_ERROR;
   uint32_t finallen,payloadsize,compsize;
   uint8_t padding;
 
+  uint8_t header[sizeof(padding) + sizeof(finallen)] = { 0 };
+
   payloadsize = currentlen;
 #ifdef WITH_ZLIB
   if (session->current_crypto
@@ -522,16 +524,13 @@ static int packet_send2(ssh_session session) {
 
   if (session->current_crypto) {
     ssh_get_random(padstring, padding, 0);
-  } else {
-    memset(padstring,0,padding);
   }
 
   finallen = htonl(currentlen + padding + 1);
 
-  if (buffer_prepend_data(session->out_buffer, &padding, sizeof(uint8_t)) < 0) {
-    goto error;
-  }
-  if (buffer_prepend_data(session->out_buffer, &finallen, sizeof(uint32_t)) < 0) {
+  memcpy(&header[0], &finallen, sizeof(finallen));
+  header[sizeof(finallen)] = padding;
+  if (buffer_prepend_data(session->out_buffer, &header, sizeof(header)) < 0) {
     goto error;
   }
   if (buffer_add_data(session->out_buffer, padstring, padding) < 0) {
-- 
1.8.4.21.g992c386


Follow-Ups:
Re: [PATCH] packet: elide two buffer_prepend calls into oneAndreas Schneider <asn@xxxxxxxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org