midori-rb / midori.rb

Lightweight, Flexible and Fast Ruby Web Framework
MIT License
870 stars 57 forks source link

Faster websocket implementation #166

Closed dsh0416 closed 6 years ago

dsh0416 commented 6 years ago

Decode masked string if opcode is [0x1, 0x9, 0xa] directly instead of returning an array.

dsh0416 commented 6 years ago
require 'benchmark'
websocket = Midori::WebSocket.new(nil)
n = 1_000_000

Benchmark.bm do |x|
  x.report { n.times do websocket.decode(StringIO.new([0x81, 0x85, 0x37, 0xfa, 0x21, 0x3d, 0x7f, 0x9f, 0x4d, 0x51, 0x58].pack('C*'))) end }
  x.report { n.times do websocket.decode(StringIO.new([0x82, 0x85, 0x37, 0xfa, 0x21, 0x3d, 0x7f, 0x9f, 0x4d, 0x51, 0x58].pack('C*'))).pack('C*') end }
end
       user     system      total        real
   3.822925   0.023027   3.845952 (  3.899719)
   4.738750   0.026968   4.765718 (  4.831735)

It gets 19.46% faster for decoding string messages.