Commit 77b3e8e
committed
unix-ffi/re: Cache the compiled patterns.
Every call to re.search(), and to the functions next to it, compiled the
pattern it was given. Keep the compiled patterns in a small cache
instead, the way CPython does, so that using the same pattern again does
not compile it a second time. compile() returns the cached pattern as
well, so re.compile(p) is re.compile(p), as it is in CPython. Matching
against a repeated pattern gets about twice as fast, compiling one about
eight times.
Because MicroPython cannot release a compiled pattern by itself, the
cache also decides what is kept: a cached pattern stays for the lifetime
of the program, and a pattern that this module compiled for its own use
is freed again afterwards.
The cache owns what it holds and never evicts it. A pattern that is
still in use, by the caller or by a call further up the stack, must not
be freed underneath it, which a replacement callback passed to sub() can
otherwise trigger. The cache is bounded instead: once it is full,
further patterns are compiled and, where this module owns them, freed
again after use.
Signed-off-by: Kirill Lukonin (Evil Wireless Man) <klukonin@gmail.com>1 parent df9ba13 commit 77b3e8e
2 files changed
Lines changed: 103 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
| 94 | + | |
94 | 95 | | |
95 | 96 | | |
96 | 97 | | |
97 | 98 | | |
98 | 99 | | |
99 | 100 | | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
100 | 106 | | |
101 | 107 | | |
102 | 108 | | |
| |||
184 | 190 | | |
185 | 191 | | |
186 | 192 | | |
187 | | - | |
| 193 | + | |
188 | 194 | | |
189 | 195 | | |
190 | 196 | | |
| |||
194 | 200 | | |
195 | 201 | | |
196 | 202 | | |
197 | | - | |
198 | | - | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
199 | 234 | | |
200 | 235 | | |
201 | 236 | | |
202 | | - | |
| 237 | + | |
203 | 238 | | |
204 | 239 | | |
205 | 240 | | |
206 | | - | |
| 241 | + | |
| 242 | + | |
207 | 243 | | |
208 | 244 | | |
209 | 245 | | |
210 | | - | |
| 246 | + | |
211 | 247 | | |
212 | 248 | | |
213 | 249 | | |
214 | | - | |
| 250 | + | |
| 251 | + | |
215 | 252 | | |
216 | 253 | | |
217 | 254 | | |
218 | | - | |
| 255 | + | |
219 | 256 | | |
220 | 257 | | |
221 | 258 | | |
222 | | - | |
| 259 | + | |
| 260 | + | |
223 | 261 | | |
224 | 262 | | |
225 | 263 | | |
226 | | - | |
| 264 | + | |
227 | 265 | | |
228 | 266 | | |
229 | 267 | | |
230 | | - | |
| 268 | + | |
| 269 | + | |
231 | 270 | | |
232 | 271 | | |
233 | 272 | | |
234 | | - | |
| 273 | + | |
235 | 274 | | |
236 | 275 | | |
237 | 276 | | |
238 | | - | |
| 277 | + | |
| 278 | + | |
239 | 279 | | |
240 | 280 | | |
241 | 281 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
9 | 13 | | |
10 | 14 | | |
11 | 15 | | |
| |||
21 | 25 | | |
22 | 26 | | |
23 | 27 | | |
| 28 | + | |
24 | 29 | | |
25 | 30 | | |
26 | 31 | | |
| |||
97 | 102 | | |
98 | 103 | | |
99 | 104 | | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
0 commit comments