Re: strncmp->memcmp when we know the shorter length

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Noah Misch <noah(at)leadboat(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: strncmp->memcmp when we know the shorter length
Date: 2010-12-22 03:15:41
Message-ID: AANLkTikxwUzS7-hMfpJZY2GkFRRH+MZDiZiWzpcSzkbM@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Dec 21, 2010 at 6:24 PM, Robert Haas <robertmhaas(at)gmail(dot)com> wrote:
> On Mon, Dec 20, 2010 at 1:10 PM, Noah Misch <noah(at)leadboat(dot)com> wrote:
>> When the caller knows the smaller string length, memcmp and strncmp are
>> functionally equivalent.  Since memcmp need not watch each byte for a NULL
>> terminator, it often compares a CPU word at a time for better performance.  The
>> attached patch changes use of strncmp to memcmp where we have the length of the
>> shorter string.  I was most interested in the varlena.c instances, but I tried
>> to find all applicable call sites.  To benchmark it, I used the attached
>> "bench-texteq.sql".  This patch improved my 5-run average timing of the SELECT
>> from 65.8s to 56.9s, a 13% improvement.  I can't think of a case where the
>> change should be pessimal.
>
> This is a good idea.  I will check this over and commit it.

A little benchmarking reveals that on my system (MacOS X 10.6.5) it
appears that strncmp() is faster for a 4 character string, but
memcmp() is faster for a 5+ character string. So I think most of
these are pretty clear wins, but I have reverted the changes to
src/backend/tsearch because I'm not entirely confident that lexemes
and affixes will be long enough on average for this to be a win there.
Please feel free to resubmit that part with performance results
showing that it works out to a win. Some of the ltree changes
produced compiler warnings, so I omitted those also. Committed the
rest.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2010-12-22 03:24:47 Re: strncmp->memcmp when we know the shorter length
Previous Message Tom Lane 2010-12-22 03:14:27 Re: bug in SignalSomeChildren