Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve unmasking performance #40

Open
mloughran opened this issue Apr 4, 2011 · 3 comments
Open

Improve unmasking performance #40

mloughran opened this issue Apr 4, 2011 · 3 comments
Labels
Milestone

Comments

@mloughran
Copy link
Collaborator

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

@dj2
Copy link
Contributor

dj2 commented Apr 5, 2011

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

@imanel
Copy link
Contributor

imanel commented Apr 5, 2011

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.

@rossta
Copy link

rossta commented Jun 2, 2011

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants