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

Commit109f079

Browse files
committed
I made the patch that improved the performance of replace_text().
The content of the patch is as follows:(1)Create shortcut when subtext was not found.(2)Stop using LEFT and RIGHT macro.In LEFT and RIGHT macro, TEXTPOS is executed by the same content asexecution immediately before. The execution frequency of TEXTPOS can bereduced by using text_substring instead of LEFT and RIGHT macro.(3)Add appendStringInfoText, and use it instead ofappendStringInfoString.There is an overhead of PG_TEXT_GET_STR when appendStringInfoString isexecuted by text type. This can be reduced by appendStringInfoText.(4)Reduce execution of TEXTDUP.The effect of the patch that I measured is as follows:- The Data for test was created by 'pgbench -i'.- Test SQL: select replace(aid, '9', 'A') from accounts;- Test results: Linux(CPU: Pentium III, Compiler option: -O2) original: 1.515s patched: 1.250sAtsushi Ogawa
1 parent4b97d51 commit109f079

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

‎src/backend/utils/adt/varlena.c

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.123 2005/05/30 01:20:50 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.124 2005/07/04 18:56:44 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -48,16 +48,10 @@ typedef struct varlena unknown;
4848
text_length(PointerGetDatum(textp))
4949
#defineTEXTPOS(buf_text,from_sub_text) \
5050
text_position(buf_text, from_sub_text, 1)
51-
#defineTEXTDUP(textp) \
52-
DatumGetTextPCopy(PointerGetDatum(textp))
5351
#defineLEFT(buf_text,from_sub_text) \
5452
text_substring(PointerGetDatum(buf_text), \
5553
1, \
5654
TEXTPOS(buf_text, from_sub_text) - 1, false)
57-
#defineRIGHT(buf_text,from_sub_text,from_sub_text_len) \
58-
text_substring(PointerGetDatum(buf_text), \
59-
TEXTPOS(buf_text, from_sub_text) + (from_sub_text_len), \
60-
-1, true)
6155

6256
staticinttext_cmp(text*arg1,text*arg2);
6357
staticint32text_length(Datumstr);
@@ -67,6 +61,8 @@ static text *text_substring(Datum str,
6761
int32length,
6862
boollength_not_specified);
6963

64+
staticvoidappendStringInfoText(StringInfostr,consttext*t);
65+
7066

7167
/*****************************************************************************
7268
* USER I/O ROUTINES *
@@ -1922,6 +1918,18 @@ byteacmp(PG_FUNCTION_ARGS)
19221918
PG_RETURN_INT32(cmp);
19231919
}
19241920

1921+
/*
1922+
* appendStringInfoText
1923+
*
1924+
* Append a text to str.
1925+
* Like appendStringInfoString(str, PG_TEXT_GET_STR(s)) but faster.
1926+
*/
1927+
staticvoid
1928+
appendStringInfoText(StringInfostr,consttext*t)
1929+
{
1930+
appendBinaryStringInfo(str,VARDATA(t),VARSIZE(t)-VARHDRSZ);
1931+
}
1932+
19251933
/*
19261934
* replace_text
19271935
* replace all occurrences of 'old_sub_str' in 'orig_str'
@@ -1938,36 +1946,45 @@ replace_text(PG_FUNCTION_ARGS)
19381946
text*to_sub_text=PG_GETARG_TEXT_P(2);
19391947
intsrc_text_len=TEXTLEN(src_text);
19401948
intfrom_sub_text_len=TEXTLEN(from_sub_text);
1941-
char*to_sub_str=PG_TEXT_GET_STR(to_sub_text);
19421949
text*left_text;
19431950
text*right_text;
19441951
text*buf_text;
19451952
text*ret_text;
19461953
intcurr_posn;
1947-
StringInfostr=makeStringInfo();
1954+
StringInfostr;
19481955

19491956
if (src_text_len==0||from_sub_text_len==0)
19501957
PG_RETURN_TEXT_P(src_text);
19511958

1952-
buf_text=TEXTDUP(src_text);
1953-
curr_posn=TEXTPOS(buf_text,from_sub_text);
1959+
curr_posn=TEXTPOS(src_text,from_sub_text);
1960+
1961+
/* When the from_sub_text is not found, there is nothing to do. */
1962+
if (curr_posn==0)
1963+
PG_RETURN_TEXT_P(src_text);
1964+
1965+
str=makeStringInfo();
1966+
buf_text=src_text;
19541967

19551968
while (curr_posn>0)
19561969
{
1957-
left_text=LEFT(buf_text,from_sub_text);
1958-
right_text=RIGHT(buf_text,from_sub_text,from_sub_text_len);
1970+
left_text=text_substring(PointerGetDatum(buf_text),
1971+
1,curr_posn-1, false);
1972+
right_text=text_substring(PointerGetDatum(buf_text),
1973+
curr_posn+from_sub_text_len,-1, true);
19591974

1960-
appendStringInfoString(str,PG_TEXT_GET_STR(left_text));
1961-
appendStringInfoString(str,to_sub_str);
1975+
appendStringInfoText(str,left_text);
1976+
appendStringInfoText(str,to_sub_text);
19621977

1963-
pfree(buf_text);
1978+
if (buf_text!=src_text)
1979+
pfree(buf_text);
19641980
pfree(left_text);
19651981
buf_text=right_text;
19661982
curr_posn=TEXTPOS(buf_text,from_sub_text);
19671983
}
19681984

1969-
appendStringInfoString(str,PG_TEXT_GET_STR(buf_text));
1970-
pfree(buf_text);
1985+
appendStringInfoText(str,buf_text);
1986+
if (buf_text!=src_text)
1987+
pfree(buf_text);
19711988

19721989
ret_text=PG_STR_GET_TEXT(str->data);
19731990
pfree(str->data);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp