| 1 |
# Benchmarking yes |
| 2 |
|
| 3 |
`yes` is a utility printing the provided string and a newline continuously. |
| 4 |
The default value `y` is used to skip the common `y/n` prompts provided by scripts. |
| 5 |
|
| 6 |
## Understanding yes |
| 7 |
|
| 8 |
The most simple way to implement `yes` is the `println!` loop. But it is slow since it |
| 9 |
calls many write syscalls. So `yes` should print an extended string to few bytes in the loop for |
| 10 |
better throughput. |
| 11 |
|
| 12 |
It is difficult to use `hyperfine` for benchmarking `yes` as it has infinite loop. |
| 13 |
But you can see throughput of `yes` by `pv` instead: |
| 14 |
|
| 15 |
```shell |
| 16 |
yes | pv >/dev/null |
| 17 |
``` |
| 18 |
|
| 19 |
### `tee()` zero-copy |
| 20 |
|
| 21 |
The `read()` and `write()` based `yes` implementation is much slower than RAM's bandwidth |
| 22 |
since those syscalls are copying content of RAM. |
| 23 |
|
| 24 |
On Linux, `tee()` and `splice()` for non-pipe output is used to avoid copying content of RAM. |