todb/packetfu — pull request #12 by justfalter (2012-07-29 03:54:27+00:00)
Title: Performance improvements

Hi there,

I got inspired today, and put together some more performance improvements.

I broke out many of the components (EthHeader, TCPHeader, etc) into their own files. This was simply to make it easier for me to read things.

I reduced (possibly removed) reliance upon PacketFu::Packet#method_missing? by creating mixins that enable access to *Header methods. method_missing? is hella-slow.

Probably the biggest gain: I removed redundant calls to @ip_header.read(..), @tcp_header.read(..), etc in *Packet.read() methods. These were already being called from within *Header.read() when 'self[:body].read(...)' was being called.

Changed all instances of 'str.force_encoding "binary"' to 'str.force_encoding Encoding::BINARY' ... The former would result in extra string/hash lookups within the encoding library.

The net result is as follows:
Parsing TCP Packets:
Packet.parse() is now ~2 times faster.
TCPPacket.new.read() is now ~2.15 times faster

Parsing UDP Packets:
Packet.parse() is now ~1.45 times faster
UDPPacket.new.read() is now ~1.47 times faster

Parsing IPv6 Packets:
Packet.parse() is now ~1.07 times faster
IPv6Packet.new.read() is now ~1.15 times faster.