Skip to content

Commit 8fbef3b

Browse files
committed
Reduce need for palloc/pfree overhead in varstr_cmp() by using fixed-size
buffers on stack for short strings.
1 parent 35223af commit 8fbef3b

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

src/backend/utils/adt/varlena.c

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.92 2002/09/04 20:31:29 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.93 2002/11/17 23:01:30 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -736,36 +736,47 @@ int
736736
varstr_cmp(char *arg1, int len1, char *arg2, int len2)
737737
{
738738
int result;
739-
char *a1p,
740-
*a2p;
741739

742740
/*
743741
* Unfortunately, there is no strncoll(), so in the non-C locale case
744742
* we have to do some memory copying. This turns out to be
745743
* significantly slower, so we optimize the case where LC_COLLATE is
746-
* C.
744+
* C. We also try to optimize relatively-short strings by avoiding
745+
* palloc/pfree overhead.
747746
*/
747+
#define STACKBUFLEN 1024
748+
748749
if (!lc_collate_is_c())
749750
{
750-
a1p = (char *) palloc(len1 + 1);
751-
a2p = (char *) palloc(len2 + 1);
751+
char a1buf[STACKBUFLEN];
752+
char a2buf[STACKBUFLEN];
753+
char *a1p,
754+
*a2p;
755+
756+
if (len1 >= STACKBUFLEN)
757+
a1p = (char *) palloc(len1 + 1);
758+
else
759+
a1p = a1buf;
760+
if (len2 >= STACKBUFLEN)
761+
a2p = (char *) palloc(len2 + 1);
762+
else
763+
a2p = a2buf;
752764

753765
memcpy(a1p, arg1, len1);
754-
*(a1p + len1) = '\0';
766+
a1p[len1] = '\0';
755767
memcpy(a2p, arg2, len2);
756-
*(a2p + len2) = '\0';
768+
a2p[len2] = '\0';
757769

758770
result = strcoll(a1p, a2p);
759771

760-
pfree(a1p);
761-
pfree(a2p);
772+
if (len1 >= STACKBUFLEN)
773+
pfree(a1p);
774+
if (len2 >= STACKBUFLEN)
775+
pfree(a2p);
762776
}
763777
else
764778
{
765-
a1p = arg1;
766-
a2p = arg2;
767-
768-
result = strncmp(a1p, a2p, Min(len1, len2));
779+
result = strncmp(arg1, arg2, Min(len1, len2));
769780
if ((result == 0) && (len1 != len2))
770781
result = (len1 < len2) ? -1 : 1;
771782
}

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