strncmp->memcmp when we know the shorter length

From: Noah Misch <noah(at)leadboat(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: strncmp->memcmp when we know the shorter length
Date: 2010-12-20 18:10:42
Message-ID: 20101220181042.GA29282@tornado.gateway.2wire.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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.

Thanks,
nm

Attachment Content-Type Size
strncmp-memcmp.patch text/plain 15.6 KB
bench-texteq.sql text/plain 829 bytes

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Noah Misch 2010-12-20 18:19:53 texteq/byteaeq: avoid detoast
Previous Message Robert Haas 2010-12-20 18:06:43 Re: relaxing sync commit if no WAL written (was Re: unlogged tables)