Commit 6e5c2f3f authored by Rusty Russell's avatar Rusty Russell

mem: get clever with memeqzero().

Best of both worlds.

Before:
1: 6ns
2: 7ns
4: 7ns
8: 7ns
16: 7ns
32: 8ns
64: 9ns
128: 13ns
256: 24ns
512: 47ns
1024: 92ns
2048: 185ns
4096: 376ns
8192: 739ns
16384: 1463ns
32768: 2914ns
65536: 5800ns
2: 7ns
3: 7ns
5: 7ns
9: 7ns
17: 7ns
33: 8ns
65: 9ns
129: 20ns
257: 31ns
513: 49ns
1025: 96ns
2049: 189ns
4097: 381ns
8193: 745ns
16385: 1477ns
32769: 2930ns
65537: 5824ns
total = 599391004

After:
1: 3ns
2: 3ns
4: 4ns
8: 5ns
16: 12ns
32: 13ns
64: 15ns
128: 19ns
256: 25ns
512: 35ns
1024: 57ns
2048: 105ns
4096: 183ns
8192: 324ns
16384: 607ns
32768: 1317ns
65536: 2774ns
2: 3ns
3: 3ns
5: 4ns
9: 6ns
17: 11ns
33: 13ns
65: 14ns
129: 19ns
257: 24ns
513: 35ns
1025: 57ns
2049: 106ns
4097: 183ns
8193: 324ns
16385: 607ns
32769: 1315ns
65537: 2773ns
total = 599391004
Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent b967dac8
......@@ -92,13 +92,18 @@ void memswap(void *a, void *b, size_t n)
bool memeqzero(const void *data, size_t length)
{
const unsigned char *p = data;
static unsigned long zeroes[16];
size_t len;
while (length > sizeof(zeroes)) {
if (memcmp(zeroes, p, sizeof(zeroes)))
/* Check first 16 bytes manually */
for (len = 0; len < 16; len++) {
if (!length)
return true;
if (*p)
return false;
p += sizeof(zeroes);
length -= sizeof(zeroes);
p++;
length--;
}
return memcmp(zeroes, p, length) == 0;
/* Now we know that's zero, memcmp with self. */
return memcmp(data, p, length) == 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment