Debian Codegraph @

src/uu/ls/BENCHMARKING.md

raw
1 # Benchmarking ls
2
3 ls majorly involves fetching a lot of details (depending upon what details are requested, eg. time/date, inode details, etc) for each path using system calls. Ideally, any system call should be done only once for each of the paths - not adhering to this principle leads to a lot of system call overhead multiplying and bubbling up, especially for recursive ls, therefore it is important to always benchmark multiple scenarios.
4
5 ls _also_ prints a lot of information, so optimizing formatting operations is also critical:
6 - Try to avoid using `format` unless required.
7 - Try to avoid repeated string copies unless necessary.
8 - If a temporary buffer is required, try to allocate a reasonable capacity to start with to avoid repeated reallocations.
9 - Some values might be expensive to compute (e.g. current line width), but are only required in some cases. Consider delaying computations, e.g. by wrapping the evaluation in a LazyCell.
10
11 This is an overview over what was benchmarked, and if you make changes to `ls`, you are encouraged to check
12 how performance was affected for the workloads listed below. Feel free to add other workloads to the
13 list that we should improve / make sure not to regress.
14
15 Run `cargo build --release` before benchmarking after you make a change!
16
17 ## Simple recursive ls
18
19 - Get a large tree, for example linux kernel source tree.
20 - Benchmark simple recursive ls with hyperfine: `hyperfine --warmup 2 "target/release/coreutils ls -R tree > /dev/null"`.
21
22 ## Recursive ls with all and long options
23
24 - Same tree as above
25 - Benchmark recursive ls with -al -R options with hyperfine: `hyperfine --warmup 2 "target/release/coreutils ls -al -R tree > /dev/null"`.
26
27 ## Comparing with GNU ls
28
29 Hyperfine accepts multiple commands to run and will compare them. To compare performance with GNU ls
30 duplicate the string you passed to hyperfine but remove the `target/release/coreutils` bit from it.
31
32 Example: `hyperfine --warmup 2 "target/release/coreutils ls -al -R tree > /dev/null"` becomes
33 `hyperfine --warmup 2 "target/release/coreutils ls -al -R tree > /dev/null" "ls -al -R tree > /dev/null"`
34 (This assumes GNU ls is installed as `ls`)
35
36 This can also be used to compare with version of ls built before your changes to ensure your change does not regress this.
37
38 Here is a `bash` script for doing this comparison:
39
40 ```shell
41 #!/bin/bash
42 cargo build --no-default-features --features ls --release
43 args="$@"
44 hyperfine "ls $args" "target/release/coreutils ls $args"
45 ```
46
47 **Note**: No localization is currently implemented. This means that the comparison above is not really fair. We can fix this by setting `LC_ALL=C`, so GNU `ls` can ignore localization.
48
49 ## Checking system call count
50
51 - Another thing to look at would be system calls count using strace (on linux) or equivalent on other operating systems.
52 - Example: `strace -c target/release/coreutils ls -al -R tree`
53
54 ## Cargo Flamegraph
55
56 With Cargo Flamegraph you can easily make a flamegraph of `ls`:
57
58 ```shell
59 cargo flamegraph --cmd coreutils -- ls [additional parameters]
60 ```
61
62 However, if the `-R` option is given, the output becomes pretty much useless due to recursion. We can fix this by merging all the direct recursive calls with `uniq`, below is a `bash` script that does this.
63
64 ```shell
65 #!/bin/bash
66 cargo build --release --no-default-features --features ls
67 perf record target/release/coreutils ls "$@"
68 perf script | uniq | inferno-collapse-perf | inferno-flamegraph > flamegraph.svg
69 ```