| 1 |
DjangoCon 2015 Lightning Talk |
| 2 |
============================= |
| 3 |
|
| 4 |
* `Accompanying Slides`_ |
| 5 |
|
| 6 |
Welcome. I'm excited to be here today. My name is Grant Jenks and I've been |
| 7 |
working with Python since about 2011. Like many of you, I was pleasantly |
| 8 |
surprised and impressed by Python's "batteries included" slogan. |
| 9 |
|
| 10 |
Not long into Python development, I started asking questions like these: |
| 11 |
* "How do you sort a Python dictionary by value?" |
| 12 |
* "How do I get the maximum value in a dictionary?" |
| 13 |
* "Can I somehow search a dictionary's keys?" |
| 14 |
These questions get viewed millions of times for every priority queue, or |
| 15 |
cache, or in-memory index, or even indexable set that somebody needs. |
| 16 |
|
| 17 |
In response, I got some great answers. The first was OrderedDict. But you |
| 18 |
quickly learn that ordered isn't the same as sorted. |
| 19 |
|
| 20 |
And then there was Counter. It gets combined with another module, heapq, which |
| 21 |
can maintain a binary heap. |
| 22 |
|
| 23 |
All of these were wonderfully fast except when they weren't. They all kind |
| 24 |
of assume that updates can be batched and sorted methods can work lazily. |
| 25 |
And that gets us really far. Really far. But sometimes you want more. |
| 26 |
|
| 27 |
So if C++ and Java and .NET all have sorted map-like data types, why doesn't |
| 28 |
Python? I found the answer from Nick Coghlan paraphrasing Guido: “if a user is |
| 29 |
sophisticated enough to realise that the builtin types aren't the right |
| 30 |
solution for their problem, then they're also up to the task of finding an |
| 31 |
appropriate third party library.” |
| 32 |
|
| 33 |
To that I say, good luck. If you go out there, you'll find over a dozen |
| 34 |
different solutions all with different APIs and varying levels of compatibility |
| 35 |
and performance. If we decided only by most-downloaded we'd end up with blist. |
| 36 |
So let's try that. |
| 37 |
|
| 38 |
Blist implements a B-tree data type in C with a node size of 128. And I was |
| 39 |
happy with it until one day I made this discovery. I'm surprised here because |
| 40 |
blist should shuffle 128 pointers while bisect should shuffle 1000 |
| 41 |
pointers. How can bisect be faster here? |
| 42 |
|
| 43 |
So I went back to the libraries and tried some others. RBtree uses |
| 44 |
Red-Black. Banyan has that too and a Splay option. Bintrees has Binary, |
| 45 |
Red-Black and AVL. All of those are implemented in C or C++. Then I discovered |
| 46 |
a Skip-list implementation that was slower but pure-Python. |
| 47 |
|
| 48 |
All I can say in five minutes is :doc:`Sorted Containers<index>` doesn't use |
| 49 |
any of these exactly. It's kind of like a B-tree but only half-heartedly. It |
| 50 |
relies entirely on the bisect module. While it's slow to program in Python, the |
| 51 |
interpreter is written in C. So if you think of it as programming the |
| 52 |
interpreter, you're effectively writing C code. It turns out lists are fast; |
| 53 |
trees, not so much. |
| 54 |
|
| 55 |
Listen to what these smart people have to say about it: |
| 56 |
|
| 57 |
* Alex Martelli: Good stuff! ... I like the simple, effective implementation |
| 58 |
idea of splitting the sorted containers into smaller “fragments” to avoid the |
| 59 |
O(N) insertion costs. |
| 60 |
|
| 61 |
* Jeff Knupp: That last part, “fast as C-extensions,” was difficult to |
| 62 |
believe. I would need some sort of performance comparison to be convinced |
| 63 |
this is true. The author includes this in the docs. It is. |
| 64 |
|
| 65 |
* Kevin Samuel, I’m quite amazed, not just by the code quality, but the actual |
| 66 |
amount of work you put at stuff that is not code: documentation, |
| 67 |
benchmarking, implementation explanations. |
| 68 |
|
| 69 |
Lastly, Mark Summerfield makes a short plea: Python’s “batteries included” |
| 70 |
standard library seems to have a battery missing. And the argument that “we |
| 71 |
never had it before” has worn thin. It is time that Python offered a full range |
| 72 |
of collection classes out of the box, including sorted ones. |
| 73 |
|
| 74 |
Ladies and gentlemen, the :doc:`Sorted Containers<index>` library. Thank you. |
| 75 |
|
| 76 |
.. _`Accompanying Slides`: http://bit.ly/socoin5 |