raster pushed a commit to branch master.

http://git.enlightenment.org/apps/terminology.git/commit/?id=8cfa269b54657ed390ee6953589dbbdd27041e07

commit 8cfa269b54657ed390ee6953589dbbdd27041e07
Author: Carsten Haitzler (Rasterman) <[email protected]>
Date:   Mon Dec 18 15:16:54 2017 +0900

    tysend - optimize sending - pretty much double the speed
    
    yes. it's still inefficient because we transfer in ascii-ized nibbles
    (4 bits) within a utf8 stream that becoems a 32bit per char unicode
    buffer then back to utf8 before being "parsed" as a command etc. etc.
    ... it's not brilliant for transferring binary data. it's horrible
    actually. but at least i've dropped overhead for some of the large
    escape handling code.
    
    this increases buffer size to 32k per block sent, and have the
    terminal escape/buffer handling track if a zero byte exists in the
    buffer at all to avoid hunting for one if none is there, making
    terminology escape handling much more efficient for large escapes and
    buffers.
---
 src/bin/termio.c     | 20 ++++++++++++++++----
 src/bin/termpty.c    | 52 +++++++++++++++++++++++++++++++++++++++++++++++-----
 src/bin/termpty.h    |  1 +
 src/bin/termptyesc.c | 30 +++++++++---------------------
 src/bin/tysend.c     |  8 +++++---
 5 files changed, 78 insertions(+), 33 deletions(-)

diff --git a/src/bin/termio.c b/src/bin/termio.c
index 50d986b..93c636b 100644
--- a/src/bin/termio.c
+++ b/src/bin/termio.c
@@ -6040,20 +6040,32 @@ _smart_pty_command(void *data)
 
                   if (bb)
                     {
-                       p++;
+                       unsigned char localbuf[128];
+                       unsigned char localbufpos = 0;
+
+                       eina_binbuf_expand(bb, 32 * 1024);
                        sum = 0;
-                       for (; *p; p++)
+                       for (sum = 0, p++; *p; p++)
                          {
+                            // high nibble
                             v = (unsigned char)(*p);
                             sum += v;
-
                             v = ((v - '@') & 0xf) << 4;
+                            // low nibble
                             p++;
                             sum += *p;
                             v |= ((*p - '@') & 0xf);
+                            localbuf[localbufpos++] = v;
+                            if (localbufpos >= sizeof(localbuf))
+                              {
+                                 eina_binbuf_append_length(bb, localbuf, 
localbufpos);
+                                 localbufpos = 0;
+                              }
                             if (!*p) valid = EINA_FALSE;
-                            eina_binbuf_append_char(bb, v);
                          }
+                       if (localbufpos > 0)
+                         eina_binbuf_append_length(bb, localbuf, localbufpos);
+
                        if ((valid) && (sum == pksum) && (sd->sendfile.active))
                          {
                             // write "ok" (k) to term
diff --git a/src/bin/termpty.c b/src/bin/termpty.c
index 3c86546..4251e8a 100644
--- a/src/bin/termpty.c
+++ b/src/bin/termpty.c
@@ -57,7 +57,7 @@ termpty_shutdown(void)
 void
 termpty_handle_buf(Termpty *ty, const Eina_Unicode *codepoints, int len)
 {
-   Eina_Unicode *c, *ce, *b;
+   Eina_Unicode *c, *ce, *c2, *b, *d;
    int n, bytes;
 
    c = (Eina_Unicode *)codepoints;
@@ -65,6 +65,7 @@ termpty_handle_buf(Termpty *ty, const Eina_Unicode 
*codepoints, int len)
 
    if (ty->buf)
      {
+        if (!ty->buf) ty->buf_have_zero = EINA_FALSE;
         bytes = (ty->buflen + len + 1) * sizeof(int);
         b = realloc(ty->buf, bytes);
         if (!b)
@@ -73,8 +74,26 @@ termpty_handle_buf(Termpty *ty, const Eina_Unicode 
*codepoints, int len)
              return;
           }
         DBG("realloc add %i + %i", (int)(ty->buflen * sizeof(int)), (int)(len 
* sizeof(int)));
-        bytes = len * sizeof(Eina_Unicode);
-        memcpy(&(b[ty->buflen]), codepoints, bytes);
+        if (!ty->buf_have_zero)
+          {
+             d = &(b[ty->buflen]);
+             ce = (Eina_Unicode *)codepoints + len;
+             for (c = (Eina_Unicode *)codepoints; c < ce; c++, d++)
+               {
+                  *d = *c;
+                  if (*c == 0x0)
+                    {
+                       ty->buf_have_zero = EINA_TRUE;
+                       break;
+                    }
+               }
+             for (; c < ce; c++, d++) *d = *c;
+          }
+        else
+          {
+             bytes = len * sizeof(Eina_Unicode);
+             memcpy(&(b[ty->buflen]), codepoints, bytes);
+          }
         ty->buf = b;
         ty->buflen += len;
         ty->buf[ty->buflen] = 0;
@@ -96,8 +115,18 @@ termpty_handle_buf(Termpty *ty, const Eina_Unicode 
*codepoints, int len)
                        ERR(_("memerr: %s"), strerror(errno));
                        return;
                     }
+                  ty->buf_have_zero = EINA_FALSE;
+                  for (d = ty->buf, c2 = c; c2 < ce; c2++, d++)
+                    {
+                       *d = *c2;
+                       if (*c2 == 0x0)
+                         {
+                            ty->buf_have_zero = EINA_TRUE;
+                            break;
+                         }
+                    }
+                  for (; c2 < ce; c2++, d++) *d = *c2;
                   bytes = (char *)ce - (char *)c;
-                  memcpy(ty->buf, c, bytes);
                   ty->buflen = bytes / sizeof(Eina_Unicode);
                   ty->buf[ty->buflen] = 0;
                   free(tmp);
@@ -109,6 +138,7 @@ termpty_handle_buf(Termpty *ty, const Eina_Unicode 
*codepoints, int len)
           {
              if (ty->buf)
                {
+                  ty->buf_have_zero = EINA_FALSE;
                   free(ty->buf);
                   ty->buf = NULL;
                }
@@ -117,12 +147,14 @@ termpty_handle_buf(Termpty *ty, const Eina_Unicode 
*codepoints, int len)
      }
    else
      {
+        ty->buf_have_zero = EINA_TRUE;
         while (c < ce)
           {
              n = termpty_handle_seq(ty, c, ce);
              if (n == 0)
                {
                   bytes = ((char *)ce - (char *)c) + sizeof(Eina_Unicode);
+                  ty->buf_have_zero = EINA_FALSE;
                   ty->buf = malloc(bytes);
                   DBG("malloc %i", (int)(bytes - sizeof(Eina_Unicode)));
                   if (!ty->buf)
@@ -131,8 +163,18 @@ termpty_handle_buf(Termpty *ty, const Eina_Unicode 
*codepoints, int len)
                     }
                   else
                     {
+                       ty->buf_have_zero = EINA_FALSE;
+                       for (d = ty->buf, c2 = c; c2 < ce; c2++, d++)
+                         {
+                            *d = *c2;
+                            if (*c2 == 0x0)
+                              {
+                                 ty->buf_have_zero = EINA_TRUE;
+                                 break;
+                              }
+                         }
+                       for (; c2 < ce; c2++, d++) *d = *c2;
                        bytes = (char *)ce - (char *)c;
-                       memcpy(ty->buf, c, bytes);
                        ty->buflen = bytes / sizeof(Eina_Unicode);
                        ty->buf[ty->buflen] = 0;
                     }
diff --git a/src/bin/termpty.h b/src/bin/termpty.h
index 309aa4b..9ad1ad6 100644
--- a/src/bin/termpty.h
+++ b/src/bin/termpty.h
@@ -94,6 +94,7 @@ struct _Termpty
    Eina_Unicode *buf;
    size_t buflen;
    Eina_Unicode last_char;
+   Eina_Bool buf_have_zero;
    unsigned char oldbuf[4];
    Termsave *back;
    size_t backsize, backpos;
diff --git a/src/bin/termptyesc.c b/src/bin/termptyesc.c
index 800cdce..06c418b 100644
--- a/src/bin/termptyesc.c
+++ b/src/bin/termptyesc.c
@@ -1767,38 +1767,27 @@ err:
 static int
 _handle_esc_terminology(Termpty *ty, const Eina_Unicode *c, const Eina_Unicode 
*ce)
 {
-   Eina_Unicode *cc;
-   Eina_Unicode *buf, bufsmall[1024], *b;
+   Eina_Unicode *cc, *cc_zero = NULL;
+   const Eina_Unicode *buf;
    char *cmd;
    int blen = 0;
    Config *config;
 
+   if (!ty->buf_have_zero) return 0;
+
    config = termio_config_get(ty->obj);
 
    cc = (Eina_Unicode *)c;
+   if ((cc < ce) && (*cc == 0x0)) cc_zero = cc;
    while ((cc < ce) && (*cc != 0x0))
      {
         blen++;
         cc++;
      }
-   buf = bufsmall;
-   if (blen > (int)((sizeof(bufsmall) / sizeof(Eina_Unicode)) - 40))
-     buf = malloc((blen * sizeof(Eina_Unicode)) + 40);
-   cc = (Eina_Unicode *)c;
-   b = buf;
-   while ((cc < ce) && (*cc != 0x0))
-     {
-        *b = *cc;
-        b++;
-        cc++;
-     }
-   if ((cc < ce) && (*cc == 0x0)) cc++;
-   else
-     {
-        if (buf != bufsmall) free(buf);
-        return 0;
-     }
-   *b = 0;
+   if ((cc < ce) && (*cc == 0x0)) cc_zero = cc;
+   if (!cc_zero) return 0;
+   buf = (Eina_Unicode *)c;
+   cc = cc_zero;
 
    // commands are stored in the buffer, 0 bytes not allowed (end marker)
    cmd = eina_unicode_unicode_to_utf8(buf, NULL);
@@ -1810,7 +1799,6 @@ _handle_esc_terminology(Termpty *ty, const Eina_Unicode 
*c, const Eina_Unicode *
    ty->cur_cmd = NULL;
    free(cmd);
 
-   if (buf != bufsmall) free(buf);
    return cc - c;
 }
 
diff --git a/src/bin/tysend.c b/src/bin/tysend.c
index b4ad458..8aab9d7 100644
--- a/src/bin/tysend.c
+++ b/src/bin/tysend.c
@@ -63,8 +63,10 @@ main(int argc, char **argv)
    echo_off();
    for (i = 1; i < argc; i++)
      {
-        char *path, buf[8192], tbuf[PATH_MAX * 3];
-        unsigned char rawbuf[8192 + 128], rawbuf2[8192 + 128];
+#define BUFSZ 37268
+        char *path, tbuf[PATH_MAX * 3];
+        unsigned char rawbuf[(BUFSZ * 2) + 128], rawbuf2[(BUFSZ * 2) + 128];
+        char buf[4];
         int file_fd, pksize, pksum, bin, bout;
 
         path = argv[i];
@@ -87,7 +89,7 @@ main(int argc, char **argv)
                     {
                        if (buf[0] == 'k')
                          {
-                            pksize = read(file_fd, rawbuf, 4096);
+                            pksize = read(file_fd, rawbuf, BUFSZ);
 
                             if (pksize > 0)
                               {

-- 


Reply via email to