Skip to content

network: PacketMetadata::IsStateOk() is called a lot (and it's slow)

First and foremost, this is something that only affects simulations where the asserts are on - as the function is always called as NS_ASSERT (IsStateOk ());.

Point is, I was trying to figure out if optimising MacAddresses conversions is worth (no, it's not worth) and I noticed that a large part of the time needed by AddHeader or RemoveHeader is actually due to PacketMetadata::IsStateOk().

One option is to optimise it (but it's a simple function, I can't see many obvious optimisations).

The second option is to avoid to be pedantic. E.g., check PacketMetadata::AddHeader:

void 
PacketMetadata::AddHeader (const Header &header, uint32_t size)
{
  NS_LOG_FUNCTION (this << &header << size);
  NS_ASSERT (IsStateOk ());
  uint32_t uid = header.GetInstanceTypeId ().GetUid () << 1;
  DoAddHeader (uid, size);
  NS_ASSERT (IsStateOk ());
}

Here you see that IsStateOk () is called twice, one before adding the header, and one afterwards. The pattern is repeated everywhere - the checks are performed before modifying the metadata, and once more before the return statement.

So, simply removing one of them would mean to have a sensible performance gain.

Point is... which one should we leave? I think that the answer isn't obvious - perhaps it would make sense to leave a check after adding a header, but what about the header removal?

Edited by Tommaso Pecorella