Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trac #24135: Clean up in coerce_dict
1. Deprecate various arguments which should have been deprecated in #15367. 2. Use a tuple instead of a list to throw away. This is very slightly faster. 3. Use safe memory functions from cysignals instead of `PyMem` functions. 4. Split `__init__` into `__cinit__` and `__init__`. 5. Rename the `iteritems()` method to `items()`. 6. Introduce a new inline function `valid(ptr)` to replace the very common `ptr != NULL and ptr != dummy`. 7. Change type of `key_id` from `void*` to `PyObject*`. This avoids a lot of casts. 8. Use a custom class with `@cython.freelist` instead of a `PyCapsule` to wrap a `PyObject*`. 9. In `tp_clear`, only delete references to elements contained in the dict. Move deallocation of the data structure to `__dealloc__`. 10. Use random 31-bit multipliers (instead of `13` and `503`) in the hash function for `TripleDict`. This might give better mixing for large sizes (in any case, it can't hurt). 11. Rename `dummy` -> `deleted_key` to make it more clear what it means. Also, there was no reason that this was of type `bytes`. Now it is simply created by `object()`. 12. Change the logic of the `lookup()` methods a bit to make them easier to understand. 13. Generic code cleanup: PEP 8, `is` instead of `==` for pointers, ... '''Timings''': All the changes above lead to a modest speed-up: ''`MonoDict` lookup'': {{{ sage: from sage.structure.coerce_dict import MonoDict; D = MonoDict() sage: L = [Integer(x) for x in range(1000)] sage: for k in L: D[k] = k sage: timeit('[D[k] for k in L]', repeat=20, number=20000) }}} Before: {{{20000 loops, best of 20: 72.7 µs per loop}}} After: {{{20000 loops, best of 20: 64.5 µs per loop}}} ''`TripleDict` lookup'': {{{ sage: from sage.structure.coerce_dict import TripleDict; D = TripleDict() sage: L = [(None, None, Integer(x)) for x in range(1000)] sage: for k in L: D[k] = None sage: timeit('[D[k] for k in L]', repeat=20, number=20000) }}} Before: {{{20000 loops, best of 20: 97.3 µs per loop}}} After: {{{20000 loops, best of 20: 87.3 µs per loop}}} URL: https://trac.sagemath.org/24135 Reported by: jdemeyer Ticket author(s): Jeroen Demeyer Reviewer(s): Travis Scrimshaw
- Loading branch information