em-websocket
em-websocket copied to clipboard
Improve unmasking performance
In drafts 04 and above all data sent by browsers must be XORed with 4 random bytes which is described in http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-06#section-4.2
I wrote the simplest code that could possibly work to do this. It works, but the performance stinks. Unmasking 1MB (1000 1K strings) takes 1.75s on my laptop. Does anyone have any bright ideas about nice ways to rewrite the code in https://github.com/igrigorik/em-websocket/blob/draft5and6/lib/em-websocket/masking04.rb ?
Please contribute to https://gist.github.com/902183
Played with it a bit. My version is destructive with the input string but went from ~1.28s to ~0.89s on my machine. Gist is at: https://gist.github.com/902942
I just tested fast_xor gem, and for this gist: https://gist.github.com/903261 i have following results:
(changed n from 1_000 to 10_000)
old:
MaskedString: 4.800000 0.010000 4.810000 ( 4.811276)
new:
MaskedString: 0.060000 0.000000 0.060000 ( 0.062952)
I know that implementation in this gist is crap, but it was 1-minute for testing purpose.
Building on imanel's approach with the fast_xor gem with some fixes and test cases: https://gist.github.com/1004242 Results below for n = 1000.
ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-darwin10.6.0], MBARI 0x6770, Ruby Enterprise Edition 2011.03
MaskedString getbytes-orig: 1.610000 0.010000 1.620000 ( 1.615039)
MaskedString getbytes-fast: 0.010000 0.000000 0.010000 ( 0.010197)