Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8940461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:02:32+00:00 2026-06-15T11:02:32+00:00

As part of the WebSocket spec , all client-sent frames must have the payload

  • 0

As part of the WebSocket spec, all client-sent frames must have the payload part of the frames masked using a 4-byte mask. In C++, this would be really easy :

for (size_t i = 0; i < length; i++) {
    data[i] ^= mask[i % 4];
}

Sadly, Python strings are immutable and I’d rather avoid having to do something like this, because of the constant copying and re-creation of the string buffer :

frame = ''
for i in range(0, length-1):
    frame += chr(ord(oldFrame[i]) ^ ord(mask[i % 4]))

So after some research, I found this one :

m = itertools.cycle(mask)
frame = ''.join(chr(ord(x) ^ ord(y)) for (x,y) in itertools.izip(oldFrame, m))

In CPython, that halved the time needed for the unmasking. In PyPy, for a 16MB masked string, this easily grows to 1.5GB RAM usage, after which it starts swapping and I have to kill it. CPython uses ‘only’ 150 MB RAM for this 16MB string (and takes 20 seconds), but that’s still bad. In comparison, my C++ benchmark did it in 0.05 seconds with no memory overhead.

Of course these are extremes that won’t actually happen once the software goes in production mode (all incoming data is capped at 10KB), but I would really like to have a good score on this benchmark.

Any ideas? The only requirements are: fast on both CPython and PyPy, and low memory usage. The original string doesn’t have to be preserved.

Some test code for those who want to experiment:

import os, time

frame = os.urandom(16 << 20)
mask = os.urandom(4)

def unmask(oldFrame, mask):
    # Do your magic
    return newFrame

for i in range(0, 3): # Run several times, to help PyPy's JIT compiler
    startTime = time.time()
    f = unmask(frame, mask)
    endTime = time.time()
    print 'This run took %.3f seconds' % (endTime - startTime)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:02:32+00:00Added an answer on June 15, 2026 at 11:02 am

    Use a bytearray instead, which is mutable.

    frame = bytearray(frame)
    for i in range(len(mask)):
        frame[i] ^= mask[i]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing a .toString() on an InetSocketAddress. This is part of an Android websocket
I developed a websocket service and client follow http://blogs.microsoft.co.il/blogs/idof/archive/2012/03/06/what-s-new-in-wcf-4-5-websocket-support-part-2-of-2.aspx . When i test it,i
Part of the GUI I'm building using tkinter has a pop-up window that says
Part of my code I get the OuterHTML propery <LI onclick=TabClicked(this, 'SearchName', 'TabGroup1');>Name so
Part of my code goes like this: while(1){ my $winmm = new Win32::MediaPlayer; $winmm->load('1.mp3');
While working on a WebSocket server in Java I came across this strange bug.
I'm trying to do some experiment with HTML5, WebSocket and File API. I'm using
Part of this assignment includes printing out on the display the current equation that
I have a little WebSocket chat demo that I am working on ( based
How feasible is it to translate this Ruby websocket server to PHP? http://github.com/gimite/web-socket-ruby/blob/master/lib/web_socket.rb I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.