| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Runtime.CompilerServices; |
| | | 8 | | |
| | | 9 | | namespace System.Buffers |
| | | 10 | | { |
| | | 11 | | internal struct AhoCorasickNode |
| | | 12 | | { |
| | 0 | 13 | | private static object EmptyChildrenSentinel => Array.Empty<int>(); |
| | | 14 | | |
| | | 15 | | public int SuffixLink; |
| | | 16 | | public int MatchLength; |
| | | 17 | | |
| | | 18 | | // This is not a radix tree so we may have a lot of very sparse nodes (single child). |
| | | 19 | | // We save 1 child separately to avoid allocating a separate collection in such cases. |
| | | 20 | | private int _firstChildChar; |
| | | 21 | | private int _firstChildIndex; |
| | | 22 | | private object _children; // Either int[] or Dictionary<char, int> |
| | | 23 | | |
| | | 24 | | public AhoCorasickNode() |
| | | 25 | | { |
| | 0 | 26 | | _firstChildChar = -1; |
| | 0 | 27 | | _children = EmptyChildrenSentinel; |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 31 | | public readonly bool TryGetChild(char c, out int index) |
| | | 32 | | { |
| | 0 | 33 | | if (_firstChildChar == c) |
| | | 34 | | { |
| | 0 | 35 | | index = _firstChildIndex; |
| | 0 | 36 | | return true; |
| | | 37 | | } |
| | | 38 | | |
| | 0 | 39 | | object children = _children; |
| | 0 | 40 | | Debug.Assert(children is int[] || children is Dictionary<char, int>); |
| | | 41 | | |
| | 0 | 42 | | if (children.GetType() == typeof(int[])) |
| | | 43 | | { |
| | 0 | 44 | | int[] table = (int[])children; |
| | 0 | 45 | | if (c < (uint)table.Length) |
| | | 46 | | { |
| | 0 | 47 | | index = table[c]; |
| | 0 | 48 | | if (index >= 0) |
| | | 49 | | { |
| | 0 | 50 | | return true; |
| | | 51 | | } |
| | | 52 | | } |
| | | 53 | | } |
| | | 54 | | else |
| | | 55 | | { |
| | 0 | 56 | | return Unsafe.As<Dictionary<char, int>>(children).TryGetValue(c, out index); |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | index = 0; |
| | 0 | 60 | | return false; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | public void AddChild(char c, int index) |
| | | 64 | | { |
| | 0 | 65 | | if (_firstChildChar < 0) |
| | | 66 | | { |
| | 0 | 67 | | _firstChildChar = c; |
| | 0 | 68 | | _firstChildIndex = index; |
| | | 69 | | } |
| | | 70 | | else |
| | | 71 | | { |
| | 0 | 72 | | if (ReferenceEquals(_children, EmptyChildrenSentinel)) |
| | | 73 | | { |
| | 0 | 74 | | _children = new Dictionary<char, int>(); |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | ((Dictionary<char, int>)_children).Add(c, index); |
| | | 78 | | } |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | public readonly void AddChildrenToQueue(Queue<(char Char, int Index)> queue) |
| | | 82 | | { |
| | 0 | 83 | | if (_firstChildChar >= 0) |
| | | 84 | | { |
| | 0 | 85 | | queue.Enqueue(((char)_firstChildChar, _firstChildIndex)); |
| | | 86 | | |
| | 0 | 87 | | if (_children is Dictionary<char, int> children) |
| | | 88 | | { |
| | 0 | 89 | | foreach ((char childChar, int childIndex) in children) |
| | | 90 | | { |
| | 0 | 91 | | queue.Enqueue((childChar, childIndex)); |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | else |
| | | 95 | | { |
| | 0 | 96 | | Debug.Assert(ReferenceEquals(_children, EmptyChildrenSentinel)); |
| | | 97 | | } |
| | | 98 | | } |
| | 0 | 99 | | } |
| | | 100 | | |
| | | 101 | | public void OptimizeChildren() |
| | | 102 | | { |
| | 0 | 103 | | if (_children is Dictionary<char, int> children) |
| | | 104 | | { |
| | 0 | 105 | | children.Add((char)_firstChildChar, _firstChildIndex); |
| | | 106 | | |
| | 0 | 107 | | float frequency = -2; |
| | | 108 | | |
| | | 109 | | // We have the _firstChildChar field that will always be checked first. |
| | | 110 | | // Improve throughput by setting it to the child character with the highest frequency. |
| | 0 | 111 | | foreach ((char childChar, int childIndex) in children) |
| | | 112 | | { |
| | 0 | 113 | | float newFrequency = char.IsAscii(childChar) ? CharacterFrequencyHelper.AsciiFrequency[childChar] : |
| | | 114 | | |
| | 0 | 115 | | if (newFrequency > frequency) |
| | | 116 | | { |
| | 0 | 117 | | frequency = newFrequency; |
| | 0 | 118 | | _firstChildChar = childChar; |
| | 0 | 119 | | _firstChildIndex = childIndex; |
| | | 120 | | } |
| | | 121 | | } |
| | | 122 | | |
| | 0 | 123 | | children.Remove((char)_firstChildChar); |
| | | 124 | | |
| | 0 | 125 | | if (TryCreateJumpTable(children, out int[]? table)) |
| | | 126 | | { |
| | 0 | 127 | | _children = table; |
| | | 128 | | } |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | static bool TryCreateJumpTable(Dictionary<char, int> children, [NotNullWhen(true)] out int[]? table) |
| | | 132 | | { |
| | | 133 | | // We can use either a Dictionary<char, int> or int[] to map child characters to node indexes. |
| | | 134 | | // int[] is generally faster but consumes more memory for characters with high values. |
| | | 135 | | // We try to find the right balance between memory usage and lookup performance. |
| | | 136 | | // Currently we will sacrifice up to ~2x the memory consumption to use int[] for faster lookups. |
| | | 137 | | const int AcceptableSizeMultiplier = 2; |
| | | 138 | | |
| | 0 | 139 | | Debug.Assert(children.Count > 0); |
| | | 140 | | |
| | 0 | 141 | | int maxValue = -1; |
| | | 142 | | |
| | 0 | 143 | | foreach ((char childChar, _) in children) |
| | | 144 | | { |
| | 0 | 145 | | maxValue = Math.Max(maxValue, childChar); |
| | | 146 | | } |
| | | 147 | | |
| | 0 | 148 | | int tableSize = TableMemoryFootprintBytesEstimate(maxValue); |
| | 0 | 149 | | int dictionarySize = DictionaryMemoryFootprintBytesEstimate(children.Count); |
| | | 150 | | |
| | 0 | 151 | | if (tableSize > dictionarySize * AcceptableSizeMultiplier) |
| | | 152 | | { |
| | | 153 | | // We would have a lot of empty entries. Avoid wasting too much memory. |
| | 0 | 154 | | table = null; |
| | 0 | 155 | | return false; |
| | | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | table = new int[maxValue + 1]; |
| | 0 | 159 | | Array.Fill(table, -1); |
| | | 160 | | |
| | 0 | 161 | | foreach ((char childChar, int childIndex) in children) |
| | | 162 | | { |
| | 0 | 163 | | table[childChar] = childIndex; |
| | | 164 | | } |
| | | 165 | | |
| | 0 | 166 | | return true; |
| | | 167 | | |
| | | 168 | | static int TableMemoryFootprintBytesEstimate(int maxValue) |
| | | 169 | | { |
| | | 170 | | // An approximate number of bytes consumed by an |
| | | 171 | | // int[] table with a known number of entries. |
| | | 172 | | // Only used as a heuristic, so numbers don't have to be exact. |
| | 0 | 173 | | return 32 + (maxValue * sizeof(int)); |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | static int DictionaryMemoryFootprintBytesEstimate(int childCount) |
| | | 177 | | { |
| | | 178 | | // An approximate number of bytes consumed by a |
| | | 179 | | // Dictionary<char, int> with a known number of entries. |
| | | 180 | | // Only used as a heuristic, so numbers don't have to be exact. |
| | 0 | 181 | | return childCount switch |
| | 0 | 182 | | { |
| | 0 | 183 | | < 4 => 192, |
| | 0 | 184 | | < 8 => 272, |
| | 0 | 185 | | < 12 => 352, |
| | 0 | 186 | | _ => childCount * 25 |
| | 0 | 187 | | }; |
| | | 188 | | } |
| | | 189 | | } |
| | 0 | 190 | | } |
| | | 191 | | } |
| | | 192 | | } |
| | | 193 | | |