Skip to content

Commit 980341b

Browse files
committed
Avoid using text_to_cstring() in levenshtein functions.
Operating directly on the underlying varlena saves palloc and memcpy overhead, which testing shows to be significant. Extracted from a larger patch by Alexander Korotkov.
1 parent aab353a commit 980341b

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

contrib/fuzzystrmatch/fuzzystrmatch.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Joe Conway <mail@joeconway.com>
77
*
8-
* $PostgreSQL: pgsql/contrib/fuzzystrmatch/fuzzystrmatch.c,v 1.32 2010/01/02 16:57:32 momjian Exp $
8+
* $PostgreSQL: pgsql/contrib/fuzzystrmatch/fuzzystrmatch.c,v 1.33 2010/07/29 20:11:48 rhaas Exp $
99
* Copyright (c) 2001-2010, PostgreSQL Global Development Group
1010
* ALL RIGHTS RESERVED;
1111
*
@@ -90,7 +90,7 @@ soundex_code(char letter)
9090
*/
9191
#define MAX_LEVENSHTEIN_STRLEN 255
9292

93-
static int levenshtein_internal(const char *s, const char *t,
93+
static int levenshtein_internal(text *s, text *t,
9494
int ins_c, int del_c, int sub_c);
9595

9696

@@ -191,7 +191,7 @@ getcode(char c)
191191
* cases, but your mileage may vary.
192192
*/
193193
static int
194-
levenshtein_internal(const char *s, const char *t,
194+
levenshtein_internal(text *s, text *t,
195195
int ins_c, int del_c, int sub_c)
196196
{
197197
int m,
@@ -203,8 +203,8 @@ levenshtein_internal(const char *s, const char *t,
203203
const char *x;
204204
const char *y;
205205

206-
m = strlen(s);
207-
n = strlen(t);
206+
m = VARSIZE_ANY_EXHDR(s);
207+
n = VARSIZE_ANY_EXHDR(t);
208208

209209
/*
210210
* We can transform an empty s into t with n insertions, or a non-empty t
@@ -244,7 +244,7 @@ levenshtein_internal(const char *s, const char *t,
244244
prev[i] = i * del_c;
245245

246246
/* Loop through rows of the notional array */
247-
for (y = t, j = 1; j < n; y++, j++)
247+
for (y = VARDATA_ANY(t), j = 1; j < n; y++, j++)
248248
{
249249
int *temp;
250250

@@ -254,7 +254,7 @@ levenshtein_internal(const char *s, const char *t,
254254
*/
255255
curr[0] = j * ins_c;
256256

257-
for (x = s, i = 1; i < m; x++, i++)
257+
for (x = VARDATA_ANY(s), i = 1; i < m; x++, i++)
258258
{
259259
int ins;
260260
int del;
@@ -288,8 +288,8 @@ PG_FUNCTION_INFO_V1(levenshtein_with_costs);
288288
Datum
289289
levenshtein_with_costs(PG_FUNCTION_ARGS)
290290
{
291-
char *src = TextDatumGetCString(PG_GETARG_DATUM(0));
292-
char *dst = TextDatumGetCString(PG_GETARG_DATUM(1));
291+
text *src = PG_GETARG_TEXT_PP(0);
292+
text *dst = PG_GETARG_TEXT_PP(1);
293293
int ins_c = PG_GETARG_INT32(2);
294294
int del_c = PG_GETARG_INT32(3);
295295
int sub_c = PG_GETARG_INT32(4);
@@ -302,8 +302,8 @@ PG_FUNCTION_INFO_V1(levenshtein);
302302
Datum
303303
levenshtein(PG_FUNCTION_ARGS)
304304
{
305-
char *src = TextDatumGetCString(PG_GETARG_DATUM(0));
306-
char *dst = TextDatumGetCString(PG_GETARG_DATUM(1));
305+
text *src = PG_GETARG_TEXT_PP(0);
306+
text *dst = PG_GETARG_TEXT_PP(1);
307307

308308
PG_RETURN_INT32(levenshtein_internal(src, dst, 1, 1, 1));
309309
}

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