(s: &Wtf8)
| 20 | } |
| 21 | |
| 22 | fn do_count_chars(s: &Wtf8) -> usize { |
| 23 | // For correctness, `CHUNK_SIZE` must be: |
| 24 | // |
| 25 | // - Less than or equal to 255, otherwise we'll overflow bytes in `counts`. |
| 26 | // - A multiple of `UNROLL_INNER`, otherwise our `break` inside the |
| 27 | // `body.chunks(CHUNK_SIZE)` loop is incorrect. |
| 28 | // |
| 29 | // For performance, `CHUNK_SIZE` should be: |
| 30 | // - Relatively cheap to `/` against (so some simple sum of powers of two). |
| 31 | // - Large enough to avoid paying for the cost of the `sum_bytes_in_usize` |
| 32 | // too often. |
| 33 | const CHUNK_SIZE: usize = 192; |
| 34 | |
| 35 | // Check the properties of `CHUNK_SIZE` and `UNROLL_INNER` that are required |
| 36 | // for correctness. |
| 37 | const _: () = assert!(CHUNK_SIZE < 256); |
| 38 | const _: () = assert!(CHUNK_SIZE.is_multiple_of(UNROLL_INNER)); |
| 39 | |
| 40 | // SAFETY: transmuting `[u8]` to `[usize]` is safe except for size |
| 41 | // differences which are handled by `align_to`. |
| 42 | let (head, body, tail) = unsafe { s.as_bytes().align_to::<usize>() }; |
| 43 | |
| 44 | // This should be quite rare, and basically exists to handle the degenerate |
| 45 | // cases where align_to fails (as well as miri under symbolic alignment |
| 46 | // mode). |
| 47 | // |
| 48 | // The `unlikely` helps discourage LLVM from inlining the body, which is |
| 49 | // nice, as we would rather not mark the `char_count_general_case` function |
| 50 | // as cold. |
| 51 | if unlikely(body.is_empty() || head.len() > USIZE_SIZE || tail.len() > USIZE_SIZE) { |
| 52 | return char_count_general_case(s.as_bytes()); |
| 53 | } |
| 54 | |
| 55 | let mut total = char_count_general_case(head) + char_count_general_case(tail); |
| 56 | // Split `body` into `CHUNK_SIZE` chunks to reduce the frequency with which |
| 57 | // we call `sum_bytes_in_usize`. |
| 58 | for chunk in body.chunks(CHUNK_SIZE) { |
| 59 | // We accumulate intermediate sums in `counts`, where each byte contains |
| 60 | // a subset of the sum of this chunk, like a `[u8; size_of::<usize>()]`. |
| 61 | let mut counts = 0; |
| 62 | |
| 63 | let (unrolled_chunks, remainder) = chunk.as_chunks::<UNROLL_INNER>(); |
| 64 | for unrolled in unrolled_chunks { |
| 65 | for &word in unrolled { |
| 66 | // Because `CHUNK_SIZE` is < 256, this addition can't cause the |
| 67 | // count in any of the bytes to overflow into a subsequent byte. |
| 68 | counts += contains_non_continuation_byte(word); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // Sum the values in `counts` (which, again, is conceptually a `[u8; |
| 73 | // size_of::<usize>()]`), and accumulate the result into `total`. |
| 74 | total += sum_bytes_in_usize(counts); |
| 75 | |
| 76 | // If there's any data in `remainder`, then handle it. This will only |
| 77 | // happen for the last `chunk` in `body.chunks()` (because `CHUNK_SIZE` |
| 78 | // is divisible by `UNROLL_INNER`), so we explicitly break at the end |
| 79 | // (which seems to help LLVM out). |
no test coverage detected