Skip to content

Allow buffer reuse in ReadHeader/WriteHeader variants.#219

Merged
cristaloleg merged 1 commit intogobwas:masterfrom
iamtakingiteasy:master
Feb 12, 2026
Merged

Allow buffer reuse in ReadHeader/WriteHeader variants.#219
cristaloleg merged 1 commit intogobwas:masterfrom
iamtakingiteasy:master

Conversation

@iamtakingiteasy
Copy link
Contributor

@iamtakingiteasy iamtakingiteasy commented Jan 26, 2026

Currently each ReadHeader/WriteHeader call incurs allocation penalty. This MR allows user to provide own buffer to those calls by adding ReadHeaderBuffer/WriteHeaderBuffer variants.

Benchmarks:

ReadHeader
goos: linux
goarch: amd64
pkg: github.com/gobwas/ws
cpu: AMD Ryzen 7 PRO 8840U w/ Radeon 780M Graphics  
BenchmarkReadHeader
BenchmarkReadHeader/no-mask#0
BenchmarkReadHeader/no-mask#0-16         	54530907	        21.27 ns/op	      16 B/op	       1 allocs/op
BenchmarkReadHeader/reused-buffer-no-mask#0
BenchmarkReadHeader/reused-buffer-no-mask#0-16         	152444149	         7.811 ns/op	       0 B/op	       0 allocs/op
BenchmarkReadHeader/mask#1
BenchmarkReadHeader/mask#1-16                          	46440010	        23.27 ns/op	      16 B/op	       1 allocs/op
BenchmarkReadHeader/reused-buffer-mask#1
BenchmarkReadHeader/reused-buffer-mask#1-16            	74732881	        14.33 ns/op	       0 B/op	       0 allocs/op
BenchmarkReadHeader/mask-u16#2
BenchmarkReadHeader/mask-u16#2-16                      	46687669	        23.77 ns/op	      16 B/op	       1 allocs/op
BenchmarkReadHeader/reused-buffer-mask-u16#2
BenchmarkReadHeader/reused-buffer-mask-u16#2-16        	72172458	        15.00 ns/op	       0 B/op	       0 allocs/op
BenchmarkReadHeader/mask-u64#3
BenchmarkReadHeader/mask-u64#3-16                      	42360039	        24.57 ns/op	      16 B/op	       1 allocs/op
BenchmarkReadHeader/reused-buffer-mask-u64#3
BenchmarkReadHeader/reused-buffer-mask-u64#3-16        	71174119	        15.51 ns/op	       0 B/op	       0 allocs/op
PASS
WriteHeader
goos: linux
goarch: amd64
pkg: github.com/gobwas/ws
cpu: AMD Ryzen 7 PRO 8840U w/ Radeon 780M Graphics  
BenchmarkWriteHeader
BenchmarkWriteHeader/no-mask
BenchmarkWriteHeader/no-mask-16         	80543956	        14.90 ns/op	      16 B/op	       1 allocs/op
BenchmarkWriteHeader/reused-buffer-no-mask
BenchmarkWriteHeader/reused-buffer-no-mask-16         	338765588	         3.562 ns/op	       0 B/op	       0 allocs/op
BenchmarkWriteHeader/mask
BenchmarkWriteHeader/mask-16                          	78015270	        15.51 ns/op	      16 B/op	       1 allocs/op
BenchmarkWriteHeader/reused-buffer-mask
BenchmarkWriteHeader/reused-buffer-mask-16            	294887298	         3.994 ns/op	       0 B/op	       0 allocs/op
BenchmarkWriteHeader/mask-u16
BenchmarkWriteHeader/mask-u16-16                      	70865414	        15.52 ns/op	      16 B/op	       1 allocs/op
BenchmarkWriteHeader/reused-buffer-mask-u16
BenchmarkWriteHeader/reused-buffer-mask-u16-16        	277524866	         4.278 ns/op	       0 B/op	       0 allocs/op
BenchmarkWriteHeader/mask-u64
BenchmarkWriteHeader/mask-u64-16                      	72674092	        15.71 ns/op	      16 B/op	       1 allocs/op
BenchmarkWriteHeader/reused-buffer-mask-u64
BenchmarkWriteHeader/reused-buffer-mask-u64-16        	274714274	         4.246 ns/op	       0 B/op	       0 allocs/op
PASS

@iamtakingiteasy
Copy link
Contributor Author

Fixed writing with varying opcodes. Also looks like autobahn test suite is broken, there are python errors during building of the docker image.

 > [4/5] RUN pip install -U pip typing && 	pip install autobahntestsuite=='0.8.2':
15.95         _install_setup_requires(attrs)
15.95       File "/tmp/pip-build-env-E_gbU4/overlay/site-packages/setuptools/__init__.py", line 156, in _install_setup_requires
15.95         dist.fetch_build_eggs(dist.setup_requires)
15.95       File "/tmp/pip-build-env-E_gbU4/overlay/site-packages/setuptools/dist.py", line 721, in fetch_build_eggs
15.95         replace_conflicting=True,
15.95       File "/tmp/pip-build-env-E_gbU4/overlay/site-packages/pkg_resources/__init__.py", line 786, in resolve
15.95         raise DistributionNotFound(req, requirers)
15.95     DistributionNotFound: The 'pycparser' distribution was not found and is required by cffi
15.95     ----------------------------------------

@cristaloleg cristaloleg merged commit 6ee0554 into gobwas:master Feb 12, 2026
6 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants