go-mc icon indicating copy to clipboard operation
go-mc copied to clipboard

some enhancement for Mojang login and some performance enhancement in packet level

Open cs8425 opened this issue 1 year ago • 8 comments

As mentioned in #209, I add KeyManager for caching RSA keypair, not yet add the option for prevent-proxy-connections: true right now.

And this PR also include some performance enhancement in packet level, which:

  1. use sync.Pool for bytes.NewReader() in Packet.Scan()
  2. switch standard library compress/zlib to third party github.com/klauspost/compress/zlib
  3. add option for setting compress level

cs8425 avatar Jul 20 '22 19:07 cs8425

I don't believe the first change can improve the performance.

bytes.NewReader doesn't allocate any memory on heap. Using sync.Pool in this case only cause the variables escape from stack(or registers) to heap, which decreases the performance actually.

Tnze avatar Jul 20 '22 22:07 Tnze

And I don't like to switch the zlib implementation, unless you can prove it's necessary and the third-party library is reliable.

(I means may be it has performance improvement but also vulnerabilities)

Tnze avatar Jul 20 '22 23:07 Tnze

I don't believe the first change can improve the performance.

bytes.NewReader doesn't allocate any memory on heap. Using sync.Pool in this case only cause the variables escape from stack(or registers) to heap, which decreases the performance actually.

I will try to make a benchmark, and check this one.

And I don't like to switch the zlib implementation, unless you can prove it's necessary and the third-party library is reliable.

(I means may be it has performance improvement but also vulnerabilities)

I had add a little benchmark code for this, and I think if we only use zlib in standard library, then we need to use BestSpeed (level 1) as default.

ps. I'm building a L7 proxy which can filter out malicious traffic (like WAF in web), need low level access/modify on packet level, neither BungeeCord nor Velocity can do this. So the performance in packet level are important to me.

here is the benchmark result:

standard library with default compress level (5):

$ go test -bench=. -benchmem -cpu=1,2,4,8 -run=^#
goos: linux
goarch: amd64
pkg: github.com/Tnze/go-mc/net
cpu: Intel(R) Core(TM) i3-10100 CPU @ 3.60GHz
BenchmarkConnTCPCompress                    	    2467	    475211 ns/op	 275.82 MB/s	   40945 B/op	      13 allocs/op
BenchmarkConnTCPCompress-2                  	    3016	    396766 ns/op	 330.35 MB/s	   40888 B/op	      13 allocs/op
BenchmarkConnTCPCompress-4                  	    2979	    404220 ns/op	 324.26 MB/s	   40912 B/op	      13 allocs/op
BenchmarkConnTCPCompress-8                  	    2907	    406397 ns/op	 322.52 MB/s	   40940 B/op	      13 allocs/op
BenchmarkConnTCPCompressLevelDefault        	    2462	    469518 ns/op	 279.16 MB/s	   40945 B/op	      13 allocs/op
BenchmarkConnTCPCompressLevelDefault-2      	    3058	    412957 ns/op	 317.40 MB/s	   40885 B/op	      13 allocs/op
BenchmarkConnTCPCompressLevelDefault-4      	    2978	    405987 ns/op	 322.85 MB/s	   40913 B/op	      13 allocs/op
BenchmarkConnTCPCompressLevelDefault-8      	    2928	    402088 ns/op	 325.98 MB/s	   40939 B/op	      13 allocs/op
BenchmarkConnTCPCompressLevelBestSpeed      	    5551	    193154 ns/op	 678.59 MB/s	   40833 B/op	      13 allocs/op
BenchmarkConnTCPCompressLevelBestSpeed-2    	    9980	    116114 ns/op	1128.82 MB/s	   40741 B/op	      13 allocs/op
BenchmarkConnTCPCompressLevelBestSpeed-4    	    9666	    129276 ns/op	1013.89 MB/s	   40768 B/op	      13 allocs/op
BenchmarkConnTCPCompressLevelBestSpeed-8    	    9777	    130696 ns/op	1002.88 MB/s	   40788 B/op	      13 allocs/op
BenchmarkConnTCPNoCompress                  	   50786	     23776 ns/op	5512.86 MB/s	      14 B/op	       6 allocs/op
BenchmarkConnTCPNoCompress-2                	   51469	     22816 ns/op	5744.70 MB/s	      14 B/op	       6 allocs/op
BenchmarkConnTCPNoCompress-4                	   52388	     23631 ns/op	5546.65 MB/s	      14 B/op	       6 allocs/op
BenchmarkConnTCPNoCompress-8                	   51853	     22698 ns/op	5774.52 MB/s	      14 B/op	       6 allocs/op
BenchmarkConnPipeCompress                   	    2565	    464185 ns/op	 282.37 MB/s	   40932 B/op	      13 allocs/op
BenchmarkConnPipeCompress-2                 	    2551	    467944 ns/op	 280.10 MB/s	   40954 B/op	      13 allocs/op
BenchmarkConnPipeCompress-4                 	    2520	    466229 ns/op	 281.13 MB/s	   40988 B/op	      13 allocs/op
BenchmarkConnPipeCompress-8                 	    2454	    468174 ns/op	 279.96 MB/s	   41026 B/op	      13 allocs/op
BenchmarkConnPipeCompressLevelDefault       	    2503	    462964 ns/op	 283.12 MB/s	   40940 B/op	      13 allocs/op
BenchmarkConnPipeCompressLevelDefault-2     	    2505	    493891 ns/op	 265.39 MB/s	   40954 B/op	      13 allocs/op
BenchmarkConnPipeCompressLevelDefault-4     	    2379	    495828 ns/op	 264.35 MB/s	   41007 B/op	      13 allocs/op
BenchmarkConnPipeCompressLevelDefault-8     	    2395	    493343 ns/op	 265.68 MB/s	   41034 B/op	      13 allocs/op
BenchmarkConnPipeCompressLevelBestSpeed     	    6310	    189067 ns/op	 693.26 MB/s	   40809 B/op	      13 allocs/op
BenchmarkConnPipeCompressLevelBestSpeed-2   	    6751	    170305 ns/op	 769.63 MB/s	   40807 B/op	      13 allocs/op
BenchmarkConnPipeCompressLevelBestSpeed-4   	    7082	    159598 ns/op	 821.27 MB/s	   40829 B/op	      13 allocs/op
BenchmarkConnPipeCompressLevelBestSpeed-8   	    6976	    161709 ns/op	 810.54 MB/s	   40845 B/op	      13 allocs/op
BenchmarkConnPipeNoCompress                 	  161882	      7238 ns/op	18107.81 MB/s	      16 B/op	       6 allocs/op
BenchmarkConnPipeNoCompress-2               	  151468	      7982 ns/op	16421.27 MB/s	      16 B/op	       6 allocs/op
BenchmarkConnPipeNoCompress-4               	  154296	      7718 ns/op	16983.31 MB/s	      16 B/op	       6 allocs/op
BenchmarkConnPipeNoCompress-8               	  150906	      7898 ns/op	16595.25 MB/s	      16 B/op	       6 allocs/op
PASS
ok  	github.com/Tnze/go-mc/net	40.928s
$ go test -bench=. -benchmem -cpu=1,2,4,8,16 -run=^#
goos: linux
goarch: amd64
pkg: github.com/Tnze/go-mc/net
cpu: AMD Ryzen 7 5700G with Radeon Graphics         
BenchmarkConnTCPCompress                      	    2791	    395030 ns/op	 331.80 MB/s	   40907 B/op	      13 allocs/op
BenchmarkConnTCPCompress-2                    	    2863	    446081 ns/op	 293.83 MB/s	   40903 B/op	      13 allocs/op
BenchmarkConnTCPCompress-4                    	    2594	    468913 ns/op	 279.52 MB/s	   40958 B/op	      13 allocs/op
BenchmarkConnTCPCompress-8                    	    3438	    368327 ns/op	 355.86 MB/s	   40894 B/op	      13 allocs/op
BenchmarkConnTCPCompress-16                   	    3142	    372094 ns/op	 352.26 MB/s	   40949 B/op	      13 allocs/op
BenchmarkConnTCPCompressLevelDefault          	    2961	    391298 ns/op	 334.97 MB/s	   40890 B/op	      13 allocs/op
BenchmarkConnTCPCompressLevelDefault-2        	    3219	    363205 ns/op	 360.88 MB/s	   40871 B/op	      13 allocs/op
BenchmarkConnTCPCompressLevelDefault-4        	    3363	    366926 ns/op	 357.22 MB/s	   40885 B/op	      13 allocs/op
BenchmarkConnTCPCompressLevelDefault-8        	    2874	    358037 ns/op	 366.08 MB/s	   40939 B/op	      13 allocs/op
BenchmarkConnTCPCompressLevelDefault-16       	    3571	    373549 ns/op	 350.88 MB/s	   40920 B/op	      13 allocs/op
BenchmarkConnTCPCompressLevelBestSpeed        	    6598	    156948 ns/op	 835.13 MB/s	   40798 B/op	      13 allocs/op
BenchmarkConnTCPCompressLevelBestSpeed-2      	    9990	    103465 ns/op	1266.82 MB/s	   40743 B/op	      13 allocs/op
BenchmarkConnTCPCompressLevelBestSpeed-4      	   10189	    115331 ns/op	1136.49 MB/s	   40762 B/op	      13 allocs/op
BenchmarkConnTCPCompressLevelBestSpeed-8      	    9100	    120232 ns/op	1090.16 MB/s	   40796 B/op	      13 allocs/op
BenchmarkConnTCPCompressLevelBestSpeed-16     	   10000	    124585 ns/op	1052.07 MB/s	   40823 B/op	      13 allocs/op
BenchmarkConnTCPNoCompress                    	   40892	     29392 ns/op	4459.49 MB/s	      14 B/op	       6 allocs/op
BenchmarkConnTCPNoCompress-2                  	   41454	     33862 ns/op	3870.81 MB/s	      14 B/op	       6 allocs/op
BenchmarkConnTCPNoCompress-4                  	   34868	     29148 ns/op	4496.77 MB/s	      14 B/op	       6 allocs/op
BenchmarkConnTCPNoCompress-8                  	   42120	     32508 ns/op	4032.00 MB/s	      14 B/op	       6 allocs/op
BenchmarkConnTCPNoCompress-16                 	   33949	     35599 ns/op	3681.88 MB/s	      14 B/op	       6 allocs/op
BenchmarkConnPipeCompress                     	    2709	    394220 ns/op	 332.48 MB/s	   40915 B/op	      13 allocs/op
BenchmarkConnPipeCompress-2                   	    2755	    436481 ns/op	 300.29 MB/s	   40918 B/op	      13 allocs/op
BenchmarkConnPipeCompress-4                   	    2432	    495604 ns/op	 264.47 MB/s	   40995 B/op	      13 allocs/op
BenchmarkConnPipeCompress-8                   	    2388	    515409 ns/op	 254.31 MB/s	   41026 B/op	      13 allocs/op
BenchmarkConnPipeCompress-16                  	    2497	    515602 ns/op	 254.21 MB/s	   41048 B/op	      13 allocs/op
BenchmarkConnPipeCompressLevelDefault         	    2829	    388350 ns/op	 337.51 MB/s	   40903 B/op	      13 allocs/op
BenchmarkConnPipeCompressLevelDefault-2       	    2659	    427471 ns/op	 306.62 MB/s	   40929 B/op	      13 allocs/op
BenchmarkConnPipeCompressLevelDefault-4       	    2377	    501661 ns/op	 261.28 MB/s	   40998 B/op	      13 allocs/op
BenchmarkConnPipeCompressLevelDefault-8       	    2415	    502988 ns/op	 260.59 MB/s	   41024 B/op	      13 allocs/op
BenchmarkConnPipeCompressLevelDefault-16      	    2451	    503161 ns/op	 260.50 MB/s	   41052 B/op	      13 allocs/op
BenchmarkConnPipeCompressLevelBestSpeed       	    6332	    160025 ns/op	 819.07 MB/s	   40809 B/op	      13 allocs/op
BenchmarkConnPipeCompressLevelBestSpeed-2     	    6945	    160985 ns/op	 814.19 MB/s	   40803 B/op	      13 allocs/op
BenchmarkConnPipeCompressLevelBestSpeed-4     	    5392	    187860 ns/op	 697.71 MB/s	   40886 B/op	      13 allocs/op
BenchmarkConnPipeCompressLevelBestSpeed-8     	    6722	    196734 ns/op	 666.24 MB/s	   40866 B/op	      13 allocs/op
BenchmarkConnPipeCompressLevelBestSpeed-16    	    5944	    198636 ns/op	 659.86 MB/s	   40929 B/op	      13 allocs/op
BenchmarkConnPipeNoCompress                   	  209176	      4978 ns/op	26330.02 MB/s	      16 B/op	       6 allocs/op
BenchmarkConnPipeNoCompress-2                 	  166504	      6128 ns/op	21388.26 MB/s	      15 B/op	       6 allocs/op
BenchmarkConnPipeNoCompress-4                 	  175752	      6893 ns/op	19016.30 MB/s	      16 B/op	       6 allocs/op
BenchmarkConnPipeNoCompress-8                 	  183888	      6701 ns/op	19558.66 MB/s	      16 B/op	       6 allocs/op
BenchmarkConnPipeNoCompress-16                	  174505	      6399 ns/op	20484.39 MB/s	      16 B/op	       6 allocs/op
PASS
ok  	github.com/Tnze/go-mc/net	51.743s

third-party library with default compress level (5):

$ go test -bench=. -benchmem -cpu=1,2,4,8 -run=^#
goos: linux
goarch: amd64
pkg: github.com/Tnze/go-mc/net
cpu: Intel(R) Core(TM) i3-10100 CPU @ 3.60GHz
BenchmarkConnTCPCompress                    	    7154	    165390 ns/op	 792.50 MB/s	   38317 B/op	      18 allocs/op
BenchmarkConnTCPCompress-2                  	   13526	     88940 ns/op	1473.71 MB/s	   38250 B/op	      18 allocs/op
BenchmarkConnTCPCompress-4                  	   12187	     97644 ns/op	1342.34 MB/s	   38275 B/op	      18 allocs/op
BenchmarkConnTCPCompress-8                  	   12138	    101844 ns/op	1286.98 MB/s	   38292 B/op	      18 allocs/op
BenchmarkConnTCPCompressLevelDefault        	    7302	    165953 ns/op	 789.82 MB/s	   38314 B/op	      18 allocs/op
BenchmarkConnTCPCompressLevelDefault-2      	   13072	     96233 ns/op	1362.03 MB/s	   38253 B/op	      18 allocs/op
BenchmarkConnTCPCompressLevelDefault-4      	   12362	    101187 ns/op	1295.35 MB/s	   38274 B/op	      18 allocs/op
BenchmarkConnTCPCompressLevelDefault-8      	   10000	    100154 ns/op	1308.71 MB/s	   38309 B/op	      18 allocs/op
BenchmarkConnTCPCompressLevelBestSpeed      	    6423	    170282 ns/op	 769.74 MB/s	   38293 B/op	      18 allocs/op
BenchmarkConnTCPCompressLevelBestSpeed-2    	   12848	     92922 ns/op	1410.56 MB/s	   38233 B/op	      18 allocs/op
BenchmarkConnTCPCompressLevelBestSpeed-4    	   10000	    103129 ns/op	1270.95 MB/s	   38266 B/op	      18 allocs/op
BenchmarkConnTCPCompressLevelBestSpeed-8    	   10000	    103585 ns/op	1265.36 MB/s	   38282 B/op	      18 allocs/op
BenchmarkConnTCPNoCompress                  	   50139	     24010 ns/op	5459.06 MB/s	      14 B/op	       6 allocs/op
BenchmarkConnTCPNoCompress-2                	   53199	     23249 ns/op	5637.85 MB/s	      14 B/op	       6 allocs/op
BenchmarkConnTCPNoCompress-4                	   53641	     22466 ns/op	5834.22 MB/s	      14 B/op	       6 allocs/op
BenchmarkConnTCPNoCompress-8                	   52851	     22399 ns/op	5851.59 MB/s	      14 B/op	       6 allocs/op
BenchmarkConnPipeCompress                   	    7042	    164710 ns/op	 795.77 MB/s	   38320 B/op	      18 allocs/op
BenchmarkConnPipeCompress-2                 	    7897	    142726 ns/op	 918.34 MB/s	   38319 B/op	      18 allocs/op
BenchmarkConnPipeCompress-4                 	    8050	    142635 ns/op	 918.93 MB/s	   38329 B/op	      18 allocs/op
BenchmarkConnPipeCompress-8                 	    7992	    145220 ns/op	 902.58 MB/s	   38349 B/op	      18 allocs/op
BenchmarkConnPipeCompressLevelDefault       	    7137	    164062 ns/op	 798.92 MB/s	   38318 B/op	      18 allocs/op
BenchmarkConnPipeCompressLevelDefault-2     	    7773	    141402 ns/op	 926.94 MB/s	   38320 B/op	      18 allocs/op
BenchmarkConnPipeCompressLevelDefault-4     	    7947	    142868 ns/op	 917.43 MB/s	   38332 B/op	      18 allocs/op
BenchmarkConnPipeCompressLevelDefault-8     	    7916	    146142 ns/op	 896.88 MB/s	   38346 B/op	      18 allocs/op
BenchmarkConnPipeCompressLevelBestSpeed     	    7095	    165301 ns/op	 792.93 MB/s	   38282 B/op	      18 allocs/op
BenchmarkConnPipeCompressLevelBestSpeed-2   	    7699	    144025 ns/op	 910.06 MB/s	   38289 B/op	      18 allocs/op
BenchmarkConnPipeCompressLevelBestSpeed-4   	    7833	    142418 ns/op	 920.33 MB/s	   38296 B/op	      18 allocs/op
BenchmarkConnPipeCompressLevelBestSpeed-8   	    7958	    145914 ns/op	 898.28 MB/s	   38313 B/op	      18 allocs/op
BenchmarkConnPipeNoCompress                 	  160188	      7353 ns/op	17825.17 MB/s	      16 B/op	       6 allocs/op
BenchmarkConnPipeNoCompress-2               	  154214	      7493 ns/op	17492.17 MB/s	      16 B/op	       6 allocs/op
BenchmarkConnPipeNoCompress-4               	  159274	      7474 ns/op	17538.11 MB/s	      16 B/op	       6 allocs/op
BenchmarkConnPipeNoCompress-8               	  153895	      7903 ns/op	16585.19 MB/s	      16 B/op	       6 allocs/op
PASS
ok  	github.com/Tnze/go-mc/net	44.223s
$ go test -bench=. -benchmem -cpu=1,2,4,8,16 -run=^#
goos: linux
goarch: amd64
pkg: github.com/Tnze/go-mc/net
cpu: AMD Ryzen 7 5700G with Radeon Graphics         
BenchmarkConnTCPCompress                      	    8784	    133766 ns/op	 979.86 MB/s	   38288 B/op	      18 allocs/op
BenchmarkConnTCPCompress-2                    	   14545	     83160 ns/op	1576.14 MB/s	   38245 B/op	      18 allocs/op
BenchmarkConnTCPCompress-4                    	   12916	     88339 ns/op	1483.74 MB/s	   38264 B/op	      18 allocs/op
BenchmarkConnTCPCompress-8                    	   12808	     91455 ns/op	1433.19 MB/s	   38282 B/op	      18 allocs/op
BenchmarkConnTCPCompress-16                   	   12381	     94965 ns/op	1380.21 MB/s	   38316 B/op	      18 allocs/op
BenchmarkConnTCPCompressLevelDefault          	    8824	    133798 ns/op	 979.63 MB/s	   38288 B/op	      18 allocs/op
BenchmarkConnTCPCompressLevelDefault-2        	   14373	     80770 ns/op	1622.79 MB/s	   38246 B/op	      18 allocs/op
BenchmarkConnTCPCompressLevelDefault-4        	   13879	     89441 ns/op	1465.46 MB/s	   38261 B/op	      18 allocs/op
BenchmarkConnTCPCompressLevelDefault-8        	   12514	     93520 ns/op	1401.54 MB/s	   38287 B/op	      18 allocs/op
BenchmarkConnTCPCompressLevelDefault-16       	   12661	     95047 ns/op	1379.03 MB/s	   38317 B/op	      18 allocs/op
BenchmarkConnTCPCompressLevelBestSpeed        	    8616	    138932 ns/op	 943.42 MB/s	   38260 B/op	      18 allocs/op
BenchmarkConnTCPCompressLevelBestSpeed-2      	   13503	     81945 ns/op	1599.52 MB/s	   38230 B/op	      18 allocs/op
BenchmarkConnTCPCompressLevelBestSpeed-4      	   12320	     94841 ns/op	1382.02 MB/s	   38250 B/op	      18 allocs/op
BenchmarkConnTCPCompressLevelBestSpeed-8      	   10000	    104422 ns/op	1255.21 MB/s	   38281 B/op	      18 allocs/op
BenchmarkConnTCPCompressLevelBestSpeed-16     	   10000	    106485 ns/op	1230.90 MB/s	   38311 B/op	      18 allocs/op
BenchmarkConnTCPNoCompress                    	   42501	     29066 ns/op	4509.52 MB/s	      14 B/op	       6 allocs/op
BenchmarkConnTCPNoCompress-2                  	   32374	     32985 ns/op	3973.70 MB/s	      14 B/op	       6 allocs/op
BenchmarkConnTCPNoCompress-4                  	   34548	     29363 ns/op	4463.91 MB/s	      14 B/op	       6 allocs/op
BenchmarkConnTCPNoCompress-8                  	   38563	     27726 ns/op	4727.41 MB/s	      14 B/op	       6 allocs/op
BenchmarkConnTCPNoCompress-16                 	   38089	     27333 ns/op	4795.42 MB/s	      14 B/op	       6 allocs/op
BenchmarkConnPipeCompress                     	    8316	    138462 ns/op	 946.63 MB/s	   38298 B/op	      18 allocs/op
BenchmarkConnPipeCompress-2                   	    6968	    150723 ns/op	 869.62 MB/s	   38340 B/op	      18 allocs/op
BenchmarkConnPipeCompress-4                   	    7059	    167589 ns/op	 782.11 MB/s	   38359 B/op	      18 allocs/op
BenchmarkConnPipeCompress-8                   	    6238	    175174 ns/op	 748.24 MB/s	   38400 B/op	      18 allocs/op
BenchmarkConnPipeCompress-16                  	    6588	    176193 ns/op	 743.91 MB/s	   38426 B/op	      18 allocs/op
BenchmarkConnPipeCompressLevelDefault         	    7267	    141580 ns/op	 925.78 MB/s	   38317 B/op	      18 allocs/op
BenchmarkConnPipeCompressLevelDefault-2       	    7839	    144355 ns/op	 907.99 MB/s	   38322 B/op	      18 allocs/op
BenchmarkConnPipeCompressLevelDefault-4       	    6847	    171854 ns/op	 762.69 MB/s	   38365 B/op	      18 allocs/op
BenchmarkConnPipeCompressLevelDefault-8       	    5984	    174048 ns/op	 753.08 MB/s	   38408 B/op	      18 allocs/op
BenchmarkConnPipeCompressLevelDefault-16      	    7562	    172656 ns/op	 759.15 MB/s	   38404 B/op	      18 allocs/op
BenchmarkConnPipeCompressLevelBestSpeed       	    7066	    148212 ns/op	 884.35 MB/s	   38282 B/op	      18 allocs/op
BenchmarkConnPipeCompressLevelBestSpeed-2     	    7628	    148497 ns/op	 882.66 MB/s	   38287 B/op	      18 allocs/op
BenchmarkConnPipeCompressLevelBestSpeed-4     	    7167	    169025 ns/op	 775.46 MB/s	   38321 B/op	      18 allocs/op
BenchmarkConnPipeCompressLevelBestSpeed-8     	    6714	    174141 ns/op	 752.68 MB/s	   38349 B/op	      18 allocs/op
BenchmarkConnPipeCompressLevelBestSpeed-16    	    7598	    177958 ns/op	 736.53 MB/s	   38363 B/op	      18 allocs/op
BenchmarkConnPipeNoCompress                   	  221833	      5095 ns/op	25723.46 MB/s	      16 B/op	       6 allocs/op
BenchmarkConnPipeNoCompress-2                 	  168592	      6419 ns/op	20418.38 MB/s	      16 B/op	       6 allocs/op
BenchmarkConnPipeNoCompress-4                 	  178712	      6580 ns/op	19918.43 MB/s	      16 B/op	       6 allocs/op
BenchmarkConnPipeNoCompress-8                 	  167635	      7047 ns/op	18600.16 MB/s	      16 B/op	       6 allocs/op
BenchmarkConnPipeNoCompress-16                	  160638	      6837 ns/op	19172.32 MB/s	      16 B/op	       6 allocs/op
PASS
ok  	github.com/Tnze/go-mc/net	61.470s

cs8425 avatar Jul 21 '22 01:07 cs8425

And I don't like to switch the zlib implementation, unless you can prove it's necessary and the third-party library is reliable.

(I means may be it has performance improvement but also vulnerabilities)

In theory, we cannot 100% prove/promise that a library will not has vulnerability, even already done code review/fuzz test...etc. And "klauspost/compress" seems using the same test code in standard library (even more test). So I think it should be almost same reliable as standard library if we could trust standard library.

Or we can add option for using third-party library or standard library? for building server, use third-party library for the packet send to client, use standard library read from client. for building client, use third-party library for the packet send to server, use standard library read from sever.

cs8425 avatar Jul 21 '22 03:07 cs8425

I don't believe the first change can improve the performance.

bytes.NewReader doesn't allocate any memory on heap. Using sync.Pool in this case only cause the variables escape from stack(or registers) to heap, which decreases the performance actually.

Actually only save 1 allocate, and seems performance nearly same...

I add this code in net/packet/packet_test.go

func BenchmarkPacketScan(b *testing.B) {
	p := pk.Packet{ID: 0x24, Data: testJoinGameData}
	var (
		EID            pk.Int
		Hardcore       pk.Boolean
		Gamemode       pk.UnsignedByte
		PreGamemode    pk.Byte
		WorldNames     = make([]pk.Identifier, 0) // This cannot replace with "var DimensionNames []pk.Identifier" because "nil" has no type information
		DimensionCodec struct {
			DimensionType interface{} `nbt:"minecraft:dimension_type"`
			WorldgenBiome interface{} `nbt:"minecraft:worldgen/biome"`
		}
		Dimension                 interface{}
		WorldName                 pk.Identifier
		HashedSeed                pk.Long
		MaxPlayers                pk.VarInt
		ViewDistance              pk.VarInt
		RDI, ERS, IsDebug, IsFlat pk.Boolean
	)

	b.SetBytes(int64(len(p.Data)))
	b.ResetTimer()
	b.ReportAllocs()

	for i := 0; i < b.N; i++ {
		err := p.Scan(
			&EID,
			&Hardcore,
			&Gamemode,
			&PreGamemode,
			pk.Array(&WorldNames),
			pk.NBT(&DimensionCodec),
			pk.NBT(&Dimension),
			&WorldName,
			&HashedSeed,
			&MaxPlayers,
			&ViewDistance,
			&RDI, &ERS, &IsDebug, &IsFlat,
		)
		if err != nil {
			b.Fatalf("err = %d; want <nil>", err)
		}
	}
}

benchmark result:

without pool:

$ go test -bench=. -benchmem -cpu=1,2,4,8 -run=^#
goos: linux
goarch: amd64
pkg: github.com/Tnze/go-mc/net/packet
cpu: Intel(R) Core(TM) i3-10100 CPU @ 3.60GHz
BenchmarkPacketScan     	    2385	    489661 ns/op	  63.00 MB/s	  224318 B/op	   12713 allocs/op
BenchmarkPacketScan-2   	    2406	    487766 ns/op	  63.24 MB/s	  224336 B/op	   12713 allocs/op
BenchmarkPacketScan-4   	    2433	    496988 ns/op	  62.07 MB/s	  224325 B/op	   12713 allocs/op
BenchmarkPacketScan-8   	    2402	    493389 ns/op	  62.52 MB/s	  224336 B/op	   12713 allocs/op
PASS
ok  	github.com/Tnze/go-mc/net/packet	4.945s
$ go test -bench=. -benchmem -cpu=1,2,4,8,16 -run=^#
goos: linux
goarch: amd64
pkg: github.com/Tnze/go-mc/net/packet
cpu: AMD Ryzen 7 5700G with Radeon Graphics         
BenchmarkPacketScan       	    2367	    465953 ns/op	  66.20 MB/s	  224331 B/op	   12713 allocs/op
BenchmarkPacketScan-2     	    2185	    486443 ns/op	  63.41 MB/s	  224340 B/op	   12713 allocs/op
BenchmarkPacketScan-4     	    2139	    498789 ns/op	  61.84 MB/s	  224335 B/op	   12713 allocs/op
BenchmarkPacketScan-8     	    2175	    524318 ns/op	  58.83 MB/s	  224333 B/op	   12713 allocs/op
BenchmarkPacketScan-16    	    2128	    529007 ns/op	  58.31 MB/s	  224338 B/op	   12713 allocs/op
PASS
ok  	github.com/Tnze/go-mc/net/packet	5.789s

with pool:

$ go test -bench=. -benchmem -cpu=1,2,4,8 -run=^#
goos: linux
goarch: amd64
pkg: github.com/Tnze/go-mc/net/packet
cpu: Intel(R) Core(TM) i3-10100 CPU @ 3.60GHz
BenchmarkPacketScan     	    2392	    491476 ns/op	  62.76 MB/s	  224283 B/op	   12712 allocs/op
BenchmarkPacketScan-2   	    2421	    485727 ns/op	  63.51 MB/s	  224301 B/op	   12712 allocs/op
BenchmarkPacketScan-4   	    2383	    493056 ns/op	  62.56 MB/s	  224308 B/op	   12712 allocs/op
BenchmarkPacketScan-8   	    2386	    505003 ns/op	  61.08 MB/s	  224354 B/op	   12712 allocs/op
PASS
ok  	github.com/Tnze/go-mc/net/packet	4.942s
$ go test -bench=. -benchmem -cpu=1,2,4,8,16 -run=^#
goos: linux
goarch: amd64
pkg: github.com/Tnze/go-mc/net/packet
cpu: AMD Ryzen 7 5700G with Radeon Graphics         
BenchmarkPacketScan       	    2390	    456629 ns/op	  67.55 MB/s	  224288 B/op	   12712 allocs/op
BenchmarkPacketScan-2     	    2342	    482222 ns/op	  63.97 MB/s	  224307 B/op	   12712 allocs/op
BenchmarkPacketScan-4     	    2216	    522766 ns/op	  59.01 MB/s	  224313 B/op	   12712 allocs/op
BenchmarkPacketScan-8     	    2210	    515063 ns/op	  59.89 MB/s	  224344 B/op	   12712 allocs/op
BenchmarkPacketScan-16    	    2276	    523909 ns/op	  58.88 MB/s	  224412 B/op	   12712 allocs/op
PASS
ok  	github.com/Tnze/go-mc/net/packet	5.986s

cs8425 avatar Jul 21 '22 03:07 cs8425

and seems performance nearly same...

Remove the pool.

Tnze avatar Jul 21 '22 03:07 Tnze

Maybe I should split into 2 PR.... should I squash the commits before merge?

cs8425 avatar Jul 21 '22 04:07 cs8425

Let's do what we talk about in #209 first.

(By simply store the server's key in the LoginHandler. There is no need to use interface if we have only one implementation.

I kept my opinion of switching the zlib lib, and you can open a new issue for that if you like.

Tnze avatar Jul 21 '22 05:07 Tnze