0

The context(although not important), in a netfilter module, we use struct like:

struct data {
    char mac[ETH_ALEN];
    char in6_addr addr;
};

to keep track of MAC address and ipv6 address.

for handling ipv6 address changes:

  1. should I compare the new and old value(memcmp), if different, then update(memcpy).
  2. or, I just update it.

both are valid behavior, so performance is the main concern here.

I found one similar question: https://jira.mongodb.org/browse/SERVER-66

It seems they choose the "just update" way.

Ted Feng
  • 829
  • 1
  • 17
  • 22

1 Answers1

1

Best thing is to benchmark it and see if there is any reasonable difference, by assuming a distribution of different addresses similar to the one you get (maybe you should use some real data).

The only thing I can think about is that if you skip a branch choice (and you do if you avoid having if (!memmcmp(..))) then you skip a chance of a fail in the branch predictor.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • Indeed. Although you have to weigh that up against the cost of unnecessary cache-line invalidations. – Oliver Charlesworth Jul 04 '13 at 04:02
  • One less code branch or unnecessary cache-line invalidations? Do we have general predications or we have to benchmark everytime in similar situations. – Ted Feng Jul 05 '13 at 07:38