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

entropy: fix race condition, massive performance boost #17

Merged
merged 7 commits into from
Sep 7, 2023

Conversation

yunginnanet
Copy link
Owner

@yunginnanet yunginnanet commented Sep 7, 2023

entropy fixes and overhaul

fix race condition

splitmix64 has a race condition. it likely (usually) doesn't matter all that much, essentially skeeto references a pointer to the instance of splitmix64 directly during the PRNG's regular churn.
in terms of the random values this seems fine, but i missed it because i wasn't using t.Parrallel(). Downstream packages compiled with the race detector of course threw traces.

the fix involves implementing a sync.Pool of splitmix64 pointers without breaking the existing API. it doesn't seem to slow things down, in fact if anything it seems to speed up operations and reduce allocations (go figure).

performance

besides the above prng pool, the random string generators have been overhauled, and their performance has dramatically increased in all areas.

look at this shit;

before

goos: linux
goarch: amd64
pkg: git.tcp.direct/kayos/common/entropy
cpu: 13th Gen Intel(R) Core(TM) i9-13900K
Benchmark_RandStr/5char/run1-32         	 991692	     1164 ns/op	    336 B/op	     17 allocs/op
Benchmark_RandStr/5char/run2-32         	1000000	     1146 ns/op	    336 B/op	     17 allocs/op
Benchmark_RandStr/5char/run3-32         	1000000	     1147 ns/op	    336 B/op	     17 allocs/op
Benchmark_RandStr/5char/run4-32         	 990246	     1146 ns/op	    336 B/op	     17 allocs/op
Benchmark_RandStr/25char/run1-32        	 209188	     5661 ns/op	   1664 B/op	     77 allocs/op
Benchmark_RandStr/25char/run2-32        	 209958	     5661 ns/op	   1664 B/op	     77 allocs/op
Benchmark_RandStr/25char/run3-32        	 209828	     5743 ns/op	   1664 B/op	     77 allocs/op
Benchmark_RandStr/25char/run4-32        	 209623	     5655 ns/op	   1664 B/op	     77 allocs/op
Benchmark_RandStr/55char/run1-32        	  96064	    12418 ns/op	   3648 B/op	    167 allocs/op
Benchmark_RandStr/55char/run2-32        	  95988	    12428 ns/op	   3648 B/op	    167 allocs/op
Benchmark_RandStr/55char/run3-32        	  96433	    12423 ns/op	   3648 B/op	    167 allocs/op
Benchmark_RandStr/55char/run4-32        	  95943	    12418 ns/op	   3648 B/op	    167 allocs/op
Benchmark_RandStr/500char/run1-32       	  10000	   112782 ns/op	  33024 B/op	   1502 allocs/op
Benchmark_RandStr/500char/run2-32       	  10000	   114732 ns/op	  33024 B/op	   1502 allocs/op
Benchmark_RandStr/500char/run3-32       	  10000	   112755 ns/op	  33024 B/op	   1502 allocs/op
Benchmark_RandStr/500char/run4-32       	  10000	   112990 ns/op	  33024 B/op	   1502 allocs/op
Benchmark_RandStr/55555char/run1-32     	     92	 12508040 ns/op	3670224 B/op	 166667 allocs/op
Benchmark_RandStr/55555char/run2-32     	     92	 12507297 ns/op	3670222 B/op	 166667 allocs/op
Benchmark_RandStr/55555char/run3-32     	     93	 12517544 ns/op	3670226 B/op	 166667 allocs/op
Benchmark_RandStr/55555char/run4-32     	     92	 12514224 ns/op	3670222 B/op	 166667 allocs/op
PASS
ok  	git.tcp.direct/kayos/common/entropy	24.257s

after

goos: linux
goarch: amd64
pkg: git.tcp.direct/kayos/common/entropy
cpu: 13th Gen Intel(R) Core(TM) i9-13900K
Benchmark_RandStr/5char/run1-32         	  	24768908	       	48.37 ns/op	        21 B/op	      2 allocs/op
Benchmark_RandStr/5char/run2-32         	  	24689665	       	49.21 ns/op	        21 B/op	      2 allocs/op
Benchmark_RandStr/5char/run3-32         	  	24731571	      	48.09 ns/op	        21 B/op	      2 allocs/op
Benchmark_RandStr/5char/run4-32           		24766143	       	48.16 ns/op	        21 B/op	      2 allocs/op
Benchmark_RandStr/25char/run1-32        	  	11914915	  	99.61 ns/op	        48 B/op	      2 allocs/op
Benchmark_RandStr/25char/run2-32          		11995597	       	99.82 ns/op	        48 B/op	      2 allocs/op
Benchmark_RandStr/25char/run3-32          		11996383	  	99.66 ns/op	        48 B/op	      2 allocs/op
Benchmark_RandStr/25char/run4-32        	  	11901414	        102.3 ns/op	        48 B/op	      2 allocs/op
Benchmark_RandStr/55char/run1-32        	  	6582032  	       	182.4 ns/op	        80 B/op	      2 allocs/op
Benchmark_RandStr/55char/run2-32        	  	6563631	        	182.4 ns/op	        80 B/op	      2 allocs/op
Benchmark_RandStr/55char/run3-32        	  	6564248  		182.3 ns/op	        80 B/op	      2 allocs/op
Benchmark_RandStr/55char/run4-32        	  	6571722	   	   	181.9 ns/op	        80 B/op	      2 allocs/op
Benchmark_RandStr/500char/run1-32       	   	878709	   	    	 1297 ns/op	       529 B/op       2 allocs/op
Benchmark_RandStr/500char/run2-32       	   	878859	  	       	 1295 ns/op	       529 B/op	      2 allocs/op
Benchmark_RandStr/500char/run3-32       	   	879234	  	       	 1325 ns/op	       529 B/op	      2 allocs/op
Benchmark_RandStr/500char/run4-32       	   	883886	  	       	 1296 ns/op	       529 B/op	      2 allocs/op
Benchmark_RandStr/55555char/run1-32     	     	8374	  	       137120 ns/op	     57493 B/op       2 allocs/op
Benchmark_RandStr/55555char/run2-32     	     	8313	  	       137030 ns/op	     57493 B/op       2 allocs/op
Benchmark_RandStr/55555char/run3-32     	     	8277	  	       137153 ns/op	     57493 B/op       2 allocs/op
Benchmark_RandStr/55555char/run4-32     	     	8287	  	       136919 ns/op	     57509 B/op       2 allocs/op
PASS
ok  	git.tcp.direct/kayos/common/entropy	25.016s

@codecov
Copy link

codecov bot commented Sep 7, 2023

Codecov Report

Merging #17 (54c7e8e) into main (85943b8) will not change coverage.
Report is 8 commits behind head on main.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main       #17   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            9         9           
  Lines          676       703   +27     
=========================================
+ Hits           676       703   +27     
Files Changed Coverage Δ
entropy/entropy.go 100.00% <100.00%> (ø)

@yunginnanet yunginnanet merged commit cb6e9c7 into main Sep 7, 2023
5 checks passed
@yunginnanet yunginnanet deleted the fix-entropy-race-condition branch July 3, 2024 08:48
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.

1 participant