diff --git a/README.md b/README.md index 27c2359..363f4aa 100644 --- a/README.md +++ b/README.md @@ -149,13 +149,16 @@ bite with them. | `-L`, 11 GB | 1.29 s | **0.56 s** | | 1050 × 1 MiB, `-lwc` | 145 ms | **45 ms** | | 1050 × 1 MiB, `-L` | 1.23 s | **66 ms** | +| 1050 × 1 MiB, `-c` | 3.7 ms | **2.1 ms** | +| 1050 × 1 MiB, `-m` | 30.8 ms | **2.7 ms** | Same files, same machine, both binaries racing the clock back to back. -Read the last row twice: on a thousand small files we are eighteen times +Read the `-L` row twice: on a thousand small files we are eighteen times faster at the one thing it used to win, because the width kernel now computes a whole 64-byte block's line widths with prefix sums and one packed subtract, and a long file list is spread over the cores instead of -counted one file at a time. +counted one file at a time. The `-c` and `-m` rows are the other half of +that: a count that is a file size never opens the file. Rust is the femboy language of systems programming: spotless hygiene, elaborate safety equipment, and a firm opinion about how you should be @@ -176,13 +179,16 @@ whether it holds one line or sixty. That isn't a language feature. It's maths their kernel doesn't do, and ours does. The same goes for the locale. Their "decode only around the high bytes" -instinct was right, so the UTF-8 path now splits the mapping into chunks -that decode independently and hands every ASCII stretch to the byte -kernels. 10 MB of lightly thé'd text under `en_US.UTF-8`: **1.6 ms**, -against their 2.8 ms and GNU's 30 ms. Dense CJK is the one place they -still edge us on `-w` and `-m`, because they carry their own width table -and we call `wcwidth` so the counts match GNU exactly. We are five times -ahead of GNU there and we will take the trade. +instinct was right, so the UTF-8 path splits the mapping into chunks that +decode independently, hands every ASCII stretch to the byte kernels, and +counts dense CJK a 64-byte block at a time from masks rather than a +character at a time. 100 MB of lightly thé'd text under `en_US.UTF-8`: +**5.5 ms**, against their 6.2 and GNU's 224. On dense CJK characters we +are 7.6 ms against their 18.7; on CJK words and longest line they still +have us by a quarter, because they keep a width table and trust the +encoding where we validate and call `wcwidth` to match GNU exactly. We +will take a quarter of a loss on two flags of one alphabet for exact +counts on all of them. ## What it does diff --git a/docs/PERFORMANCE.md b/docs/PERFORMANCE.md index 113c45b..2a3b879 100644 --- a/docs/PERFORMANCE.md +++ b/docs/PERFORMANCE.md @@ -158,8 +158,11 @@ half and then the honest reporting: 6. **No work that isn't asked for.** `-c` on a regular file is `st_size` from `fstat` — GNU figured that one out too, so we copied the good idea, and extended it to `-m` in a single-byte locale, where a - character is a byte. `-l` without `-w` skips the whitespace mask - entirely; `-w` without `-L` never builds the print table. + character is a byte. Then we stopped opening a stream for it at all: + the size the column width was computed from is the answer, so a + thousand small files cost the stat pass and a row each. `-l` without + `-w` skips the whitespace mask entirely; `-w` without `-L` never + builds the print table. 7. **ASCII pays nothing, even in a UTF-8 locale.** Multibyte decoding is expensive, so we don't volunteer for it. A sample of the first few KiB and two points in the middle decides whether a file is worth @@ -200,19 +203,26 @@ The UTF-8 decoder used to be the same story: correct, GNU-exact, and serial. It paid for every byte of a file, after a probe pass had already read the whole thing once. Now a UTF-8 file is split at line ends, each chunk decodes without inheriting state, ASCII stretches go to the byte -kernels, and only the characters around a high byte are decoded. 10 MB -under `en_US.UTF-8`: +kernels, and dense CJK is counted a 64-byte block at a time from masks: +characters as bytes minus continuations, lines from the newline mask, +words from a byte-level white-space mask. A block that is not exactly +ASCII plus three-byte characters is refused and decoded the slow way, so +the fast path never guesses. 100 MB under `en_US.UTF-8`: | case | before | after | Rust clone | GNU | |------|-------:|------:|-----------:|----:| -| words, lightly multibyte | 26.7 ms | **1.6 ms** | 2.8 ms | 30.2 ms | -| words, dense CJK | 33.8 ms | **7.8 ms** | 2.7 ms | 38.9 ms | -| longest line, dense CJK | 43.0 ms | **11.5 ms** | 6.0 ms | 46.8 ms | +| words, lightly multibyte | 6.8 ms | **5.5 ms** | 6.2 ms | 224 ms | +| longest line, lightly multibyte | 26.2 ms | **9.1 ms** | 11.6 ms | 223 ms | +| characters, dense CJK | 46.2 ms | **7.6 ms** | 18.7 ms | 458 ms | +| words, dense CJK | 55.8 ms | **8.6 ms** | 6.8 ms | 464 ms | +| longest line, dense CJK | 59.1 ms | **43.1 ms** | 33.7 ms | 587 ms | -Dense CJK is the one case the clone still wins, because it carries a -width table and we call `wcwidth` so the counts agree with GNU on every -code point. We are 5x ahead of GNU there; the parity we care about is -with the oracle, not with the clone. +The clone still wins words and longest line on dense CJK, by 26% and +28%, because it carries its own width table and trusts the encoding where +we validate and call `wcwidth`. Both of us are 20 to 50 times ahead of +GNU there, and on everything else - ASCII, lightly multibyte text, +characters in any mix - we are ahead of the clone. This is the honest +state of the one case it still owns. ## Threads you actually have @@ -251,12 +261,13 @@ locales, binary and UTF-8 data, every flag combination) passes 100%. ## Known divergences, stated plainly -- **Dense CJK is not where we win.** `-w` and `-m` over text that is - multibyte end to end decode a character at a time, because a correct - count needs `wcwidth`/`iswspace` per code point. The Rust clone carries - its own table and is 2-3x faster there. Both of us are 5x faster than - GNU, which is the number the contract cares about, and ASCII — the case - that actually shows up in a shell — takes the byte kernels. +- **Dense CJK still belongs to them on two flags.** `-w` and `-L` over + text that is multibyte end to end are 26% and 28% behind the Rust + clone, which carries its own width table and does not validate the + encoding. We validate every block and call `wcwidth`, because the + counts have to match GNU on every code point, and both of us are 20 to + 50 times ahead of GNU there. Characters (`-m`) and every ASCII or + lightly multibyte case are ours. - **A tab-heavy block walks its runs.** The prefix-sum width kernel assumes a printable byte is worth one column. A tab is worth between one and eight depending on where in the line it sits, so any block