|
219 | 219 | #define __Pyx_DOCSTR(n) (n)
|
220 | 220 | #endif
|
221 | 221 |
|
| 222 | +/////////////// CodeObjectCache.proto /////////////// |
| 223 | + |
| 224 | +typedef struct { |
| 225 | + int code_line; |
| 226 | + PyCodeObject* code_object; |
| 227 | +} __Pyx_CodeObjectCacheEntry; |
| 228 | + |
| 229 | +struct __Pyx_CodeObjectCache { |
| 230 | + int count; |
| 231 | + int max_count; |
| 232 | + __Pyx_CodeObjectCacheEntry* entries; |
| 233 | +}; |
| 234 | + |
| 235 | +static struct __Pyx_CodeObjectCache __pyx_code_cache = {0,0,NULL}; |
| 236 | + |
| 237 | +static void __pyx_clear_code_object_cache(void); |
| 238 | +static PyCodeObject *__pyx_find_code_object(int code_line); |
| 239 | +static void __pyx_insert_code_object(int code_line, PyCodeObject* code_object); |
| 240 | + |
| 241 | +/////////////// CodeObjectCache /////////////// |
| 242 | +// Note that errors are simply ignored in the code below. |
| 243 | +// This is just a cache, if a lookup or insertion fails - so what? |
| 244 | + |
| 245 | +static void __pyx_clear_code_object_cache(void) { |
| 246 | + __Pyx_CodeObjectCacheEntry* entries = __pyx_code_cache.entries; |
| 247 | + int count = __pyx_code_cache.count; |
| 248 | + int i; |
| 249 | + if (entries == NULL) { |
| 250 | + return; |
| 251 | + } |
| 252 | + __pyx_code_cache.count = 0; |
| 253 | + __pyx_code_cache.max_count = 0; |
| 254 | + __pyx_code_cache.entries = NULL; |
| 255 | + for (i=0; i<count; i++) { |
| 256 | + Py_DECREF(entries[i].code_object); |
| 257 | + } |
| 258 | + PyMem_Free(entries); |
| 259 | +} |
| 260 | + |
| 261 | +static int __pyx_bisect_code_objects(int code_line) { |
| 262 | + int start, end, mid; |
| 263 | + __Pyx_CodeObjectCacheEntry* entries = __pyx_code_cache.entries; |
| 264 | + start = 0; |
| 265 | + end = __pyx_code_cache.count - 1; |
| 266 | + while (start < end) { |
| 267 | + mid = (start + end) / 2; |
| 268 | + if (code_line < entries[mid].code_line) { |
| 269 | + end = mid - 1; |
| 270 | + } else if (code_line > entries[mid].code_line) { |
| 271 | + start = mid + 1; |
| 272 | + } else { |
| 273 | + return mid; |
| 274 | + } |
| 275 | + } |
| 276 | + if (start == end || code_line <= entries[start].code_line) { |
| 277 | + return start; |
| 278 | + } else { |
| 279 | + return end; |
| 280 | + } |
| 281 | +} |
| 282 | + |
| 283 | +static PyCodeObject *__pyx_find_code_object(int code_line) { |
| 284 | + PyCodeObject* code_object; |
| 285 | + int pos; |
| 286 | + if (unlikely(!code_line) || unlikely(!__pyx_code_cache.entries)) { |
| 287 | + return NULL; |
| 288 | + } |
| 289 | + pos = __pyx_bisect_code_objects(code_line); |
| 290 | + if (unlikely(pos >= __pyx_code_cache.count) || unlikely(__pyx_code_cache.entries[pos].code_line != code_line)) { |
| 291 | + return NULL; |
| 292 | + } |
| 293 | + code_object = __pyx_code_cache.entries[pos].code_object; |
| 294 | + Py_INCREF(code_object); |
| 295 | + return code_object; |
| 296 | +} |
| 297 | + |
| 298 | +static void __pyx_insert_code_object(int code_line, PyCodeObject* code_object) { |
| 299 | + int pos, i; |
| 300 | + __Pyx_CodeObjectCacheEntry* entries = __pyx_code_cache.entries; |
| 301 | + if (unlikely(!code_line)) { |
| 302 | + return; |
| 303 | + } |
| 304 | + if (unlikely(!entries)) { |
| 305 | + entries = (__Pyx_CodeObjectCacheEntry*)PyMem_Malloc(64*sizeof(__Pyx_CodeObjectCacheEntry)); |
| 306 | + if (likely(entries)) { |
| 307 | + __pyx_code_cache.entries = entries; |
| 308 | + __pyx_code_cache.max_count = 64; |
| 309 | + __pyx_code_cache.count = 1; |
| 310 | + entries[0].code_line = code_line; |
| 311 | + entries[0].code_object = code_object; |
| 312 | + Py_INCREF(code_object); |
| 313 | + } |
| 314 | + return; |
| 315 | + } |
| 316 | + pos = __pyx_bisect_code_objects(code_line); |
| 317 | + if ((pos < __pyx_code_cache.count) && unlikely(__pyx_code_cache.entries[pos].code_line == code_line)) { |
| 318 | + PyCodeObject* tmp = entries[pos].code_object; |
| 319 | + entries[pos].code_object = code_object; |
| 320 | + Py_DECREF(tmp); |
| 321 | + return; |
| 322 | + } |
| 323 | + if (__pyx_code_cache.count == __pyx_code_cache.max_count) { |
| 324 | + int new_max = __pyx_code_cache.max_count + 64; |
| 325 | + entries = (__Pyx_CodeObjectCacheEntry*)PyMem_Realloc( |
| 326 | + __pyx_code_cache.entries, new_max*sizeof(__Pyx_CodeObjectCacheEntry)); |
| 327 | + if (unlikely(!entries)) { |
| 328 | + return; |
| 329 | + } |
| 330 | + __pyx_code_cache.entries = entries; |
| 331 | + __pyx_code_cache.max_count = new_max; |
| 332 | + } |
| 333 | + for (i=__pyx_code_cache.count; i>pos; i--) { |
| 334 | + entries[i] = entries[i-1]; |
| 335 | + } |
| 336 | + entries[pos].code_line = code_line; |
| 337 | + entries[pos].code_object = code_object; |
| 338 | + __pyx_code_cache.count++; |
| 339 | + Py_INCREF(code_object); |
| 340 | +} |
222 | 341 |
|
223 | 342 | /////////////// CheckBinaryVersion.proto ///////////////
|
224 | 343 |
|
|
0 commit comments