Debian Codegraph @

src/uu/basenc/BENCHMARKING.md

raw
1 <!--
2 spell-checker:ignore gibibyte toybox SSSE oneline
3 -->
4
5 # Benchmarking base32, base64, and basenc
6
7 Note that the functionality of the `base32` and `base64` programs is identical to that of the `basenc` program, using
8 the "--base32" and "--base64" options, respectively. For that reason, it is only necessary to benchmark `basenc`.
9
10 To compare the runtime performance of the uutils implementation with the GNU Core Utilities implementation, you can
11 use a benchmarking tool like [hyperfine][0].
12
13 hyperfine currently does not measure maximum memory usage. Memory usage can be benchmarked using [poop][2], or
14 [toybox][3]'s "time" subcommand (both are Linux only).
15
16 Build the `basenc` binary using the release profile:
17
18 ```Shell
19 cargo build --package uu_basenc --profile release
20 ```
21
22 ## Base16 output benchmark
23
24 This benchmark measures buffered Base16 output. 16 MiB input for every run. Writes to `/dev/null`
25 and a temporary file. Runs only on Unix.
26
27 ```Shell
28 cargo bench --package uu_basenc --bench basenc_bench
29 ```
30
31 ## Expected performance
32
33 uutils' `basenc` performs streaming decoding and encoding, and therefore should perform all operations with a constant
34 maximum memory usage, regardless of the size of the input. Release builds currently use less than 3 mebibytes of
35 memory, and memory usage greater than 10 mebibytes should be considered a bug.
36
37 As of September 2024, uutils' `basenc` has runtime performance equal to or superior to GNU Core Utilities' `basenc` in
38 in most scenarios. uutils' `basenc` uses slightly more memory, but given how small these quantities are in absolute
39 terms (see above), this is highly unlikely to be practically relevant to users.
40
41 ### SIMD Acceleration
42
43 Our implementation of base64 encoding and decoding operations use SIMD acceleration via the `base64-simd`
44 crate. This provides significant performance improvements for base64 operations:
45
46 - **Base64 encoding**: ~3-4x faster than the previous implementation
47 - **Base64 decoding**: ~4-5x faster than the previous implementation
48 - **Overall performance**: 1.77x faster than GNU coreutils base64 on large files (4GB+)
49
50 The SIMD implementation automatically detects and uses the best available CPU instructions (SSE2, SSSE3, SSE4.1,
51 AVX2, etc.) for maximum performance on the target platform.
52
53 ## Benchmark results (2024-09-27)
54
55 ### Setup
56
57 ```Shell
58 # Use uutils' dd to create a 1 gibibyte in-memory file filled with random bytes (Linux only).
59 # On other platforms, you can use /tmp instead of /dev/shm, but note that /tmp is not guaranteed to be in-memory.
60 coreutils dd if=/dev/urandom of=/dev/shm/one-random-gibibyte bs=1024 count=1048576
61
62 # Encode this file for use in decoding performance testing
63 /usr/bin/basenc --base32hex -- /dev/shm/one-random-gibibyte 1>/dev/shm/one-random-gibibyte-base32hex-encoded
64 /usr/bin/basenc --z85 -- /dev/shm/one-random-gibibyte 1>/dev/shm/one-random-gibibyte-z85-encoded
65 ```
66
67 ### Programs being tested
68
69 uutils' `basenc`:
70
71 ```
72 ❯ git rev-list HEAD | coreutils head -n 1 -- -
73 a0718ef0ffd50539a2e2bc0095c9fadcd70ab857
74 ```
75
76 GNU Core Utilities' `basenc`:
77
78 ```
79 ❯ /usr/bin/basenc --version | coreutils head -n 1 -- -
80 basenc (GNU coreutils) 9.4
81 ```
82
83 ### Encoding performance
84
85 #### "--base64", default line wrapping (76 characters)
86
87 ➕ Faster than GNU Core Utilities
88
89 ```
90 ❯ hyperfine \
91 --sort \
92 command \
93 -- \
94 '/usr/bin/basenc --base64 -- /dev/shm/one-random-gibibyte 1>/dev/null' \
95 './target/release/basenc --base64 -- /dev/shm/one-random-gibibyte 1>/dev/null'
96
97 Benchmark 1: /usr/bin/basenc --base64 -- /dev/shm/one-random-gibibyte 1>/dev/null
98 Time (mean ± σ): 965.1 ms ± 7.9 ms [User: 766.2 ms, System: 193.4 ms]
99 Range (min … max): 950.2 ms … 976.9 ms 10 runs
100
101 Benchmark 2: ./target/release/basenc --base64 -- /dev/shm/one-random-gibibyte 1>/dev/null
102 Time (mean ± σ): 696.6 ms ± 9.1 ms [User: 574.9 ms, System: 117.3 ms]
103 Range (min … max): 683.1 ms … 713.5 ms 10 runs
104
105 Relative speed comparison
106 1.39 ± 0.02 /usr/bin/basenc --base64 -- /dev/shm/one-random-gibibyte 1>/dev/null
107 1.00 ./target/release/basenc --base64 -- /dev/shm/one-random-gibibyte 1>/dev/null
108 ```
109
110 #### "--base16", no line wrapping
111
112 ➖ Slower than GNU Core Utilities
113
114 ```
115 ❯ poop \
116 '/usr/bin/basenc --base16 --wrap 0 -- /dev/shm/one-random-gibibyte' \
117 './target/release/basenc --base16 --wrap 0 -- /dev/shm/one-random-gibibyte'
118
119 Benchmark 1 (6 runs): /usr/bin/basenc --base16 --wrap 0 -- /dev/shm/one-random-gibibyte
120 measurement mean ± σ min … max outliers delta
121 wall_time 836ms ± 13.3ms 822ms … 855ms 0 ( 0%) 0%
122 peak_rss 2.05MB ± 73.0KB 1.94MB … 2.12MB 0 ( 0%) 0%
123 cpu_cycles 2.85G ± 32.8M 2.82G … 2.91G 0 ( 0%) 0%
124 instructions 14.0G ± 58.7 14.0G … 14.0G 0 ( 0%) 0%
125 cache_references 70.0M ± 6.48M 63.7M … 78.8M 0 ( 0%) 0%
126 cache_misses 582K ± 172K 354K … 771K 0 ( 0%) 0%
127 branch_misses 667K ± 4.55K 662K … 674K 0 ( 0%) 0%
128 Benchmark 2 (6 runs): ./target/release/basenc --base16 --wrap 0 -- /dev/shm/one-random-gibibyte
129 measurement mean ± σ min … max outliers delta
130 wall_time 884ms ± 6.38ms 878ms … 895ms 0 ( 0%) 💩+ 5.7% ± 1.6%
131 peak_rss 2.65MB ± 66.8KB 2.55MB … 2.74MB 0 ( 0%) 💩+ 29.3% ± 4.4%
132 cpu_cycles 3.15G ± 8.61M 3.14G … 3.16G 0 ( 0%) 💩+ 10.6% ± 1.1%
133 instructions 10.5G ± 275 10.5G … 10.5G 0 ( 0%) ⚡- 24.9% ± 0.0%
134 cache_references 93.5M ± 6.10M 87.2M … 104M 0 ( 0%) 💩+ 33.7% ± 11.6%
135 cache_misses 415K ± 52.3K 363K … 474K 0 ( 0%) - 28.8% ± 28.0%
136 branch_misses 1.43M ± 4.82K 1.42M … 1.43M 0 ( 0%) 💩+113.9% ± 0.9%
137 ```
138
139 ### Decoding performance
140
141 #### "--base32hex"
142
143 ➕ Faster than GNU Core Utilities
144
145 ```
146 ❯ hyperfine \
147 --sort \
148 command \
149 -- \
150 '/usr/bin/basenc --base32hex --decode -- /dev/shm/one-random-gibibyte-base32hex-encoded 1>/dev/null' \
151 './target/release/basenc --base32hex --decode -- /dev/shm/one-random-gibibyte-base32hex-encoded 1>/dev/null'
152
153 Benchmark 1: /usr/bin/basenc --base32hex --decode -- /dev/shm/one-random-gibibyte-base32hex-encoded 1>/dev/null
154 Time (mean ± σ): 7.154 s ± 0.082 s [User: 6.802 s, System: 0.323 s]
155 Range (min … max): 7.051 s … 7.297 s 10 runs
156
157 Benchmark 2: ./target/release/basenc --base32hex --decode -- /dev/shm/one-random-gibibyte-base32hex-encoded 1>/dev/null
158 Time (mean ± σ): 2.679 s ± 0.025 s [User: 2.446 s, System: 0.221 s]
159 Range (min … max): 2.649 s … 2.718 s 10 runs
160
161 Relative speed comparison
162 2.67 ± 0.04 /usr/bin/basenc --base32hex --decode -- /dev/shm/one-random-gibibyte-base32hex-encoded 1>/dev/null
163 1.00 ./target/release/basenc --base32hex --decode -- /dev/shm/one-random-gibibyte-base32hex-encoded 1>/dev/null
164 ```
165
166 #### "--z85", with "--ignore-garbage"
167
168 ➕ Faster than GNU Core Utilities
169
170 ```
171 ❯ poop \
172 '/usr/bin/basenc --decode --ignore-garbage --z85 -- /dev/shm/one-random-gibibyte-z85-encoded' \
173 './target/release/basenc --decode --ignore-garbage --z85 -- /dev/shm/one-random-gibibyte-z85-encoded'
174
175 Benchmark 1 (3 runs): /usr/bin/basenc --decode --ignore-garbage --z85 -- /dev/shm/one-random-gibibyte-z85-encoded
176 measurement mean ± σ min … max outliers delta
177 wall_time 14.4s ± 68.4ms 14.3s … 14.4s 0 ( 0%) 0%
178 peak_rss 1.98MB ± 10.8KB 1.97MB … 1.99MB 0 ( 0%) 0%
179 cpu_cycles 58.4G ± 211M 58.3G … 58.7G 0 ( 0%) 0%
180 instructions 74.7G ± 64.0 74.7G … 74.7G 0 ( 0%) 0%
181 cache_references 41.8M ± 624K 41.2M … 42.4M 0 ( 0%) 0%
182 cache_misses 693K ± 118K 567K … 802K 0 ( 0%) 0%
183 branch_misses 1.24G ± 183K 1.24G … 1.24G 0 ( 0%) 0%
184 Benchmark 2 (3 runs): ./target/release/basenc --decode --ignore-garbage --z85 -- /dev/shm/one-random-gibibyte-z85-encoded
185 measurement mean ± σ min … max outliers delta
186 wall_time 2.80s ± 17.9ms 2.79s … 2.82s 0 ( 0%) ⚡- 80.5% ± 0.8%
187 peak_rss 2.61MB ± 67.4KB 2.57MB … 2.69MB 0 ( 0%) 💩+ 31.9% ± 5.5%
188 cpu_cycles 10.8G ± 27.9M 10.8G … 10.9G 0 ( 0%) ⚡- 81.5% ± 0.6%
189 instructions 39.0G ± 353 39.0G … 39.0G 0 ( 0%) ⚡- 47.7% ± 0.0%
190 cache_references 114M ± 2.43M 112M … 116M 0 ( 0%) 💩+173.3% ± 9.6%
191 cache_misses 1.06M ± 288K 805K … 1.37M 0 ( 0%) + 52.6% ± 72.0%
192 branch_misses 1.18M ± 14.7K 1.16M … 1.19M 0 ( 0%) ⚡- 99.9% ± 0.0%
193 ```
194
195 ## SIMD Benchmark Results (2025-09-08)
196
197 ### Base64 encoding performance with SIMD acceleration
198
199 The following benchmark demonstrates the significant performance improvement from SIMD acceleration for base64
200 encoding on large files:
201
202 ```Shell
203 ❯ hyperfine '/usr/bin/base64 /tmp/oneline_4G.txt' './target/release/coreutils base64 /tmp/oneline_4G.txt' -N --warmup 3
204
205 Benchmark 1: /usr/bin/base64 /tmp/oneline_4G.txt
206 Time (mean ± σ): 5.326 s ± 0.193 s [User: 4.278 s, System: 1.047 s]
207 Range (min … max): 5.049 s … 5.682 s 10 runs
208
209 Benchmark 2: ./target/release/coreutils base64 /tmp/oneline_4G.txt
210 Time (mean ± σ): 3.006 s ± 0.129 s [User: 1.342 s, System: 1.662 s]
211 Range (min … max): 2.872 s … 3.289 s 10 runs
212
213 Summary
214 ./target/release/coreutils base64 /tmp/oneline_4G.txt ran
215 1.77 ± 0.10 times faster than /usr/bin/base64 /tmp/oneline_4G.txt
216 ```
217
218 **Key improvements:**
219 - **1.77x faster** than GNU coreutils `base64`
220 - **3.2x reduction** in user CPU time (4.278s → 1.342s)
221 - **Overall 77% performance improvement** on large file encoding
222
223 The dramatic reduction in user CPU time demonstrates the effectiveness of SIMD acceleration for the computational
224 aspects of base64 encoding, while system time remains similar due to I/O overhead.
225
226 [0]: https://github.com/sharkdp/hyperfine
227 [1]: https://github.com/sharkdp/hyperfine?tab=readme-ov-file#installation
228 [2]: https://github.com/andrewrk/poop
229 [3]: https://landley.net/toybox/