Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Grant Jenks - Python Sorted Collections

Grant Jenks - Python Sorted Collections

C++, Java, and .NET provide sorted collections types. Wish Python did too? Look around and you'll find Pandas DataFrame indexes, Sqlite in-memory databases, even redis-py sorted set commands. The SortedContainers module was designed to fill this gap with sorted list, dict and set implementations. It's written in pure-Python but generally faster than C-extension modules. Come see how it works.

https://us.pycon.org/2016/schedule/presentation/1885/

PyCon 2016

May 29, 2016
Tweet

More Decks by PyCon 2016

Other Decks in Programming

Transcript

  1. 5

  2. 12

  3. blist • Daniel Stutzbach; 2006 start, 2014 last PyPI update.

    • blist.blist B-tree based replacement for list. • Sorted collections based on blist.blist type. • Full-featured, long-standing API. 14
  4. sortedcollection • Raymond Hettinger; published on ActiveState, 2010. • Linked

    from the Python Standard Library docs. • Mostly meant for read-only workloads. 15
  5. • Manfred Moitzi; 2010 start, 2015 last PyPI update. •

    Multiple tree implementations: Binary, AVL, Red-Black. • API extends blist with tree traversal for slicing by value. bintrees 16
  6. banyan • Ami Tavory; 2013 start, 2013 last PyPI update.

    • Highly optimized C++ implementation. • Supports tree-augmentation with metadata. 17
  7. skiplistcollections • Jakub Stasiak; 2013 start, 2014 last PyPI update.

    • Pure-Python with competitive performance. 18
  8. 19

  9. 21

  10. 22

  11. 23

  12. Recipes • ValueSortedDict - dictionary sorted by item value. •

    ItemSortedDict - key, value sort order function. • OrderedDict - insertion order with positional indexing. • IndexableSet - supports positional indexing. • $ pip install sortedcollections 29
  13. List of Sublists [ # _lists [ 0, 1, 2,

    3], [ 4, 5, 6], [ 7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17], ] 33
  14. List of Maxes [ # _lists [ 0, 1, 2,

    3], [ 4, 5, 6], [ 7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17], ] [ # _maxes 3, 6, 12, 17, ] 34
  15. “Jenks” Index 1 lengths = [ 4, 3, 6, 5

    ] 2 pair_wise_sums1 = [ 7, 11 ] 3 pair_wise_sums2 = [ 18 ] 4 _index = [ 18, 7, 11, 4, 3, 6, 5 ] 5 _offset = 3 35
  16. Positional Indexing _index = [ 18, 7, 11, 4, 3,

    6, 5 ] 1 @18, index = 8, position = 0 2 @11, index = 1, position = 2 3 @6, index = 1, position = 5, topindex = 2 _offset = 3 36
  17. SortedList.__contains__ 1 def __contains__(self, val): 2 _lists = self._lists 3

    pos = bisect_left(self._maxes, val) 4 idx = bisect_left(_lists[pos], val) 5 return _lists[pos][idx] == val 38
  18. Memory is Tiered • Registers - dozenish. • L1 Instruction/Data

    Cache - 32 KB. • L2 Cache - 256 KB. • [L3 Cache (Shared) - 8 MB.] • Main Memory - Gigabytes. 40
  19. Memory Access Patterns • Sequential 1 2 3 4 5

    6 7 8 9 10 11 12 13 14 15 16 • Random 10 14 13 3 5 4 12 2 6 11 8 9 7 16 15 1 • Data-dependent 14 8 11 9 5 3 10 13 12 16 2 15 1 6 7 4 41
  20. list.insert 1 for (i = n; --i >= where; )

    2 items[i+1] = items[i]; 3 Py_INCREF(v); 4 items[where] = v; 42
  21. SortedList.__init__ 1 values = sorted(iterable) 2 _lists = [values[pos:pos+load] for

    pos in 3 range(0, len(values), load)] 4 _maxes = [sub[-1] for sub in _lists] 44
  22. SortedSet.add 1 def add(self, value): 2 _set, _list = self._set,

    self._list 3 if value not in _set: 4 _set.add(value) 5 _list.add(value) 45
  23. • Punchline: O(∛n) • Billion integers in CPython: 30 GBs.

    • Timsort: comparisons are expensive. • Memory is expensive. • Performance at Scale: 10,000,000,000 Runtime Complexity 47
  24. • Builtin types are fast. • Program in Python your

    interpreter. • Memory is tiered. • Cheat, if you can. • Measure. Measure. Measure. SortedContainers Performance 49
  25. 50