Reworking the prime path cache
I just reworked how the caching of prime paths works in zcov. Computing the prime paths can be quite expensive in both time and memory, so GCC limits to 250 000 paths by default. This limit was chosen because at around 250k the later passes slow down significantly (verifying the newly emitted instructions seem to be the worst offender, but I don’t have hard data here), but there are functions out there where just finding the prime paths is infeasible on my workstation.
The prime paths are given by the structure of the function (specifically, the control flow graph), which makes it a prime candidate for caching. This is especially useful for zcov as it is an interactive application where every microsecond matters. Even more so since code tends to change very little between coverage runs (and most of those changes are in the test suite and not the code being tested), and so the same functions will be analyzed again and again. Since the same project will be tested over a long time with reasonably few and slow changes we have an perfect use case for a persistent cache.
The caching feature was implemented a long time ago, and was quite quickly put together. It was for all intents and purposes the just-computed suffix tree dumped to disk. For non-trivial functions this was an obvious improvement, but still had a large space- and time overhead it meant storing all subpaths, too.
I tried a few different things and put together a new bespoke representation for a set of prime paths optimized to reduce memory usage, which I then benchmarked on 485 functions from GNU Coreutils. In the benchmark I measured the time (in microseconds) it too to:
- Compute the prime paths
- Look up the prime paths in the old cache
- Look up the prime paths in the new cache
Here are some aggregate statistics from a pandas dataframe:
| mean | median | min | max | |
|---|---|---|---|---|
| path count | 181698 | 15 | 1 | 84197299 |
| compute µs | 4636.26 | 6.00 | 1.00 | 1572546.00 |
| new-cache-hit µs | 44.79 | 7.00 | 4.00 | 13678.00 |
| old-cache-hit µs | 1716.59 | 10.00 | 5.00 | 525424.00 |
| compute µs/path | 0.53 | 0.45 | 0.01 | 1.73 |
| new-cache µs/path | 0.60 | 0.44 | 0.00 | 3.50 |
| old-cache µs/path | 0.86 | 0.64 | 0.00 | 3.00 |
What the median and min/max path count tells us is that most functions are small and simple, but once they go beyond that the path count explodes. That we apparently have higher throughput (lower elapsed per-path time) suggest that for very simple functions the cost of looking up the cache is higher than just computing the number of paths. This is confirmed by filtering out any function with less than 30 paths:
| mean | median | min | max | |
|---|---|---|---|---|
| path count | 530842 | 124 | 31 | 84197299 |
| compute µs | 13536.43 | 101.00 | 3.00 | 1572546.00 |
| new-cache-hit µs | 118.52 | 9.00 | 4.00 | 13678.00 |
| old-cache-hit µs | 4998.68 | 39.00 | 10.00 | 525424.00 |
| compute µs/path | 0.77 | 0.74 | 0.01 | 1.73 |
| new-cache µs/path | 0.08 | 0.06 | 0.00 | 0.35 |
| old-cache µs/path | 0.33 | 0.33 | 0.00 | 0.58 |
| new-cache/compute | 0.18 | 0.06 | 0.00 | 2.67 |
| compute/new-cache | 39.58 | 17.19 | 0.38 | 254.53 |
| old-cache/compute | 0.63 | 0.41 | 0.12 | 10.25 |
| new-cache/old-cache | 0.22 | 0.16 | 0.01 | 0.73 |
| old-cache/new-cache | 15.91 | 6.14 | 1.36 | 115.83 |
So on average the new cache takes 0.08µs/path while computing takes 0.77µs/path, a 10x on average as opposed to the approx 2x of the old cache. This is in practice skewed by the cost of the cache being high for very small functions (sub-50 paths tend to take less than 5 µs to compute). If we look at the fastest/slowest functions while still filtering out those with less than 30 paths:
| function | path count | compute µs | new-cache-hit µs | old-cache-hit µs | compute µs/path | new-cache µs/path |
|---|---|---|---|---|---|---|
| three_arguments | 31 | 13 | 7 | 15 | 0.42 | 0.23 |
| main | 31 | 5 | 11 | 15 | 0.16 | 0.35 |
| value_to_baud | 32 | 32 | 8 | 14 | 1.00 | 0.25 |
| apply_mode | 32 | 13 | 8 | 14 | 0.41 | 0.25 |
| write_to_file | 32 | 21 | 8 | 13 | 0.66 | 0.25 |
| … | … | … | … | … | … | … |
| string_to_baud | 61 | 46 | 8 | 21 | 0.75 | 0.13 |
| sane_mode | 62 | 58 | 8 | 17 | 0.94 | 0.13 |
| set_suffix_length | 64 | 97 | 8 | 32 | 1.52 | 0.12 |
| parse_symbols | 64 | 55 | 7 | 20 | 0.86 | 0.11 |
| set_file_security_ctx | 65 | 45 | 7 | 23 | 0.69 | 0.11 |
| … | … | … | … | … | … | … |
| main | 63790 | 24282 | 144 | 15101 | 0.38 | 0.00 |
| lines_rr | 74899 | 22539 | 100 | 4256 | 0.30 | 0.00 |
| ng_pollard_rho2 | 1416562 | 101036 | 627 | 39961 | 0.07 | 0.00 |
| ine_bytes_split | 1425449 | 147657 | 633 | 27143 | 0.10 | 0.00 |
| parents_private | 84197299 | 1572546 | 13678 | 525424 | 0.02 | 0.00 |
From this table we can see that even for very complex functions the cache hits are fast; we fetch 84 million paths in about 13.5ms which is decently quick and should be fine. For comparison, computing the same function takes about 1.6s. And finally, the relative improvement:
| function | path count | compute/new-cache | old-cache/new-cache |
|---|---|---|---|
| three_arguments | 31 | 1.86 | 2.14 |
| main | 31 | 0.45 | 1.36 |
| value_to_baud | 32 | 4.00 | 1.75 |
| apply_mode | 32 | 1.62 | 1.75 |
| write_to_file | 32 | 2.62 | 1.62 |
| … | … | … | … |
| string_to_baud | 61 | 5.75 | 2.62 |
| sane_mode | 62 | 7.25 | 2.12 |
| set_suffix_length | 64 | 12.12 | 4.00 |
| parse_symbols | 64 | 7.86 | 2.86 |
| set_file_security_ctx | 65 | 6.43 | 3.29 |
| … | … | … | … |
| main | 63790 | 168.62 | 104.87 |
| lines_rr | 74899 | 225.39 | 42.56 |
| ng_pollard_rho2 | 1416562 | 161.14 | 63.73 |
| ine_bytes_split | 1425449 | 233.27 | 42.88 |
| parents_private | 84197299 | 114.97 | 38.41 |
As the number of path goes past 40–50 into moderately complex functions we are looking at from 5–10 speed improvement over computing and 2–4x speed improvement over the old cache. For complex functions, more than 50 000 paths, the new cache is more than 100x faster than computing, and more than 40 times faster than the old cache.
One of the ways this has been made quick is by significantly compressing the representation, which also reduces memory pressure. The old cache needs roughly 460 MB to store all the prime paths, while the new cache needs 93 MB to store the same set, about 20%.
Speed matters. This translates into making the experience using zcov much nicer and smoother.