Skip to content

improve ByteSize function#132

Merged
ReneWerner87 merged 5 commits intomasterfrom
improve_bytesize
Jul 11, 2025
Merged

improve ByteSize function#132
ReneWerner87 merged 5 commits intomasterfrom
improve_bytesize

Conversation

@ReneWerner87
Copy link
Member

@ReneWerner87 ReneWerner87 commented Jul 11, 2025

old:

Benchmark_ByteSize/0-12  	394620337	         2.983 ns/op	       0 B/op	       0 allocs/op
Benchmark_ByteSize/0-12  	402809990	         2.958 ns/op	       0 B/op	       0 allocs/op
Benchmark_ByteSize/0-12  	413246502	         2.921 ns/op	       0 B/op	       0 allocs/op
Benchmark_ByteSize/0-12  	403580769	         2.928 ns/op	       0 B/op	       0 allocs/op
Benchmark_ByteSize/1-12  	12665492	        99.71 ns/op	      26 B/op	       2 allocs/op
Benchmark_ByteSize/1-12  	12164020	        98.69 ns/op	      26 B/op	       2 allocs/op
Benchmark_ByteSize/1-12  	12065070	       101.4 ns/op	      26 B/op	       2 allocs/op
Benchmark_ByteSize/1-12  	12394597	        98.16 ns/op	      26 B/op	       2 allocs/op
Benchmark_ByteSize/500-12         	12344122	        97.39 ns/op	      28 B/op	       2 allocs/op
Benchmark_ByteSize/500-12         	12245131	        97.55 ns/op	      28 B/op	       2 allocs/op
Benchmark_ByteSize/500-12         	12014792	        97.59 ns/op	      28 B/op	       2 allocs/op
Benchmark_ByteSize/500-12         	12355567	        97.73 ns/op	      28 B/op	       2 allocs/op
Benchmark_ByteSize/1024-12        	12244044	        98.73 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1024-12        	12220083	       101.0 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1024-12        	11133416	       102.3 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1024-12        	12035841	       101.2 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1126-12        	10641859	       111.8 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1126-12        	10875364	       111.6 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1126-12        	10740609	       120.4 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1126-12        	10505193	       113.0 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1048576-12     	11958792	       100.0 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1048576-12     	11678012	       102.1 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1048576-12     	11683394	       101.4 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1048576-12     	11338077	        98.68 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1153024-12     	10879534	       112.3 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1153024-12     	10741414	       112.3 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1153024-12     	10648194	       111.2 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1153024-12     	10915987	       111.4 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1073741824-12  	12326180	        99.14 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1073741824-12  	11839748	       102.8 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1073741824-12  	11915380	       101.2 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1073741824-12  	11987697	        97.93 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1180696576-12  	10691478	       111.6 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1180696576-12  	10902478	       111.4 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1180696576-12  	10897396	       111.6 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1180696576-12  	10656692	       111.8 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1099511627776-12         	12218870	       100.1 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1099511627776-12         	11694182	       101.6 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1099511627776-12         	11744306	       100.5 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1099511627776-12         	12106914	       106.1 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1209033293824-12         	10924442	       111.5 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1209033293824-12         	10612323	       112.8 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1209033293824-12         	10885879	       113.1 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1209033293824-12         	10919641	       120.9 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1125899906842624-12      	12151698	       102.2 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1125899906842624-12      	11863318	       100.6 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1125899906842624-12      	11944686	        99.75 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1125899906842624-12      	11987617	        97.80 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1238050092875776-12      	10737126	       111.2 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1238050092875776-12      	11016943	       111.6 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1238050092875776-12      	10792494	       115.3 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1238050092875776-12      	10666341	       113.5 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1152921504606846976-12   	11189354	       102.0 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1152921504606846976-12   	11548677	       101.7 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1152921504606846976-12   	12055868	       106.0 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1152921504606846976-12   	11992898	        98.86 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1267763295104794624-12   	10693737	       110.8 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1267763295104794624-12   	10881495	       111.0 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1267763295104794624-12   	10967534	       116.0 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1267763295104794624-12   	10403409	       115.0 ns/op	      29 B/op	       2 allocs/op

new:

Benchmark_ByteSize/0-12  	381202279	         2.953 ns/op	       0 B/op	       0 allocs/op
Benchmark_ByteSize/0-12  	412914474	         2.923 ns/op	       0 B/op	       0 allocs/op
Benchmark_ByteSize/0-12  	392389334	         2.992 ns/op	       0 B/op	       0 allocs/op
Benchmark_ByteSize/0-12  	411269878	         2.926 ns/op	       0 B/op	       0 allocs/op
Benchmark_ByteSize/1-12  	66645376	        18.02 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1-12  	67031926	        18.48 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1-12  	65031100	        18.79 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1-12  	67271643	        18.37 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/500-12         	55519891	        21.61 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/500-12         	56071926	        21.70 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/500-12         	52847816	        21.71 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/500-12         	56364048	        21.67 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1024-12        	60792967	        19.33 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1024-12        	61283637	        19.65 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1024-12        	59079831	        20.01 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1024-12        	58885951	        19.84 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1126-12        	52129024	        22.86 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1126-12        	50120601	        22.93 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1126-12        	51379209	        22.75 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1126-12        	51349436	        22.64 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1048576-12     	62054118	        19.15 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1048576-12     	61777123	        19.36 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1048576-12     	63581073	        19.94 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1048576-12     	63366613	        19.45 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1153024-12     	52423779	        22.41 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1153024-12     	52352594	        22.35 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1153024-12     	48861012	        22.79 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1153024-12     	41920482	        24.33 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1073741824
Benchmark_ByteSize/1073741824-12  	61493130	        19.40 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1073741824-12  	59481151	        19.84 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1073741824-12  	61494837	        19.85 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1073741824-12  	60446295	        19.83 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1180696576-12  	52824068	        22.09 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1180696576-12  	52169942	        22.17 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1180696576-12  	55176746	        22.52 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1180696576-12  	54001458	        22.39 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1099511627776-12         	61136632	        18.58 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1099511627776-12         	61396255	        18.96 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1099511627776-12         	63482826	        19.51 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1099511627776-12         	62977662	        19.30 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1209033293824-12         	53747990	        22.98 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1209033293824-12         	52719302	        22.03 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1209033293824-12         	53753505	        21.85 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1209033293824-12         	54607508	        22.76 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1125899906842624-12      	65190524	        18.50 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1125899906842624-12      	64880208	        19.68 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1125899906842624-12      	64009957	        20.05 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1125899906842624-12      	66231104	        18.61 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1238050092875776-12      	55698992	        21.87 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1238050092875776-12      	54699502	        22.81 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1238050092875776-12      	55585365	        21.98 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1238050092875776-12      	55902247	        21.81 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1152921504606846976-12   	67190190	        18.43 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1152921504606846976-12   	66749640	        19.93 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1152921504606846976-12   	65533930	        18.57 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1152921504606846976-12   	62504475	        18.38 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1267763295104794624-12   	54216764	        21.37 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1267763295104794624-12   	55445602	        21.37 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1267763295104794624-12   	52494489	        21.50 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1267763295104794624-12   	56084814	        21.37 ns/op	      16 B/op	       1 allocs/op

Summary by CodeRabbit

  • Refactor

    • Improved byte size formatting using integer arithmetic for more accurate and efficient output, with safeguards for very large values.
  • Tests

    • Added benchmark tests to measure performance and memory usage of byte size formatting.
    • Introduced tests validating behavior near maximum supported input values.

old:
```
Benchmark_ByteSize/0-12  	394620337	         2.983 ns/op	       0 B/op	       0 allocs/op
Benchmark_ByteSize/0-12  	402809990	         2.958 ns/op	       0 B/op	       0 allocs/op
Benchmark_ByteSize/0-12  	413246502	         2.921 ns/op	       0 B/op	       0 allocs/op
Benchmark_ByteSize/0-12  	403580769	         2.928 ns/op	       0 B/op	       0 allocs/op
Benchmark_ByteSize/1-12  	12665492	        99.71 ns/op	      26 B/op	       2 allocs/op
Benchmark_ByteSize/1-12  	12164020	        98.69 ns/op	      26 B/op	       2 allocs/op
Benchmark_ByteSize/1-12  	12065070	       101.4 ns/op	      26 B/op	       2 allocs/op
Benchmark_ByteSize/1-12  	12394597	        98.16 ns/op	      26 B/op	       2 allocs/op
Benchmark_ByteSize/500-12         	12344122	        97.39 ns/op	      28 B/op	       2 allocs/op
Benchmark_ByteSize/500-12         	12245131	        97.55 ns/op	      28 B/op	       2 allocs/op
Benchmark_ByteSize/500-12         	12014792	        97.59 ns/op	      28 B/op	       2 allocs/op
Benchmark_ByteSize/500-12         	12355567	        97.73 ns/op	      28 B/op	       2 allocs/op
Benchmark_ByteSize/1024-12        	12244044	        98.73 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1024-12        	12220083	       101.0 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1024-12        	11133416	       102.3 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1024-12        	12035841	       101.2 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1126-12        	10641859	       111.8 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1126-12        	10875364	       111.6 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1126-12        	10740609	       120.4 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1126-12        	10505193	       113.0 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1048576-12     	11958792	       100.0 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1048576-12     	11678012	       102.1 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1048576-12     	11683394	       101.4 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1048576-12     	11338077	        98.68 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1153024-12     	10879534	       112.3 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1153024-12     	10741414	       112.3 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1153024-12     	10648194	       111.2 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1153024-12     	10915987	       111.4 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1073741824-12  	12326180	        99.14 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1073741824-12  	11839748	       102.8 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1073741824-12  	11915380	       101.2 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1073741824-12  	11987697	        97.93 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1180696576-12  	10691478	       111.6 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1180696576-12  	10902478	       111.4 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1180696576-12  	10897396	       111.6 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1180696576-12  	10656692	       111.8 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1099511627776-12         	12218870	       100.1 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1099511627776-12         	11694182	       101.6 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1099511627776-12         	11744306	       100.5 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1099511627776-12         	12106914	       106.1 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1209033293824-12         	10924442	       111.5 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1209033293824-12         	10612323	       112.8 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1209033293824-12         	10885879	       113.1 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1209033293824-12         	10919641	       120.9 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1125899906842624-12      	12151698	       102.2 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1125899906842624-12      	11863318	       100.6 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1125899906842624-12      	11944686	        99.75 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1125899906842624-12      	11987617	        97.80 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1238050092875776-12      	10737126	       111.2 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1238050092875776-12      	11016943	       111.6 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1238050092875776-12      	10792494	       115.3 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1238050092875776-12      	10666341	       113.5 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1152921504606846976-12   	11189354	       102.0 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1152921504606846976-12   	11548677	       101.7 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1152921504606846976-12   	12055868	       106.0 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1152921504606846976-12   	11992898	        98.86 ns/op	      27 B/op	       2 allocs/op
Benchmark_ByteSize/1267763295104794624-12   	10693737	       110.8 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1267763295104794624-12   	10881495	       111.0 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1267763295104794624-12   	10967534	       116.0 ns/op	      29 B/op	       2 allocs/op
Benchmark_ByteSize/1267763295104794624-12   	10403409	       115.0 ns/op	      29 B/op	       2 allocs/op
```
new:
```
Benchmark_ByteSize/0-12  	381202279	         2.953 ns/op	       0 B/op	       0 allocs/op
Benchmark_ByteSize/0-12  	412914474	         2.923 ns/op	       0 B/op	       0 allocs/op
Benchmark_ByteSize/0-12  	392389334	         2.992 ns/op	       0 B/op	       0 allocs/op
Benchmark_ByteSize/0-12  	411269878	         2.926 ns/op	       0 B/op	       0 allocs/op
Benchmark_ByteSize/1-12  	66645376	        18.02 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1-12  	67031926	        18.48 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1-12  	65031100	        18.79 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1-12  	67271643	        18.37 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/500-12         	55519891	        21.61 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/500-12         	56071926	        21.70 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/500-12         	52847816	        21.71 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/500-12         	56364048	        21.67 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1024-12        	60792967	        19.33 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1024-12        	61283637	        19.65 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1024-12        	59079831	        20.01 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1024-12        	58885951	        19.84 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1126-12        	52129024	        22.86 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1126-12        	50120601	        22.93 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1126-12        	51379209	        22.75 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1126-12        	51349436	        22.64 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1048576-12     	62054118	        19.15 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1048576-12     	61777123	        19.36 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1048576-12     	63581073	        19.94 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1048576-12     	63366613	        19.45 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1153024-12     	52423779	        22.41 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1153024-12     	52352594	        22.35 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1153024-12     	48861012	        22.79 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1153024-12     	41920482	        24.33 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1073741824
Benchmark_ByteSize/1073741824-12  	61493130	        19.40 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1073741824-12  	59481151	        19.84 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1073741824-12  	61494837	        19.85 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1073741824-12  	60446295	        19.83 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1180696576-12  	52824068	        22.09 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1180696576-12  	52169942	        22.17 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1180696576-12  	55176746	        22.52 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1180696576-12  	54001458	        22.39 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1099511627776-12         	61136632	        18.58 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1099511627776-12         	61396255	        18.96 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1099511627776-12         	63482826	        19.51 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1099511627776-12         	62977662	        19.30 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1209033293824-12         	53747990	        22.98 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1209033293824-12         	52719302	        22.03 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1209033293824-12         	53753505	        21.85 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1209033293824-12         	54607508	        22.76 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1125899906842624-12      	65190524	        18.50 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1125899906842624-12      	64880208	        19.68 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1125899906842624-12      	64009957	        20.05 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1125899906842624-12      	66231104	        18.61 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1238050092875776-12      	55698992	        21.87 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1238050092875776-12      	54699502	        22.81 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1238050092875776-12      	55585365	        21.98 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1238050092875776-12      	55902247	        21.81 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1152921504606846976-12   	67190190	        18.43 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1152921504606846976-12   	66749640	        19.93 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1152921504606846976-12   	65533930	        18.57 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1152921504606846976-12   	62504475	        18.38 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1267763295104794624-12   	54216764	        21.37 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1267763295104794624-12   	55445602	        21.37 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1267763295104794624-12   	52494489	        21.50 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteSize/1267763295104794624-12   	56084814	        21.37 ns/op	      16 B/op	       1 allocs/op
```
@ReneWerner87 ReneWerner87 requested a review from a team as a code owner July 11, 2025 11:59
@ReneWerner87 ReneWerner87 requested review from efectn, gaby and sixcolors and removed request for a team July 11, 2025 11:59
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 11, 2025

Walkthrough

The ByteSize function in convert.go was refactored to use integer arithmetic and manual string construction instead of floating-point operations for formatting byte counts. The function now handles large values safely by capping input to a defined maximum. In convert_test.go, new subtests for boundary conditions and a benchmark function Benchmark_ByteSize were added to test performance and correctness.

Changes

File(s) Change Summary
convert.go Refactored ByteSize to use integer math and manual string assembly, added maxSafe constant to cap input, removed float usage.
convert_test.go Added subtests for max safe and overflow byte values; introduced Benchmark_ByteSize to measure performance and memory allocations.

Suggested labels

codex

Poem

In the warren of bytes, a change took flight,
Floats hopped away, ints hold the light.
Benchmarks now scamper, swift and spry,
Testing each nibble as data hops by.
With paws on the code, the rabbit delights—
Speedier numbers, and tidier bytes! 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6efd2b8 and 23a1110.

📒 Files selected for processing (1)
  • convert.go (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • convert.go
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: Build (1.24.x, macos-13)
  • GitHub Check: Build (1.23.x, windows-latest)
  • GitHub Check: Build (1.24.x, macos-latest)
  • GitHub Check: Build (1.24.x, windows-latest)
  • GitHub Check: Analyse
  • GitHub Check: Build (1.23.x, macos-latest)
  • GitHub Check: Build (1.23.x, macos-13)
  • GitHub Check: Build (1.24.x, ubuntu-latest)
  • GitHub Check: Build (1.23.x, ubuntu-latest)
  • GitHub Check: Compare
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch improve_bytesize

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @ReneWerner87, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

I've reviewed this pull request, and it primarily focuses on a substantial performance improvement for the ByteSize utility function. My analysis shows that by switching to integer-based calculations and optimizing string construction, the function now executes much faster and consumes significantly less memory, as evidenced by the provided benchmark results. This is a great improvement for any code path that frequently calls this conversion function.

Highlights

  • Performance Optimization: I've identified a significant performance optimization for the ByteSize function, resulting in a substantial reduction in execution time and memory allocations, as demonstrated by the provided benchmark results.
  • Arithmetic Refactoring: I noted that the internal calculation for ByteSize was refactored from using floating-point arithmetic to more efficient integer arithmetic, which I believe is a key contributor to the observed performance gains.
  • Efficient String Formatting: I observed that string construction within ByteSize now leverages a pre-allocated byte buffer ([]byte) and strconv.AppendUint along with an UnsafeString utility function to minimize string allocations and improve overall speed.
  • New Benchmark Coverage: I see that a new comprehensive benchmark (Benchmark_ByteSize) has been added to convert_test.go to thoroughly measure and validate the performance of the ByteSize function across a wide range of input sizes, from bytes to exabytes.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@codecov
Copy link

codecov bot commented Jul 11, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.20%. Comparing base (c5db933) to head (23a1110).
Report is 15 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #132      +/-   ##
==========================================
+ Coverage   96.07%   96.20%   +0.12%     
==========================================
  Files           9        9              
  Lines         535      553      +18     
==========================================
+ Hits          514      532      +18     
  Misses         14       14              
  Partials        7        7              
Flag Coverage Δ
unittests 96.20% <100.00%> (+0.12%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c5db933 and b15481a.

📒 Files selected for processing (2)
  • convert.go (1 hunks)
  • convert_test.go (2 hunks)
🧰 Additional context used
🧠 Learnings (1)
convert_test.go (8)
Learnt from: sixcolors
PR: gofiber/fiber#3016
File: middleware/session/store.go:164-167
Timestamp: 2024-10-02T23:03:31.727Z
Learning: Unit tests in this project use testify require.
Learnt from: sixcolors
PR: gofiber/fiber#3016
File: middleware/session/store.go:164-167
Timestamp: 2024-10-08T19:06:06.583Z
Learning: Unit tests in this project use testify require.
Learnt from: sixcolors
PR: gofiber/fiber#2922
File: middleware/cors/utils.go:63-71
Timestamp: 2024-07-26T21:00:12.902Z
Learning: The project uses the testify/assert package for assertions in unit tests.
Learnt from: sixcolors
PR: gofiber/fiber#2922
File: middleware/cors/utils.go:63-71
Timestamp: 2024-10-08T19:06:06.583Z
Learning: The project uses the testify/assert package for assertions in unit tests.
Learnt from: efectn
PR: gofiber/fiber#3162
File: hooks_test.go:228-228
Timestamp: 2024-12-13T08:14:22.851Z
Learning: In Go test files, prefer using the `require` methods from the `testify` package for assertions instead of manual comparisons and calls to `t.Fatal` or `t.Fatalf`.
Learnt from: sixcolors
PR: gofiber/fiber#3016
File: middleware/session/middleware_test.go:190-191
Timestamp: 2024-10-12T10:01:44.206Z
Learning: When testing session `IdleTimeout` expiration, it's acceptable to use `time.Sleep` to simulate the passage of time in tests.
Learnt from: sixcolors
PR: gofiber/fiber#3016
File: middleware/session/middleware_test.go:190-191
Timestamp: 2024-09-25T17:05:06.991Z
Learning: When testing session `IdleTimeout` expiration, it's acceptable to use `time.Sleep` to simulate the passage of time in tests.
Learnt from: gaby
PR: gofiber/fiber#3170
File: ctx_test.go:1721-1724
Timestamp: 2024-10-16T12:12:30.506Z
Learning: In the Go unit tests in `ctx_test.go`, it is acceptable to use invalid CIDR notation such as `"0.0.0.1/31junk"` for testing purposes.
🧬 Code Graph Analysis (1)
convert_test.go (1)
convert.go (1)
  • ByteSize (56-102)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)
  • GitHub Check: Build (1.24.x, macos-13)
  • GitHub Check: Build (1.24.x, windows-latest)
  • GitHub Check: lint
  • GitHub Check: Build (1.23.x, ubuntu-latest)
  • GitHub Check: Build (1.23.x, windows-latest)
  • GitHub Check: Build (1.23.x, macos-latest)
  • GitHub Check: Build (1.23.x, macos-13)
  • GitHub Check: Build (1.24.x, ubuntu-latest)
  • GitHub Check: Build (1.24.x, macos-latest)
  • GitHub Check: Analyse
  • GitHub Check: Compare
🔇 Additional comments (4)
convert.go (2)

58-58: LGTM: Integer arithmetic optimization

The type change from float64 to uint64 for the division variable is a key part of the performance optimization, eliminating floating-point operations.


62-77: LGTM: Simplified unit assignment

The switch cases are correctly simplified to directly assign the unit constants without float64 casting, maintaining the same logic while improving performance.

convert_test.go (2)

9-9: LGTM: Required import addition

The strconv import is necessary for the strconv.FormatUint call in the benchmark function names.


300-328: LGTM: Comprehensive benchmark implementation

The benchmark function is well-implemented with:

  • Comprehensive test cases covering the full range from bytes to exabytes
  • Both exact powers of 1024 and fractional values (1126 = 1.1 * 1024)
  • Proper benchmark patterns using ResetTimer() and ReportAllocs()
  • Consistent with the project's testing conventions using testify

The test cases align perfectly with the PR's benchmark data, enabling accurate performance measurement of the ByteSize optimization.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request significantly improves the performance of the ByteSize function by replacing floating-point arithmetic with more efficient integer-based calculations, which also reduces memory allocations. This is a fantastic optimization.

My review includes a critical fix for a potential uint64 overflow in the new logic that could affect very large values, and a medium-severity suggestion to improve the readability of the new benchmarks. Overall, this is a valuable improvement that will be even more robust with the suggested changes.

@ReneWerner87 ReneWerner87 merged commit 30d51f5 into master Jul 11, 2025
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant