Skip to content

Commit 5c33b3c

Browse files
committed
Change a few routines into macros to improve speed of COPY IN inner loop.
1 parent d32cd1b commit 5c33b3c

File tree

2 files changed

+32
-35
lines changed

2 files changed

+32
-35
lines changed

src/backend/commands/copy.c

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
*
88
* IDENTIFICATION
9-
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.97 2000/01/19 23:54:56 tgl Exp $
9+
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.98 2000/01/22 03:52:04 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -137,7 +137,8 @@ CopySendChar(char c, FILE *fp)
137137
* backend->frontend functions
138138
*
139139
* CopyGetChar does the same for single characters
140-
* CopyGetEof checks if it's EOF on the input
140+
* CopyGetEof checks if it's EOF on the input (or, check for EOF result
141+
* from CopyGetChar)
141142
*
142143
* NB: no data conversion is applied by these functions
143144
*/
@@ -1106,18 +1107,6 @@ GetIndexRelations(Oid main_relation_oid,
11061107
}
11071108
}
11081109

1109-
1110-
/*
1111-
returns 1 if c is in s
1112-
*/
1113-
static bool
1114-
inString(char c, char *s)
1115-
{
1116-
if (s && c)
1117-
return strchr(s, c) != NULL;
1118-
return 0;
1119-
}
1120-
11211110
/*
11221111
* Reads input from fp until an end of line is seen.
11231112
*/
@@ -1171,19 +1160,24 @@ CopyReadAttribute(FILE *fp, bool *isnull, char *delim, int *newline, char *null_
11711160

11721161
*isnull = (bool) false; /* set default */
11731162

1174-
if (CopyGetEof(fp))
1175-
goto endOfFile;
1176-
11771163
for (;;)
11781164
{
11791165
c = CopyGetChar(fp);
1180-
if (CopyGetEof(fp))
1166+
if (c == EOF)
11811167
goto endOfFile;
1182-
1168+
if (c == '\n')
1169+
{
1170+
*newline = 1;
1171+
break;
1172+
}
1173+
if (strchr(delim, c))
1174+
{
1175+
break;
1176+
}
11831177
if (c == '\\')
11841178
{
11851179
c = CopyGetChar(fp);
1186-
if (CopyGetEof(fp))
1180+
if (c == EOF)
11871181
goto endOfFile;
11881182
switch (c)
11891183
{
@@ -1213,14 +1207,14 @@ CopyReadAttribute(FILE *fp, bool *isnull, char *delim, int *newline, char *null_
12131207
}
12141208
else
12151209
{
1216-
if (CopyGetEof(fp))
1210+
if (c == EOF)
12171211
goto endOfFile;
12181212
CopyDonePeek(fp, c, 0); /* Return to stream! */
12191213
}
12201214
}
12211215
else
12221216
{
1223-
if (CopyGetEof(fp))
1217+
if (c == EOF)
12241218
goto endOfFile;
12251219
CopyDonePeek(fp, c, 0); /* Return to stream! */
12261220
}
@@ -1231,7 +1225,7 @@ CopyReadAttribute(FILE *fp, bool *isnull, char *delim, int *newline, char *null_
12311225
rather then just 'N' to provide compatibility with
12321226
the default NULL output. -- pe */
12331227
case 'N':
1234-
appendStringInfoChar(&attribute_buf, '\\');
1228+
appendStringInfoCharMacro(&attribute_buf, '\\');
12351229
c = 'N';
12361230
break;
12371231
case 'b':
@@ -1257,26 +1251,19 @@ CopyReadAttribute(FILE *fp, bool *isnull, char *delim, int *newline, char *null_
12571251
if (c != '\n')
12581252
elog(ERROR, "CopyReadAttribute - end of record marker corrupted. line: %d", lineno);
12591253
goto endOfFile;
1260-
break;
12611254
}
12621255
}
1263-
else if (c == '\n' || inString(c, delim))
1264-
{
1265-
if (c == '\n')
1266-
*newline = 1;
1267-
break;
1268-
}
1269-
appendStringInfoChar(&attribute_buf, c);
1256+
appendStringInfoCharMacro(&attribute_buf, c);
12701257
#ifdef MULTIBYTE
12711258
/* get additional bytes of the char, if any */
12721259
s[0] = c;
12731260
mblen = pg_encoding_mblen(encoding, s);
12741261
for (j = 1; j < mblen; j++)
12751262
{
12761263
c = CopyGetChar(fp);
1277-
if (CopyGetEof(fp))
1264+
if (c == EOF)
12781265
goto endOfFile;
1279-
appendStringInfoChar(&attribute_buf, c);
1266+
appendStringInfoCharMacro(&attribute_buf, c);
12801267
}
12811268
#endif
12821269
}

src/include/lib/stringinfo.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
* Copyright (c) 1994, Regents of the University of California
1111
*
12-
* $Id: stringinfo.h,v 1.14 1999/08/31 01:28:21 tgl Exp $
12+
* $Id: stringinfo.h,v 1.15 2000/01/22 03:52:03 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -89,12 +89,22 @@ extern void appendStringInfo(StringInfo str, const char *fmt,...);
8989
*/
9090
extern void appendStringInfoChar(StringInfo str, char ch);
9191

92+
/*------------------------
93+
* appendStringInfoCharMacro
94+
* As above, but a macro for even more speed where it matters.
95+
* Caution: str argument will be evaluated multiple times.
96+
*/
97+
#define appendStringInfoCharMacro(str,ch) \
98+
(((str)->len + 1 >= (str)->maxlen) ? \
99+
appendStringInfoChar(str, ch) : \
100+
((str)->data[(str)->len] = (ch), (str)->data[++(str)->len] = '\0'))
101+
92102
/*------------------------
93103
* appendBinaryStringInfo
94104
* Append arbitrary binary data to a StringInfo, allocating more space
95105
* if necessary.
96106
*/
97107
extern void appendBinaryStringInfo(StringInfo str,
98-
const char *data, int datalen);
108+
const char *data, int datalen);
99109

100110
#endif /* STRINGINFO_H */

0 commit comments

Comments
 (0)