RE: Patch avoid call strlen repeatedly in loop. - Mailing list pgsql-hackers

From Ranier VF
Subject RE: Patch avoid call strlen repeatedly in loop.
Date
Msg-id MN2PR18MB2927BDD3EF56FCA70ABA4A13E37A0@MN2PR18MB2927.namprd18.prod.outlook.com
Whole thread Raw
In response to Re: Patch avoid call strlen repeatedly in loop.  (Mark Dilger <hornschnorter@gmail.com>)
List pgsql-hackers
Hi Mark,
"In general, writing a string with snprintf and then calling strlen on
that same string is not guaranteed to give the same lengths.  You can
easily construct a case where they differ:

     char foo[3] = {0};
     int foolen;
     foolen = snprintf(foo, sizeof(foo), "%s", "xxxxxxxx");
     printf("strlen(foo) = %u, foolen = %u, foo = '%s'\n", strlen(foo),
foolen, foo);

Using standard snprintf (and not pg_snprintf), I get:

     strlen(foo) = 2, foolen = 8, foo = 'xx'"

Well, I've been using snprintf, no problem for several years now.
But what you reported, I would easily solve with an assert.

assert(foolen == strlen(foo));

To make sure things would stay under control.

"I think the way it is coded is easier to read, and probably more robust
against future changes, even if your proposed change happens to be safe
today."

I find it amazing that software I admire so much, such as PostgreSQL, makes extensive and heavy use of functions like
strlen.
Speed makes a lot of difference, for some people it is above safety.
Maybe that's why PostgreSQL loses some battles against MySQL.
Not using strlen is for educational purposes as well. Allowing is to encourage use!
So stupid things such as:
#define CheckComplicatedStuff (a, b) (strlen (a) > strlen (b))
for (;;) {
      if CheckComplicatedStuff (x, y) {
         break;
      }
}
They start to contaminate all the code.
Using features like strlen, the programmer begins to create easy shortcuts, but in the end, they are very slow.

Maybe that's why I have things in my code like:
char sql [4096];
PQexec (cn, sql);

While MySQL for example, would look like this:
char sql [4096];
int sql_len;
sql_len = snprintf (sql, sizeof (sql), "INSERT ...");
mysql_real_query (cn, sql, sql_len);

"A bit more information about the performance problem you are
encountering might make it easier to understand the motivation for this
patch."
My motivation? Speed.
Win from MySQL, always.

Anyway I'm redoing the patch with your suggestion.
What about other functions that make extensive use of strlen?

Thank you.
Ranier Vilela

________________________________________
De: Mark Dilger <hornschnorter@gmail.com>
Enviado: sábado, 9 de novembro de 2019 00:12
Para: Ranier VF; pgsql-hackers@lists.postgresql.org
Assunto: Re: Patch avoid call strlen repeatedly in loop.



On 11/8/19 9:41 AM, Ranier VF wrote:
> --- \dll\postgresql-12.0\a\backend\libpq\auth.c       Mon Sep 30 17:06:55 2019
> +++ auth.c    Fri Nov 08 14:27:17 2019
> @@ -1815,6 +1815,7 @@
>       char            ident_user[IDENT_USERNAME_MAX + 1];
>       pgsocket        sock_fd = PGINVALID_SOCKET; /* for talking to Ident server */
>       int                     rc;                             /* Return code from a locally called function */
> +     int                     ident_query_len;
>       bool            ident_return;
>       char            remote_addr_s[NI_MAXHOST];
>       char            remote_port[NI_MAXSERV];
> @@ -1913,7 +1914,7 @@
>       }
>
>       /* The query we send to the Ident server */
> -     snprintf(ident_query, sizeof(ident_query), "%s,%s\r\n",
> +     ident_query_len = snprintf(ident_query, sizeof(ident_query), "%s,%s\r\n",
>                        remote_port, local_port);
>
>       /* loop in case send is interrupted */
> @@ -1921,7 +1922,7 @@
>       {
>               CHECK_FOR_INTERRUPTS();
>
> -             rc = send(sock_fd, ident_query, strlen(ident_query), 0);
> +             rc = send(sock_fd, ident_query, ident_query_len, 0);

Hello Ranier,

In general, writing a string with snprintf and then calling strlen on
that same string is not guaranteed to give the same lengths.  You can
easily construct a case where they differ:

     char foo[3] = {0};
     int foolen;
     foolen = snprintf(foo, sizeof(foo), "%s", "xxxxxxxx");
     printf("strlen(foo) = %u, foolen = %u, foo = '%s'\n", strlen(foo),
foolen, foo);

Using standard snprintf (and not pg_snprintf), I get:

     strlen(foo) = 2, foolen = 8, foo = 'xx'

Perhaps an analysis of the surrounding code would prove that in all
cases this particular snprintf will return the same result that
strlen(ident_query) would return, but I don't care to do the analysis.
I think the way it is coded is easier to read, and probably more robust
against future changes, even if your proposed change happens to be safe
today.

As for calling strlen(ident_query) just once, caching that result, and
then looping, I don't immediately see a problem, but I also don't expect
that loop to run more than one iteration except under unusual instances.
  Do you find that send() gets interrupted a lot?  Is
strlen(ident_query) taking long enough to be significant compared to how
long send() takes?

A bit more information about the performance problem you are
encountering might make it easier to understand the motivation for this
patch.

--
Mark Dilger



pgsql-hackers by date:

Previous
From: Ranier VF
Date:
Subject: RE: Patch avoid call strlen repeatedly in loop.
Next
From: Ranier VF
Date:
Subject: [PATH] spell.c (avoid call strlen repeatedly in loop.