Replace pq_getbytes(&ch, 1) calls with pq_getbyte(), which is easier
authorTom Lane <[email protected]>
Tue, 4 Dec 2001 19:40:17 +0000 (19:40 +0000)
committerTom Lane <[email protected]>
Tue, 4 Dec 2001 19:40:17 +0000 (19:40 +0000)
to use and significantly faster.  This tweak saves 25% (!) of the runtime
of COPY IN in a test with 8000-character lines.  I wouldn't normally
commit a performance improvement this late in the cycle, but 25% got
my attention...

src/backend/commands/copy.c
src/backend/libpq/pqcomm.c
src/backend/tcop/postgres.c
src/include/libpq/libpq.h

index 45d49a660966cebc5fc26819e6a0da863fe6c6da..c8f30924825b7828a213832149345f7228b957b5 100644 (file)
@@ -157,13 +157,10 @@ CopyGetChar(FILE *fp)
 {
        if (!fp)
        {
-               unsigned char ch;
+               int                     ch = pq_getbyte();
 
-               if (pq_getbytes((char *) &ch, 1))
-               {
+               if (ch == EOF)
                        fe_eof = true;
-                       return EOF;
-               }
                return ch;
        }
        else
@@ -209,12 +206,9 @@ CopyDonePeek(FILE *fp, int c, int pickup)
                if (pickup)
                {
                        /*
-                        * We want to pick it up - just receive again into dummy
-                        * buffer
+                        * We want to pick it up
                         */
-                       char            c;
-
-                       pq_getbytes(&c, 1);
+                       (void) pq_getbyte();
                }
                /* If we didn't want to pick it up, just leave it where it sits */
        }
index 486ae0007d30e367aecd71b8f620d2cbcc2bd3a7..d6551b5bfcf1b8163a9336a500871e42c627302b 100644 (file)
@@ -47,6 +47,7 @@
  * low-level I/O:
  *             pq_getbytes             - get a known number of bytes from connection
  *             pq_getstring    - get a null terminated string from connection
+ *             pq_getbyte              - get next byte from connection
  *             pq_peekbyte             - peek at next byte from connection
  *             pq_putbytes             - send bytes to connection (not flushed until pq_flush)
  *             pq_flush                - flush pending output
@@ -527,7 +528,7 @@ pq_recvbuf(void)
  *             pq_getbyte      - get a single byte from connection, or return EOF
  * --------------------------------
  */
-static int
+int
 pq_getbyte(void)
 {
        while (PqRecvPointer >= PqRecvLength)
index 95a5ef6abc56bd28d542e122e27b7e6be0179d4a..d398f256dac897d3a93702262c81f968664f3fbb 100644 (file)
@@ -242,25 +242,25 @@ InteractiveBackend(StringInfo inBuf)
 static int
 SocketBackend(StringInfo inBuf)
 {
-       char            qtype;
-       char            result = '\0';
+       int                     qtype;
 
        /*
         * get input from the frontend
         */
-       qtype = '?';
-       if (pq_getbytes(&qtype, 1) == EOF)
-               return EOF;
+       qtype = pq_getbyte();
 
        switch (qtype)
        {
+               case EOF:
+                       /* frontend disconnected */
+                       break;
+
                        /*
                         * 'Q': user entered a query
                         */
                case 'Q':
                        if (pq_getstr(inBuf))
                                return EOF;
-                       result = 'Q';
                        break;
 
                        /*
@@ -269,14 +269,12 @@ SocketBackend(StringInfo inBuf)
                case 'F':
                        if (pq_getstr(inBuf))
                                return EOF;             /* ignore "string" at start of F message */
-                       result = 'F';
                        break;
 
                        /*
                         * 'X':  frontend is exiting
                         */
                case 'X':
-                       result = 'X';
                        break;
 
                        /*
@@ -289,7 +287,8 @@ SocketBackend(StringInfo inBuf)
                        elog(FATAL, "Socket command type %c unknown", qtype);
                        break;
        }
-       return result;
+
+       return qtype;
 }
 
 /* ----------------
@@ -1627,7 +1626,7 @@ PostgresMain(int argc, char *argv[], const char *username)
        if (!IsUnderPostmaster)
        {
                puts("\nPOSTGRES backend interactive interface ");
-               puts("$Revision: 1.242 $ $Date: 2001/11/10 23:51:14 $\n");
+               puts("$Revision: 1.243 $ $Date: 2001/12/04 19:40:17 $\n");
        }
 
        /*
index 6a89ab61f4de0b32de6a641b5a89fe5015161c50..4ead1d5639ba3c42fe371a354f5584c50db1f104 100644 (file)
@@ -62,6 +62,7 @@ extern void StreamClose(int sock);
 extern void pq_init(void);
 extern int     pq_getbytes(char *s, size_t len);
 extern int     pq_getstring(StringInfo s);
+extern int     pq_getbyte(void);
 extern int     pq_peekbyte(void);
 extern int     pq_putbytes(const char *s, size_t len);
 extern int     pq_flush(void);