Skip to content

Commit 04c5b69

Browse files
committed
Clean up psql variable code a little: eliminate unnecessary tests in
GetVariable() and be consistent about treatment of the list header. Motivated by noticing strspn() taking an unreasonable percentage of runtime --- the call removed from GetVariable() was the only one that could be in a high-usage path ...
1 parent 3f9aace commit 04c5b69

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

src/bin/psql/variables.c

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2006, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/variables.c,v 1.24 2006/06/14 16:49:03 tgl Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/variables.c,v 1.25 2006/06/21 16:05:11 tgl Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "common.h"
@@ -29,15 +29,13 @@ GetVariable(VariableSpace space, const char *name)
2929
if (!space)
3030
return NULL;
3131

32-
if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name))
33-
return NULL;
34-
35-
for (current = space; current; current = current->next)
32+
for (current = space->next; current; current = current->next)
3633
{
37-
psql_assert(current->name);
38-
psql_assert(current->value);
3934
if (strcmp(current->name, name) == 0)
35+
{
36+
psql_assert(current->value);
4037
return current->value;
38+
}
4139
}
4240

4341
return NULL;
@@ -126,6 +124,9 @@ PrintVariables(VariableSpace space)
126124
{
127125
struct _variable *ptr;
128126

127+
if (!space)
128+
return;
129+
129130
for (ptr = space->next; ptr; ptr = ptr->next)
130131
{
131132
printf("%s = '%s'\n", ptr->name, ptr->value);
@@ -143,18 +144,19 @@ SetVariable(VariableSpace space, const char *name, const char *value)
143144
if (!space)
144145
return false;
145146

146-
if (!value)
147-
return DeleteVariable(space, name);
148-
149147
if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name))
150148
return false;
151149

152-
for (current = space, previous = NULL; current; previous = current, current = current->next)
150+
if (!value)
151+
return DeleteVariable(space, name);
152+
153+
for (previous = space, current = space->next;
154+
current;
155+
previous = current, current = current->next)
153156
{
154-
psql_assert(current->name);
155-
psql_assert(current->value);
156157
if (strcmp(current->name, name) == 0)
157158
{
159+
psql_assert(current->value);
158160
free(current->value);
159161
current->value = pg_strdup(value);
160162
return true;
@@ -182,19 +184,16 @@ DeleteVariable(VariableSpace space, const char *name)
182184
if (!space)
183185
return false;
184186

185-
if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name))
186-
return false;
187-
188-
for (current = space, previous = NULL; current; previous = current, current = current->next)
187+
for (previous = space, current = space->next;
188+
current;
189+
previous = current, current = current->next)
189190
{
190-
psql_assert(current->name);
191-
psql_assert(current->value);
192191
if (strcmp(current->name, name) == 0)
193192
{
193+
psql_assert(current->value);
194+
previous->next = current->next;
194195
free(current->name);
195196
free(current->value);
196-
if (previous)
197-
previous->next = current->next;
198197
free(current);
199198
return true;
200199
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy