Skip to contentSkip to conversation

memcmp() is slow?

DISCUSSION Maciej Soltysiak 3 participants 4 messages Jan 22, 2005 – Jan 23, 2005 1 message not in the archive netfilter

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

Hi,

I have seen a patch that replaces memcmp() with a few comparison statements. The log for the change says that memcmp() is slow.

Why is it? Is it just due to the overhead of calling a function and going through the memory in a loop? Or are there other issues I am not aware of.

Ragards,
Maciej

Below is the mention patch:

trunk/patch-o-matic-ng/nf_conntrack/linux-2.6/include/linux/netfilter/nf_conntrack_tuple.h

2005-01-22 19:05:07 UTC (rev 3607)

@@ -142,19 +142,25 @@
static inline int nf_ct_tuple_src_equal(const struct nf_conntrack_tuple *t1,
const struct nf_conntrack_tuple *t2)
{
- return (!memcmp(t1->src.u3.all, t2->src.u3.all, sizeof(t1->src.u3.all)))

-              && (t1->src.u.all == t2->src.u.all)
-              && (t1->src.l3num == t2->src.l3num)
-              && (t1->dst.protonum == t2->dst.protonum);
+       return (t1->>src.u3.all[0] == t2->src.u3.all[0] &&
+               t1->>src.u3.all[1] == t2->src.u3.all[1] &&
+               t1->>src.u3.all[2] == t2->src.u3.all[2] &&
+               t1->>src.u3.all[3] == t2->src.u3.all[3] &&
+               t1->>src.u.all == t2->src.u.all &&
+               t1->>src.l3num == t2->src.l3num &&
+               t1->>dst.protonum == t2->dst.protonum);
Quoted 1 line

}

#2 lore raw
Quoted 4 lines · On Sat, 22 Jan 2005 21:31:52 +0100 Maciej Soltysiak <[email protected]>…

I have seen a patch that replaces memcmp() with a few comparison statements. The log for the change says that memcmp() is slow.

Why is it?

This is a platform specific question, as memcmp() is implemented differently on each architecture. So tell us what you're test was run on. :)

#3 lore raw

Hello David,

Quoted 3 lines · you

This is a platform specific question, as memcmp() is implemented differently on each architecture. So tell us what you're test was run on. :)

Well, it was yoshfuji's patch that suggested the change explaining that memcmp() is slow.

If joshfuji suggests a change like that, I think he is pretty sure that for all archs' memcmp() of 4 integers is slower than simply comparing them.

Anyway I was not aware of such a multiplicity of memcmp() and friends implementations for different archs. Good to know :-)

Regards,
Maciej

#4 lore raw

Hi,

From: Maciej Soltysiak <[email protected]>
Date: Sun, 23 Jan 2005 10:57:09 +0100

Quoted 8 lines

Hello David,

Quoted 3 lines · you

This is a platform specific question, as memcmp() is implemented differently on each architecture. So tell us what you're test was run on. :)

Quoted 6 lines

Well, it was yoshfuji's patch that suggested the change explaining that memcmp() is slow.

If joshfuji suggests a change like that, I think he is pretty sure that for all archs' memcmp() of 4 integers is slower than simply comparing them.

Yoshifuji and I think the reason is that memcmp() distinguishes 3 cases and this code needs to distinguish only 2 cases (equal or not).

I run this code on Pentium M 1.1GHz. But I'm not sure it's true on other archs. Should I revert it ?

-----------------------------------------------------------------
Yasuyuki KOZAKAI @ USAGI Project <[email protected]>