Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
GitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
x/net/websocket: Frame masking code is very inefficient [includes fix] #5269
The code for masking outgoing frames in hybi.go is very inefficient. It starts with an empty byte array and uses append() to append the masked bytes one at a time. This results in a lot of unnecessary allocations, especially with larger frames. It showed up clearly as a hot-spot in some CPU profiling I did with a project that sends frames on the order of 50kbytes. It's very easy to fix this code to preallocate the array to the correct size and copy the masked bytes into it in a tight loop. This has a pretty dramatic effect on performance. Attached is the fix as a tiny two-line patch based on commit adcbdfde2e2f (which was tip a few days ago).