Commit abdede8
committed
Add ASCII fast path optimization to replace function
Add a fast path for replacing single ASCII characters with another single
ASCII character, matching Rust's str::replace() optimization. This enables
vectorization and avoids UTF-8 boundary checking overhead.
Changes:
- Added ASCII character detection in replace_into_string()
- When both 'from' and 'to' are single ASCII bytes, use direct byte mapping
- Updated benchmark to include single ASCII character replacement tests
Optimization:
- Fast path operates directly on bytes using simple map operation
- Compiler can vectorize the byte-wise replacement
- Avoids overhead of match_indices() pattern matching for this common case
Benchmark Results (Single ASCII Character Replacement):
- size=1024, str_len=32: 29.5 µs → 21.4 µs (27% faster)
- size=1024, str_len=128: 73.9 µs → 23.4 µs (68% faster)
- size=4096, str_len=32: 121.8 µs → 85.6 µs (30% faster)
- size=4096, str_len=128: 316.9 µs → 83.8 µs (74% faster)
The optimization shows exceptional 27-74% improvements, with the benefit
scaling dramatically with string length. For 128-character strings, we
achieve over 3x speedup by enabling vectorization and eliminating
pattern matching overhead.
This addresses reviewer feedback about capturing Rust's str::replace()
optimization tricks for single ASCII character replacements.1 parent c10d410 commit abdede8
File tree
2 files changed
+51
-11
lines changed- datafusion/functions
- benches
- src/string
2 files changed
+51
-11
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
| 39 | + | |
38 | 40 | | |
39 | 41 | | |
40 | 42 | | |
41 | 43 | | |
42 | | - | |
43 | | - | |
| 44 | + | |
| 45 | + | |
44 | 46 | | |
45 | 47 | | |
46 | 48 | | |
| |||
49 | 51 | | |
50 | 52 | | |
51 | 53 | | |
52 | | - | |
53 | | - | |
| 54 | + | |
| 55 | + | |
54 | 56 | | |
55 | 57 | | |
56 | 58 | | |
| |||
87 | 89 | | |
88 | 90 | | |
89 | 91 | | |
90 | | - | |
| 92 | + | |
91 | 93 | | |
92 | | - | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
93 | 107 | | |
94 | 108 | | |
95 | 109 | | |
| |||
100 | 114 | | |
101 | 115 | | |
102 | 116 | | |
103 | | - | |
| 117 | + | |
104 | 118 | | |
105 | 119 | | |
106 | 120 | | |
| |||
111 | 125 | | |
112 | 126 | | |
113 | 127 | | |
114 | | - | |
| 128 | + | |
115 | 129 | | |
116 | 130 | | |
117 | 131 | | |
| |||
124 | 138 | | |
125 | 139 | | |
126 | 140 | | |
127 | | - | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
128 | 153 | | |
129 | 154 | | |
130 | 155 | | |
| |||
135 | 160 | | |
136 | 161 | | |
137 | 162 | | |
138 | | - | |
| 163 | + | |
139 | 164 | | |
140 | 165 | | |
141 | 166 | | |
| |||
146 | 171 | | |
147 | 172 | | |
148 | 173 | | |
149 | | - | |
| 174 | + | |
150 | 175 | | |
151 | 176 | | |
152 | 177 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
228 | 228 | | |
229 | 229 | | |
230 | 230 | | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
231 | 246 | | |
232 | 247 | | |
233 | 248 | | |
| |||
0 commit comments