Commit 42a0663
Optimize PrefixMap find by walking through small prefixes
Summary:
When the bucket's SmallPrefix differs from the query's (or query < 8 bytes), skip the binary search entirely and walk backwards from roughTo using pre-built SmallPrefix values and precomputed masks. This avoids touching fullPrefixes_ string data entirely — we only do uint64 comparisons.
* Add SmallPrefix::starts_with_mask() that uses a precomputed mask for the fast-path walk, avoiding runtime bit-counting.
* Add bucketInfo_ vector of (bucketIdx, prefixMask) pairs: the bucket index locates the pre-built SmallPrefix in smallPrefixes_, and the mask has 1-bits for the significant prefix bytes (derived from min(prefix.size(), 8 ) at construction). Both are read in the same cache line during the walk.
* Populate bucketInfo_ in a separate loop after smallPrefixes_ is fully built, so we don't depend on iterator stability during insertions.
* Template findPrefix into findPrefixImpl<enableSmallPrefixLookup> so the constructor can use the slow path (findPrefixImpl<false>) while bucketInfo_ is not yet populated.
Benchmark command:
```
buck run fbcode/mode/opt fbcode//mcrouter/lib/fbi/cpp/test/facebook:string_prefix_map_benchmark -- --bm_regex _Lb --bm_mode=adaptive --bm_min_secs=5 --bm_max_secs=30
```
# x86
Before:
```
============================================================================
[...]facebook/StringPrefixMapBenchmark.cpp relative time/iter iters/s
============================================================================
PrefixMapInCache_Lb 68.87ns 14.52M
PrefixMapNotInCache_Lb 305.95ns 3.27M
```
After:
```
============================================================================
[...]facebook/StringPrefixMapBenchmark.cpp relative time/iter iters/s
============================================================================
PrefixMapInCache_Lb 54.56ns 18.33M
PrefixMapNotInCache_Lb 233.65ns 4.28M
```
- PrefixMapInCache_Lb: 68.87ns → 54.56ns (21% less time/iter)
- PrefixMapNotInCache_Lb: 305.95ns → 233.65ns (24% less time/iter)
# ARM
Before
```
============================================================================
[...]facebook/StringPrefixMapBenchmark.cpp relative time/iter iters/s
============================================================================
PrefixMapInCache_Lb 93.37ns 10.71M
PrefixMapNotInCache_Lb 458.93ns 2.18M
```
After
```
============================================================================
[...]facebook/StringPrefixMapBenchmark.cpp relative time/iter iters/s
============================================================================
PrefixMapInCache_Lb 76.96ns 12.99M
PrefixMapNotInCache_Lb 376.88ns 2.65M
```
- PrefixMapInCache_Lb: 93.37ns → 76.96ns (18% less time/iter)
- PrefixMapNotInCache_Lb: 458.93ns → 376.88ns (18% less time/iter)
Reviewed By: DenisYaroshevskiy
Differential Revision: D97313182
fbshipit-source-id: ba0e9ddd59e4eb9a8df367860260e30cb06d5d5a1 parent 067ebcd commit 42a0663
3 files changed
Lines changed: 108 additions & 12 deletions
File tree
- mcrouter/lib/fbi/cpp
- test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| 14 | + | |
| 15 | + | |
13 | 16 | | |
14 | 17 | | |
15 | 18 | | |
| |||
46 | 49 | | |
47 | 50 | | |
48 | 51 | | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
49 | 60 | | |
50 | 61 | | |
51 | 62 | | |
| |||
57 | 68 | | |
58 | 69 | | |
59 | 70 | | |
| 71 | + | |
60 | 72 | | |
61 | 73 | | |
62 | 74 | | |
| |||
68 | 80 | | |
69 | 81 | | |
70 | 82 | | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
71 | 86 | | |
72 | 87 | | |
73 | 88 | | |
74 | 89 | | |
75 | 90 | | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
76 | 95 | | |
77 | 96 | | |
78 | 97 | | |
| |||
90 | 109 | | |
91 | 110 | | |
92 | 111 | | |
| 112 | + | |
93 | 113 | | |
94 | 114 | | |
95 | 115 | | |
96 | 116 | | |
97 | | - | |
| 117 | + | |
98 | 118 | | |
99 | 119 | | |
100 | 120 | | |
101 | | - | |
| 121 | + | |
102 | 122 | | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
113 | 144 | | |
114 | 145 | | |
115 | 146 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
84 | 88 | | |
85 | 89 | | |
86 | 90 | | |
87 | 91 | | |
88 | 92 | | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
89 | 98 | | |
90 | 99 | | |
91 | 100 | | |
| |||
125 | 134 | | |
126 | 135 | | |
127 | 136 | | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
128 | 145 | | |
129 | 146 | | |
130 | 147 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
210 | 210 | | |
211 | 211 | | |
212 | 212 | | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
213 | 261 | | |
214 | 262 | | |
0 commit comments