Browse files

Optimized message decoding

One #unpack call, with a constant format specifier, yields about 50% speedup in message decoding.
  • Loading branch information...
1 parent 984161d commit db78d65aaad9924bf988edd26fd779d373314cfe @iconara iconara committed May 7, 2012
Showing with 3 additions and 4 deletions.
  1. +3 −4 lib/kafka/message.rb
View
7 lib/kafka/message.rb
@@ -21,6 +21,7 @@ module Kafka
class Message
MAGIC_IDENTIFIER_DEFAULT = 0
+ MESSAGE_HEADER_FORMAT = 'NCN'.freeze
attr_accessor :magic, :checksum, :payload
@@ -39,11 +40,9 @@ def valid?
end
def self.parse_from(binary)
- size = binary[0, 4].unpack("N").shift.to_i
- magic = binary[4, 1].unpack("C").shift
- checksum = binary[5, 4].unpack("N").shift
+ size, magic, checksum = binary.unpack(MESSAGE_HEADER_FORMAT)
payload = binary[9, size] # 5 = 1 + 4 is Magic + Checksum
- return Kafka::Message.new(payload, magic, checksum)
+ Kafka::Message.new(payload, magic, checksum)
end
end
end

0 comments on commit db78d65

Please sign in to comment.