Re: Remove redundant strlen call in ReplicationSlotValidateName - Mailing list pgsql-hackers

From Ranier Vilela
Subject Re: Remove redundant strlen call in ReplicationSlotValidateName
Date
Msg-id CAEudQAqSnBq02hLdoYYOSk6aW=94eThdYbAyFETSJnT0nBw6vw@mail.gmail.com
Whole thread Raw
In response to Re: Remove redundant strlen call in ReplicationSlotValidateName  (Ranier Vilela <ranier.vf@gmail.com>)
Responses Re: Remove redundant strlen call in ReplicationSlotValidateName  (Peter Smith <smithpb2250@gmail.com>)
List pgsql-hackers
Em sex., 16 de jul. de 2021 às 13:18, Ranier Vilela <ranier.vf@gmail.com> escreveu:
Em sex., 16 de jul. de 2021 às 12:37, Alvaro Herrera <alvherre@alvh.no-ip.org> escreveu:
On 2021-Jul-16, David Rowley wrote:

> On Fri, 16 Jul 2021 at 20:35, Japin Li <japinli@hotmail.com> wrote:
> > > When I fix a bug about ALTER SUBSCRIPTION ... SET (slot_name) [1], Ranier Vilela
> > > finds that ReplicationSlotValidateName() has redundant strlen() call, Since it's
> > > not related to that problem, so I start a new thread to discuss it.
>
> I think this is a waste of time.  The first strlen() call is just
> checking for an empty string. I imagine all compilers would just
> optimise that to checking if the first char is '\0';

I could find the following idioms

95 times: var[0] == '\0'
146 times: *var == '\0'
35 times: strlen(var) == 0

Resp.
git grep  "[a-zA-Z_]*\[0\] == '\\\\0"
git grep  "\*[a-zA-Z_]* == '\\\\0"
git grep  "strlen([^)]*) == 0"

See https://postgr.es/m/13847.1587332283@sss.pgh.pa.us about replacing
strlen with a check on first byte being zero.  So still not Ranier's
patch, but rather the attached.  I doubt this change is worth committing
on its own, though, since performance-wise it doesn't matter at all; if
somebody were to make it so that all "strlen(foo) == 0" occurrences were
changed to use the test on byte 0, that could be said to be establishing
a consistent style, which might be more pallatable.
Yeah, can be considered a refactoring.

IMHO not in C is free.
I do not think that this will improve 1% generally, but some function can
gain.

Another example I can mention is this little Lua code, in which I made comparisons between the generated asms, made some time ago.

p[0] = luaS_newlstr(L, str, strlen(str));

with strlen (msvc 64 bits):
inc r8
cmp BYTE PTR [r11+r8], 0
jne SHORT $LL19@luaS_new
mov rdx, r11
mov rcx, rdi
call luaS_newlstr

without strlen (msvc 64 bits):
mov r8, rsi
mov rdx, r11
mov QWORD PTR [rbx+8], rax
mov rcx, rdi
call luaS_newlstr

Of course, that doesn't mean anything about Postgres, but that I'm talking about using strlen.
Clearly I can see that usage is not always free.

If have some interest in actually changing that in Postgres, I can prepare a patch,
where static analyzers claim it's performance loss.
I did the patch, but to my surprise, the results weren't so good.
Despite that claiming a tiny improvement in performance, I didn't expect any slowdown.
I put a counter in pg_regress.c, summing the results of each test and did it three times for HEAD and for the patch.
Some tests were better, but others were bad.
Tests comments per example, show 180%, combocid 174%, dbize 165%, xmlmap 136%, lock 134%.


HEAD 1HEAD 2HEAD 3HEAD avg
patch strlen1patch strlen2patch strlen3patch avg











tablespace326328339331
321320335325,333333333333101,74%
boolean63486257,6666666666667
63396254,6666666666667105,49%
char47252432
33212526,3333333333333121,52%
name30332629,6666666666667
34266240,666666666666772,95%
varchar43472638,6666666666667
65315550,333333333333376,82%
text51435349
37575449,333333333333399,32%
int229664647
5758385192,16%
int455354645,3333333333333
51547058,333333333333377,71%
int81038511199,6666666666667
79969188,6666666666667112,41%
oid70857175,3333333333333
45648063119,58%
float468748676
84684967113,43%
float8988710998
95988893,6666666666667104,63%
bit99849191,3333333333333
108969298,666666666666792,57%
numeric414392393399,666666666667
379399411396,333333333333100,84%
txid82846376,3333333333333
71795668,6666666666667111,17%
uuid48967673,3333333333333
7976677499,10%
enum101125102109,333333333333
113117113114,33333333333395,63%
money81777979
45667762,6666666666667126,06%
rangetypes404411415410
42640442141798,32%
pg_lsn60636964
69575159108,47%
regproc69526060,3333333333333
79577670,666666666666785,38%
strings145152139145,333333333333
17013813614898,20%
numerology46353137,3333333333333
34353334109,80%
point45374542,3333333333333
52387655,333333333333376,51%
lseg25151919,6666666666667
33371929,666666666666766,29%
line31302428,3333333333333
20243225,3333333333333111,84%
box259254266259,666666666667
25530730528989,85%
path25205232,3333333333333
3557253982,91%
polygon274269271271,333333333333
247258315273,33333333333399,27%
circle35525547,3333333333333
43335142,3333333333333111,81%
date108829093,3333333333333
106101104103,66666666666790,03%
time67623956
69343646,3333333333333120,86%
timetz38624147
3675334897,92%
timestamp416412425417,666666666667
451431357413101,13%
timestamptz479468474473,666666666667
503461411458,333333333333103,35%
interval811049392,6666666666667
69967981,3333333333333113,93%
inet727410884,6666666666667
83808582,6666666666667102,42%
macaddr37614347
59747268,333333333333368,78%
macaddr854616459,6666666666667
3978786591,79%
multirangetypes278271290279,666666666667
29033326529694,48%
create_function_066375954
48313939,3333333333333137,29%
geometry153136133140,666666666667
131137148138,666666666667101,44%
horology10213298110,666666666667
10810197102108,50%
tstypes81696070
86565465,3333333333333107,14%
regex498515517510
499486510498,333333333333102,34%
type_sanity153132134139,666666666667
16113014414596,32%
opr_sanity389377381382,333333333333
387387363379100,88%
misc_sanity33373735,6666666666667
47343839,666666666666789,92%
comments19563536,6666666666667
30171420,3333333333333180,33%
expressions62446757,6666666666667
52406953,6666666666667107,45%
unicode49522140,6666666666667
53461738,6666666666667105,17%
xid46454043,6666666666667
83813867,333333333333364,85%
mvcc98107111105,333333333333
9511694101,666666666667103,61%
create_function_1910109,66666666666667
101099,66666666666667100,00%
create_type29283029
28292928,6666666666667101,16%
create_table365364364364,333333333333
365363360362,666666666667100,46%
create_function_213121212,3333333333333
13121212,3333333333333100,00%
copy461450458456,333333333333
463454450455,666666666667100,15%
copyselect29293230
30272928,6666666666667104,65%
copydml35353535
37353335100,00%
insert303309338316,666666666667
305287305299105,91%
insert_conflict142141176153
150137154147104,08%
create_misc94939694,3333333333333
10094114102,66666666666791,88%
create_operator24272224,3333333333333
2826242693,59%
create_procedure62646563,6666666666667
73638272,666666666666787,61%
create_index880933866893
896864904888100,56%
create_index_spgist582512574556
59858851556798,06%
create_view341250329306,666666666667
286334292304100,88%
index_including183207204198
215143163173,666666666667114,01%
index_including_gist307362364344,333333333333
413260278317108,62%
create_aggregate62699073,6666666666667
601001078982,77%
create_function_3146162173160,333333333333
151144181158,666666666667101,05%
create_cast26292225,6666666666667
24187137,666666666666768,14%
constraints210218216214,666666666667
239227195220,33333333333397,43%
triggers856920882886
889871861873,666666666667101,41%
select103133101112,333333333333
92133140121,66666666666792,33%
inherit615627671637,666666666667
642646616634,666666666667100,47%
typed_table146129131135,333333333333
115121116117,333333333333115,34%
vacuum494417455455,333333333333
442451402431,666666666667105,48%
drop_if_exists115848394
138635786109,30%
updatable_views645630678651
635644647642101,40%
roleattributes119958098
10513490109,66666666666789,36%
create_am156114126132
15516719117177,19%
hash_func83526165,3333333333333
50509163,6666666666667102,62%
errors54806365,6666666666667
91334155119,39%
infinite_recurse312321361331,333333333333
32934140135792,81%
sanity_check136134134134,666666666667
135134134134,333333333333100,25%
select_into911396698,6666666666667
811187892,3333333333333106,86%
select_distinct228202181203,666666666667
242238214231,33333333333388,04%
select_distinct_on27386041,6666666666667
44362434,6666666666667120,19%
select_implicit77484757,3333333333333
4451946391,01%
select_having38487553,6666666666667
50643951105,23%
subselect274297305292
316351310325,66666666666789,66%
union308285289294
35128928130795,77%
case531374779
815614995,333333333333382,87%
join685718736713
690678793720,33333333333398,98%
aggregates706675697692,666666666667
802680728736,66666666666794,03%
transactions285182273246,666666666667
159247251219112,63%
random44674652,3333333333333
43825058,333333333333389,71%
portals264265189239,333333333333
26324722524597,69%
arrays379355358364
352398339363100,28%
btree_index1550153214761519,33333333333
170815651557161094,37%
hash_index436480477464,333333333333
464447440450,333333333333103,11%
update468464464465,333333333333
52246447248695,75%
delete112434867,6666666666667
10189417787,88%
namespace96505667,3333333333333
13652497985,23%
prepared_xacts185109160151,333333333333
161127175154,33333333333398,06%
brin1689171917401716
1777167117041717,3333333333399,92%
gin1166121311391172,66666666667
1215124310951184,3333333333399,01%
gist1052104510731056,66666666667
1029107210641055100,16%
spgist1013102910941045,33333333333
1009946994983106,34%
privileges876863904881
924942100895891,96%
init_privs27202524
44181726,333333333333391,14%
security_label43496351,6666666666667
8946170101,66666666666750,82%
collate315393346351,333333333333
319222341294119,50%
matview671699663677,666666666667
69867672369996,95%
lock220171177189,333333333333
112132179141134,28%
replica_identity294248235259
314229428323,66666666666780,02%
rowsecurity869888832863
853919933901,66666666666795,71%
object_address244210202218,666666666667
257224301260,66666666666783,89%
tablesample154164194170,666666666667
217149169178,33333333333395,70%
groupingsets644606640630
564637665622101,29%
drop_operator981255592,6666666666667
54727667,3333333333333137,62%
password585572535564
527538465510110,59%
identity529435499487,666666666667
504571453509,33333333333395,75%
generated777672759736
830744734769,33333333333395,67%
join_hash1672170617261701,33333333333
1761166716911706,3333333333399,71%
brin_bloom9797131108,333333333333
9910997101,666666666667106,56%
brin_multi236234269246,333333333333
245236226235,666666666667104,53%
create_table_like281277257271,666666666667
28327728028097,02%
alter_generic124116126122
11913113712994,57%
alter_operator33283130,6666666666667
6136504962,59%
misc161172157163,333333333333
176160167167,66666666666797,42%
async19134024
33602238,333333333333362,61%
dbsize73312242
24193325,3333333333333165,79%
misc_functions84798482,3333333333333
94899291,666666666666789,82%
sysviews971088697
91889290,3333333333333107,38%
tsrf69728073,6666666666667
6096848092,08%
tid84373853
4773575989,83%
tidscan75618172,3333333333333
98779690,333333333333380,07%
tidrangescan83497669,3333333333333
47696359,6666666666667116,20%
collate.icu.utf830372932
16602734,333333333333393,20%
incremental_sort174176163171
160164179167,666666666667101,99%
rules303383315333,666666666667
385376341367,33333333333390,83%
psql253315253273,666666666667
329243297289,66666666666794,48%
psql_crosstab35313032
49333338,333333333333383,48%
amutils18201818,6666666666667
3420182477,78%
stats_ext1530156815721556,66666666667
157616051571158498,27%
collate.linux.utf810111110,6666666666667
17101112,666666666666784,21%
select_parallel786786797789,666666666667
79780782681097,49%
write_parallel78808079,3333333333333
79798480,666666666666798,35%
publication76838180
74757474,3333333333333107,62%
subscription41484845,6666666666667
40434342108,73%
select_views201222199207,333333333333
188205204199104,19%
portals_p257384145,3333333333333
35443437,6666666666667120,35%
foreign_key969955965963
925971947947,666666666667101,62%
cluster407400408405
41743939841896,89%
dependency107212112143,666666666667
17814716416388,14%
guc101184168151
17718116117387,28%
bitmapops448443429440
44446044645097,78%
combocid156144150150
461625086174,42%
tsearch396443424421
404434415417,666666666667100,80%
tsdicts137170166157,666666666667
98157130128,333333333333122,86%
foreign_data633637636635,333333333333
647648629641,33333333333399,06%
window374428391397,666666666667
353374355360,666666666667110,26%
xmlmap1085111390,6666666666667
85456966,3333333333333136,68%
functional_deps177169125157
145162144150,333333333333104,43%
advisory_lock71687972,6666666666667
72578671,6666666666667101,40%
indirect_toast541522554539
567627596596,66666666666790,34%
equivclass123197118146
184178165175,66666666666783,11%
json106105111107,333333333333
116108110111,33333333333396,41%
jsonb246253256251,666666666667
26225325625797,92%
json_encoding17161616,3333333333333
1715191796,08%
jsonpath31323131,3333333333333
3232323297,92%
jsonpath_encoding15141414,3333333333333
15151414,666666666666797,73%
jsonb_jsonpath81818482
8382848398,80%
plancache107103145118,333333333333
131101120117,333333333333100,85%
limit186130127147,666666666667
138167138147,666666666667100,00%
plpgsql725713739725,666666666667
726699722715,666666666667101,40%
copy2324227187246
226137226196,333333333333125,30%
temp215236251234
208178190192121,88%
domain380368391379,666666666667
371351351357,666666666667106,15%
rangefuncs379336371362
354373359362100,00%
prepare164175131156,666666666667
87179149138,333333333333113,25%
conversion190113198167
18625922122275,23%
truncate349362365358,666666666667
285342305310,666666666667115,45%
alter_table1419141513631399
1406140914021405,6666666666799,53%
sequence260227312266,333333333333
236264212237,333333333333112,22%
polymorphism298341357332
342341323335,33333333333399,01%
rowtypes291266284280,333333333333
281254309281,33333333333399,64%
returning1469264100,666666666667
106243108152,33333333333366,08%
largeobject365447415409
426494426448,66666666666791,16%
with385329360358
345286349326,666666666667109,59%
xml160222217199,666666666667
289253228256,66666666666777,79%
partition_join865861861862,333333333333
816924872870,66666666666799,04%
partition_prune783719791764,333333333333
758758781765,66666666666799,83%
reloptions96847886
74748778,3333333333333109,79%
hash_part51433944,3333333333333
46575853,666666666666782,61%
indexing702707685698
73771473973095,62%
partition_aggregate949939884924
96095791594497,88%
partition_info961117794,6666666666667
1118310499,333333333333395,30%
tuplesort1359133913871361,66666666667
1404138113591381,3333333333398,58%
explain971018293,3333333333333
1021027492,6666666666667100,72%
compression164157162161
154149156153105,23%
memoize90958790,6666666666667
80758479,6666666666667113,81%
event_trigger126126135129
127111112116,666666666667110,57%
oidjoins257240252249,666666666667
245243245244,333333333333102,18%
fast_default146145145145,333333333333
148145162151,66666666666795,82%
stats615611611612,333333333333
62161361161599,57%

So I'm posting the patch here, merely as an illustration of my findings.
Perhaps someone with a better understanding of the process of translating C to asm can have an explanation.
Is it worth it to change only where there has been improvement?

regards,
Ranier Vilela
Attachment

pgsql-hackers by date:

Previous
From: Dean Rasheed
Date:
Subject: Re: CREATE COLLATION - check for duplicate options and error out if found one
Next
From: Yugo NAGATA
Date:
Subject: Re: corruption of WAL page header is never reported