Skip to content

Improve performance by preallocating the output buffer#5

Merged
dim13 merged 1 commit intodim13:masterfrom
waterlinked:master
Nov 7, 2025
Merged

Improve performance by preallocating the output buffer#5
dim13 merged 1 commit intodim13:masterfrom
waterlinked:master

Conversation

@wlkh
Copy link
Copy Markdown
Contributor

@wlkh wlkh commented Nov 5, 2025

Thanks for a great package. I've been using this successfully many times.

We have been working on optimizing some software using this package and while profiling we noticed that it cobs.Encode/Decode uses a lot of time on allocating memory. This PR improves the performance on Encode/Decode by preallocating the output buffers leading to much less memory allocation while running. This is most noticeable when encoding larger payloads.

Benchmark before:

$ go test -bench=. --benchmem
goos: linux
goarch: amd64
pkg: github.com/dim13/cobs
cpu: Intel(R) Core(TM) Ultra 9 185H
BenchmarkCobs/32/Encode-22         	17362496	        72.44 ns/op	      40 B/op	       2 allocs/op
BenchmarkCobs/32/Decode-22         	29137506	        40.76 ns/op	      40 B/op	       2 allocs/op
BenchmarkCobs/256/Encode-22        	 2756058	       401.2 ns/op	     776 B/op	       3 allocs/op
BenchmarkCobs/256/Decode-22        	18218660	        65.89 ns/op	     264 B/op	       2 allocs/op
BenchmarkCobs/1024/Encode-22       	  723136	      1687 ns/op	    3080 B/op	       5 allocs/op
BenchmarkCobs/1024/Decode-22       	 2752242	       455.7 ns/op	    3080 B/op	       5 allocs/op
BenchmarkCobs/4096/Encode-22       	  134398	      8721 ns/op	   17672 B/op	       9 allocs/op
BenchmarkCobs/4096/Decode-22       	  673791	      1764 ns/op	   12296 B/op	       8 allocs/op
PASS
ok  	github.com/dim13/cobs	10.792s

And after:

$ go test -bench=. --benchmem
goos: linux
goarch: amd64
pkg: github.com/dim13/cobs
cpu: Intel(R) Core(TM) Ultra 9 185H
BenchmarkCobs/32/Encode-22         	21400770	        53.49 ns/op	      32 B/op	       1 allocs/op
BenchmarkCobs/32/Decode-22         	162552748	         7.383 ns/op	       0 B/op	       0 allocs/op
BenchmarkCobs/256/Encode-22        	 4238409	       280.2 ns/op	     288 B/op	       1 allocs/op
BenchmarkCobs/256/Decode-22        	19978677	        54.38 ns/op	     288 B/op	       1 allocs/op
BenchmarkCobs/1024/Encode-22       	 1000000	      1211 ns/op	    1152 B/op	       1 allocs/op
BenchmarkCobs/1024/Decode-22       	 6245422	       183.0 ns/op	    1152 B/op	       1 allocs/op
BenchmarkCobs/4096/Encode-22       	  235732	      4596 ns/op	    4864 B/op	       1 allocs/op
BenchmarkCobs/4096/Decode-22       	 2016283	       614.8 ns/op	    4864 B/op	       1 allocs/op
PASS
ok  	github.com/dim13/cobs	11.339s

Short summary of this is that:

  • 32 byte encode is 1.2x faster (21400770/17362496)
  • 4096 byte encode is 2.9x faster (2016283/673791)
  • 32 byte decode is 5.5x faster
  • 4096 byte decode is 2.9x faster

@dim13 dim13 merged commit d475db9 into dim13:master Nov 7, 2025
1 check 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