Skip to content

Commit

Permalink
Avoid unnecessary copying of source string when generating a cloned T…
Browse files Browse the repository at this point in the history
…Parser.

For long source strings the copying results in O(N^2) behavior, and the
multiplier can be significant if wide-char conversion is involved.

Andres Freund, reviewed by Kevin Grittner.
  • Loading branch information
tglsfdc committed Dec 15, 2009
1 parent a5495cd commit 21d11e7
Showing 1 changed file with 69 additions and 5 deletions.
74 changes: 69 additions & 5 deletions src/backend/tsearch/wparser_def.c
Expand Up @@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/tsearch/wparser_def.c,v 1.25 2009/11/15 13:57:01 petere Exp $
* $PostgreSQL: pgsql/src/backend/tsearch/wparser_def.c,v 1.26 2009/12/15 20:37:17 tgl Exp $
*
*-------------------------------------------------------------------------
*/
Expand Down Expand Up @@ -328,6 +328,46 @@ TParserInit(char *str, int len)
return prs;
}

/*
* As an alternative to a full TParserInit one can create a
* TParserCopy which basically is a regular TParser without a private
* copy of the string - instead it uses the one from another TParser.
* This is useful because at some places TParsers are created
* recursively and the repeated copying around of the strings can
* cause major inefficiency if the source string is long.
* The new parser starts parsing at the original's current position.
*
* Obviously one must not close the original TParser before the copy.
*/
static TParser *
TParserCopyInit(const TParser *orig)
{
TParser *prs = (TParser *) palloc0(sizeof(TParser));

prs->charmaxlen = orig->charmaxlen;
prs->str = orig->str + orig->state->posbyte;
prs->lenstr = orig->lenstr - orig->state->posbyte;

#ifdef USE_WIDE_UPPER_LOWER
prs->usewide = orig->usewide;

if (orig->pgwstr)
prs->pgwstr = orig->pgwstr + orig->state->poschar;
if (orig->wstr)
prs->wstr = orig->wstr + orig->state->poschar;
#endif

prs->state = newTParserPosition(NULL);
prs->state->state = TPS_Base;

#ifdef WPARSER_TRACE
fprintf(stderr, "parsing copy of \"%.*s\"\n", prs->lenstr, prs->str);
#endif

return prs;
}


static void
TParserClose(TParser *prs)
{
Expand All @@ -346,9 +386,33 @@ TParserClose(TParser *prs)
pfree(prs->pgwstr);
#endif

#ifdef WPARSER_TRACE
fprintf(stderr, "closing parser");
#endif
pfree(prs);
}

/*
* Close a parser created with TParserCopyInit
*/
static void
TParserCopyClose(TParser *prs)
{
while (prs->state)
{
TParserPosition *ptr = prs->state->prev;

pfree(prs->state);
prs->state = ptr;
}

#ifdef WPARSER_TRACE
fprintf(stderr, "closing parser copy");
#endif
pfree(prs);
}


/*
* Character-type support functions, equivalent to is* macros, but
* working with any possible encodings and locales. Notes:
Expand Down Expand Up @@ -617,7 +681,7 @@ p_isignore(TParser *prs)
static int
p_ishost(TParser *prs)
{
TParser *tmpprs = TParserInit(prs->str + prs->state->posbyte, prs->lenstr - prs->state->posbyte);
TParser *tmpprs = TParserCopyInit(prs);
int res = 0;

tmpprs->wanthost = true;
Expand All @@ -631,15 +695,15 @@ p_ishost(TParser *prs)
prs->state->charlen = tmpprs->state->charlen;
res = 1;
}
TParserClose(tmpprs);
TParserCopyClose(tmpprs);

return res;
}

static int
p_isURLPath(TParser *prs)
{
TParser *tmpprs = TParserInit(prs->str + prs->state->posbyte, prs->lenstr - prs->state->posbyte);
TParser *tmpprs = TParserCopyInit(prs);
int res = 0;

tmpprs->state = newTParserPosition(tmpprs->state);
Expand All @@ -654,7 +718,7 @@ p_isURLPath(TParser *prs)
prs->state->charlen = tmpprs->state->charlen;
res = 1;
}
TParserClose(tmpprs);
TParserCopyClose(tmpprs);

return res;
}
Expand Down

0 comments on commit 21d11e7

Please sign in to comment.