| 1 | // Adapted from the coregex project, https://github.com/coregx/coregex |
| 2 | // (package simd), Copyright (c) 2025 Andrey Kolkov and contributors, |
| 3 | // MIT License. See the LICENSE file in this directory for the full text. |
| 4 | |
| 5 | package simd |
| 6 | |
| 7 | // byteFrequencies holds empirical byte frequency ranks derived from English |
| 8 | // text, source code, and binary data samples. Lower rank means rarer byte, |
| 9 | // which makes it a better anchor for a search prefilter. The approach |
| 10 | // mirrors Rust's memchr crate (https://github.com/BurntSushi/memchr). |
| 11 | // The table is unexported (read it through ByteRank) so callers cannot |
| 12 | // mutate the ranking Memmem's prefilter selection depends on. |
| 13 | var byteFrequencies = [256]byte{ |
| 14 | // 0x00-0x0F: control characters (generally rare) |
| 15 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, |
| 16 | // 0x10-0x1F: more control characters |
| 17 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 18 | // 0x20-0x2F: space and punctuation |
| 19 | 255, 60, 140, 50, 40, 35, 30, 160, 130, 130, 80, 55, 200, 140, 210, 100, |
| 20 | // 0x30-0x3F: digits and more punctuation |
| 21 | 180, 190, 170, 150, 140, 140, 130, 120, 120, 120, 150, 100, 70, 160, 70, 50, |
| 22 | // 0x40-0x4F: '@' and uppercase A-O |
| 23 | 25, 120, 80, 90, 85, 130, 75, 70, 80, 115, 30, 35, 90, 85, 100, 105, |
| 24 | // 0x50-0x5F: uppercase P-Z and brackets |
| 25 | 80, 15, 100, 110, 115, 70, 45, 55, 20, 50, 10, 90, 60, 90, 20, 110, |
| 26 | // 0x60-0x6F: backtick and lowercase a-o |
| 27 | 30, 225, 140, 170, 165, 245, 135, 130, 150, 200, 25, 65, 175, 155, 195, 205, |
| 28 | // 0x70-0x7F: lowercase p-z and braces |
| 29 | 145, 15, 195, 200, 215, 150, 75, 95, 45, 120, 20, 85, 40, 85, 15, 0, |
| 30 | // 0x80-0xFF: UTF-8 continuation/lead bytes (rare in typical text) |
| 31 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 32 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 33 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 34 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 35 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 36 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 37 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 38 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 39 | } |
| 40 | |
| 41 | // ByteRank returns the empirical frequency rank of b. Lower values indicate |
| 42 | // rarer bytes, which are better anchors for search prefilters; SelectRareBytes |
| 43 | // and Memmem rank needle bytes with it. |
| 44 | func ByteRank(b byte) byte { |
| 45 | return byteFrequencies[b] |
| 46 | } |
| 47 | |
| 48 | // RareByteInfo describes the two rarest bytes of a needle, as selected by |
| 49 | // SelectRareBytes. When the needle contains at least two distinct byte |
| 50 | // values, Byte2 always differs from Byte1; only for empty, single-byte, or |
| 51 | // all-identical needles do Byte2/Index2 equal Byte1/Index1. |
| 52 | type RareByteInfo struct { |
| 53 | // Byte1 is the rarest byte found in the needle. |
| 54 | Byte1 byte |
| 55 | // Byte2 is the rarest byte with a value different from Byte1, when the |
| 56 | // needle has one; otherwise it equals Byte1. |
| 57 | Byte2 byte |
| 58 | // Index1 is the position of Byte1's first occurrence in the needle. |
| 59 | Index1 int |
| 60 | // Index2 is the position of Byte2's first occurrence in the needle. |
| 61 | Index2 int |
| 62 | } |
| 63 | |
| 64 | // SelectRareBytes returns the two rarest bytes of needle according to |
| 65 | // ByteRank, preferring distinct byte values: whenever the needle contains |
| 66 | // two distinct values, Byte2 is the rarest byte different from Byte1, so |
| 67 | // duplicated occurrences of the rarest byte never crowd out a usable |
| 68 | // second anchor. Memmem feeds the result to MemchrPair to build a highly |
| 69 | // selective substring prefilter; it is exported so downstream code can |
| 70 | // build its own prefilters the same way. |
| 71 | func SelectRareBytes(needle []byte) RareByteInfo { |
| 72 | n := len(needle) |
| 73 | if n == 0 { |
| 74 | return RareByteInfo{} |
| 75 | } |
| 76 | |
| 77 | byte1, idx1 := needle[0], 0 |
| 78 | byte2, idx2 := needle[0], 0 |
| 79 | have2 := false |
| 80 | |
| 81 | for i := 1; i < n; i++ { |
| 82 | b := needle[i] |
| 83 | switch { |
| 84 | case byteFrequencies[b] < byteFrequencies[byte1]: |
| 85 | // b is strictly rarer than byte1, so it also has a different |
| 86 | // value; the demoted byte1 is at least as rare as any previous |
| 87 | // byte2 and becomes the second anchor. |
| 88 | byte2, idx2 = byte1, idx1 |
| 89 | byte1, idx1 = b, i |
| 90 | have2 = true |
| 91 | case b != byte1 && (!have2 || byteFrequencies[b] < byteFrequencies[byte2]): |
| 92 | byte2, idx2 = b, i |
| 93 | have2 = true |
| 94 | } |
| 95 | } |
| 96 | if !have2 { |
| 97 | // All bytes identical: no distinct second anchor exists. |
| 98 | byte2, idx2 = byte1, idx1 |
| 99 | } |
| 100 | |
| 101 | return RareByteInfo{Byte1: byte1, Byte2: byte2, Index1: idx1, Index2: idx2} |
| 102 | } |