Re: Remove redundant strlen call in ReplicationSlotValidateName

From: Ranier Vilela <ranier(dot)vf(at)gmail(dot)com>
To: David Rowley <dgrowleyml(at)gmail(dot)com>
Cc: Japin Li <japinli(at)hotmail(dot)com>, Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>, "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Remove redundant strlen call in ReplicationSlotValidateName
Date: 2021-07-16 10:55:39
Message-ID: CAEudQAp_RG3bAF0D5pYjLToFvLQO86ewJM5g4X5OedUQsTq-zg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Em sex., 16 de jul. de 2021 às 07:05, David Rowley <dgrowleyml(at)gmail(dot)com>
escreveu:

> On Fri, 16 Jul 2021 at 20:35, Japin Li <japinli(at)hotmail(dot)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 think with very simple functions, the compiler can do the job.

But, it's not always like that, I think.

https://godbolt.org/z/1jdW3zT58

With gcc 11, I can see clear and different ASM.

strlen1(char const*):
sub rsp, 8
cmp BYTE PTR [rdi], 0
je .L8
call strlen
mov r8, rax
xor eax, eax
cmp r8, 64
ja .L9

strlen2(char const*):
sub rsp, 8
call strlen
test eax, eax
je .L15
xor r8d, r8d
cmp eax, 64
jg .L16

For me strlen2's ASM is much more compact.
And as some functions with strlen are always a hotpath, it's worth it.

regards,
Ranier Vilela

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Dean Rasheed 2021-07-16 11:17:23 Re: CREATE COLLATION - check for duplicate options and error out if found one
Previous Message Japin Li 2021-07-16 10:42:38 Re: Remove redundant strlen call in ReplicationSlotValidateName