Packet.read_byte reads a byte from the socket and returns it. Before
this commit it would allocate a string and an array, after this commit
it makes 0 allocations.
Here is a benchmark:
require "mqtt/packet"
def count_alloc
x = GC.stat(:total_allocated_objects)
yield
GC.stat(:total_allocated_objects) - x
end
File.open(__FILE__) do |f|
10.times do
allocations = count_alloc do
MQTT::Packet.read_byte f
end
p ALLOCATIONS: allocations
end
end
Packet.read_byte
reads a byte from the socket and returns it. Before this commit it would allocate a string and an array, after this commit it makes 0 allocations.Here is a benchmark:
On the master branch:
On this branch: