Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf987f83

Browse files
committed
pgbench: replace run-time string comparisons with an enum identifier.
Minor refactoring that should yield some performance benefit.Fabien Coelho, reviewed by Aleksandr ParfenovDiscussion:https://postgr.es/m/alpine.DEB.2.20.1709230538130.4999@lancre
1 parent81e334c commitf987f83

File tree

1 file changed

+51
-11
lines changed

1 file changed

+51
-11
lines changed

‎src/bin/pgbench/pgbench.c

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,15 @@ typedef struct
362362
#defineMETA_COMMAND2
363363
#defineMAX_ARGS10
364364

365+
typedefenumMetaCommand
366+
{
367+
META_NONE,/* not a known meta-command */
368+
META_SET,/* \set */
369+
META_SETSHELL,/* \setshell */
370+
META_SHELL,/* \shell */
371+
META_SLEEP/* \sleep */
372+
}MetaCommand;
373+
365374
typedefenumQueryMode
366375
{
367376
QUERY_SIMPLE,/* simple query */
@@ -378,6 +387,7 @@ typedef struct
378387
char*line;/* text of command line */
379388
intcommand_num;/* unique index of this Command struct */
380389
inttype;/* command type (SQL_COMMAND or META_COMMAND) */
390+
MetaCommandmeta;/* meta command identifier, or META_NONE */
381391
intargc;/* number of command words */
382392
char*argv[MAX_ARGS];/* command word list */
383393
PgBenchExpr*expr;/* parsed expression, if needed */
@@ -1721,6 +1731,29 @@ evaluateExpr(TState *thread, CState *st, PgBenchExpr *expr, PgBenchValue *retval
17211731
}
17221732
}
17231733

1734+
/*
1735+
* Convert command name to meta-command enum identifier
1736+
*/
1737+
staticMetaCommand
1738+
getMetaCommand(constchar*cmd)
1739+
{
1740+
MetaCommandmc;
1741+
1742+
if (cmd==NULL)
1743+
mc=META_NONE;
1744+
elseif (pg_strcasecmp(cmd,"set")==0)
1745+
mc=META_SET;
1746+
elseif (pg_strcasecmp(cmd,"setshell")==0)
1747+
mc=META_SETSHELL;
1748+
elseif (pg_strcasecmp(cmd,"shell")==0)
1749+
mc=META_SHELL;
1750+
elseif (pg_strcasecmp(cmd,"sleep")==0)
1751+
mc=META_SLEEP;
1752+
else
1753+
mc=META_NONE;
1754+
returnmc;
1755+
}
1756+
17241757
/*
17251758
* Run a shell command. The result is assigned to the variable if not NULL.
17261759
* Return true if succeeded, or false on error.
@@ -2214,7 +2247,7 @@ doCustom(TState *thread, CState *st, StatsData *agg)
22142247
fprintf(stderr,"\n");
22152248
}
22162249

2217-
if (pg_strcasecmp(argv[0],"sleep")==0)
2250+
if (command->meta==META_SLEEP)
22182251
{
22192252
/*
22202253
* A \sleep doesn't execute anything, we just get the
@@ -2240,7 +2273,7 @@ doCustom(TState *thread, CState *st, StatsData *agg)
22402273
}
22412274
else
22422275
{
2243-
if (pg_strcasecmp(argv[0],"set")==0)
2276+
if (command->meta==META_SET)
22442277
{
22452278
PgBenchExpr*expr=command->expr;
22462279
PgBenchValueresult;
@@ -2259,7 +2292,7 @@ doCustom(TState *thread, CState *st, StatsData *agg)
22592292
break;
22602293
}
22612294
}
2262-
elseif (pg_strcasecmp(argv[0],"setshell")==0)
2295+
elseif (command->meta==META_SETSHELL)
22632296
{
22642297
boolret=runShellCommand(st,argv[1],argv+2,argc-2);
22652298

@@ -2279,7 +2312,7 @@ doCustom(TState *thread, CState *st, StatsData *agg)
22792312
/* succeeded */
22802313
}
22812314
}
2282-
elseif (pg_strcasecmp(argv[0],"shell")==0)
2315+
elseif (command->meta==META_SHELL)
22832316
{
22842317
boolret=runShellCommand(st,NULL,argv+1,argc-1);
22852318

@@ -3023,6 +3056,7 @@ process_sql_command(PQExpBuffer buf, const char *source)
30233056
my_command= (Command*)pg_malloc0(sizeof(Command));
30243057
my_command->command_num=num_commands++;
30253058
my_command->type=SQL_COMMAND;
3059+
my_command->meta=META_NONE;
30263060
initSimpleStats(&my_command->stats);
30273061

30283062
/*
@@ -3091,7 +3125,10 @@ process_backslash_command(PsqlScanState sstate, const char *source)
30913125
my_command->argv[j++]=pg_strdup(word_buf.data);
30923126
my_command->argc++;
30933127

3094-
if (pg_strcasecmp(my_command->argv[0],"set")==0)
3128+
/* ... and convert it to enum form */
3129+
my_command->meta=getMetaCommand(my_command->argv[0]);
3130+
3131+
if (my_command->meta==META_SET)
30953132
{
30963133
/* For \set, collect var name, then lex the expression. */
30973134
yyscan_tyyscanner;
@@ -3146,7 +3183,7 @@ process_backslash_command(PsqlScanState sstate, const char *source)
31463183
expr_scanner_offset(sstate),
31473184
true);
31483185

3149-
if (pg_strcasecmp(my_command->argv[0],"sleep")==0)
3186+
if (my_command->meta==META_SLEEP)
31503187
{
31513188
if (my_command->argc<2)
31523189
syntax_error(source,lineno,my_command->line,my_command->argv[0],
@@ -3187,20 +3224,21 @@ process_backslash_command(PsqlScanState sstate, const char *source)
31873224
my_command->argv[2],offsets[2]-start_offset);
31883225
}
31893226
}
3190-
elseif (pg_strcasecmp(my_command->argv[0],"setshell")==0)
3227+
elseif (my_command->meta==META_SETSHELL)
31913228
{
31923229
if (my_command->argc<3)
31933230
syntax_error(source,lineno,my_command->line,my_command->argv[0],
31943231
"missing argument",NULL,-1);
31953232
}
3196-
elseif (pg_strcasecmp(my_command->argv[0],"shell")==0)
3233+
elseif (my_command->meta==META_SHELL)
31973234
{
31983235
if (my_command->argc<2)
31993236
syntax_error(source,lineno,my_command->line,my_command->argv[0],
32003237
"missing command",NULL,-1);
32013238
}
32023239
else
32033240
{
3241+
/* my_command->meta == META_NONE */
32043242
syntax_error(source,lineno,my_command->line,my_command->argv[0],
32053243
"invalid command",NULL,-1);
32063244
}
@@ -4592,12 +4630,12 @@ threadRun(void *arg)
45924630
timeout.tv_usec=min_usec %1000000;
45934631
nsocks=select(maxsock+1,&input_mask,NULL,NULL,&timeout);
45944632
}
4595-
else/* nothing active, simple sleep */
4633+
else/* nothing active, simple sleep */
45964634
{
45974635
pg_usleep(min_usec);
45984636
}
45994637
}
4600-
else/* no explicit delay, select without timeout */
4638+
else/* no explicit delay, select without timeout */
46014639
{
46024640
nsocks=select(maxsock+1,&input_mask,NULL,NULL,NULL);
46034641
}
@@ -4614,8 +4652,10 @@ threadRun(void *arg)
46144652
gotodone;
46154653
}
46164654
}
4617-
else/* min_usec == 0, i.e. something needs to be executed */
4655+
else
46184656
{
4657+
/* min_usec == 0, i.e. something needs to be executed */
4658+
46194659
/* If we didn't call select(), don't try to read any data */
46204660
FD_ZERO(&input_mask);
46214661
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp