| 1 |
Performance at Scale |
| 2 |
==================== |
| 3 |
|
| 4 |
:doc:`Sorted Containers<index>` scales extremely well. This page discusses why |
| 5 |
both in theory and in practice. The design of :doc:`Sorted Containers<index>` |
| 6 |
permits testing through tens of billions of items and benchmarks present |
| 7 |
results at this scale. The results of this page would be difficult and |
| 8 |
expensive to replicate with other implementations. |
| 9 |
|
| 10 |
Two methods are most important for analysis. Each stresses editing while |
| 11 |
maintaining sorted order. The first is :func:`SortedList.add` which identifies |
| 12 |
the insertion position using binary search. The second is |
| 13 |
:func:`SortedList.__delitem__` which identifies the deletion position using |
| 14 |
indexing. Most other methods are either symmetric or a subset of the runtime |
| 15 |
complexity of these methods. |
| 16 |
|
| 17 |
To fully grasp the content below, its strongly recommended that you read the |
| 18 |
:doc:`implementation details<implementation>` |
| 19 |
first. :doc:`Sorted Containers<index>` maintains a B-tree-like data structure |
| 20 |
limited to two levels of nodes. |
| 21 |
|
| 22 |
Theory |
| 23 |
------ |
| 24 |
|
| 25 |
To discuss the runtime complexity of :doc:`Sorted Containers<index>`, we must |
| 26 |
consider two values: |
| 27 |
|
| 28 |
1. `n` -- the size of the sorted list. |
| 29 |
2. `m` -- the "load" or sublist length. |
| 30 |
|
| 31 |
In a :doc:`sortedlist` with `n` elements, there will be a top-level list of |
| 32 |
pointers to :math:`\frac{n}{m}` sublists each of which are approximately `m` |
| 33 |
elements long. There will also be a "maxes" index which is the max of each |
| 34 |
sublist. The index will contain :math:`\frac{n}{m}` references. |
| 35 |
|
| 36 |
Now adding an element using :func:`SortedList.add` requires these steps: |
| 37 |
|
| 38 |
1. Bisect the "maxes" index to find the appropriate sublist -- |
| 39 |
:math:`O(\log_2{\frac{n}{m}})` |
| 40 |
|
| 41 |
2. Bisect the sublist to find the appropriate insertion position -- |
| 42 |
:math:`O(\log_2{m})` |
| 43 |
|
| 44 |
3. Insert the element in the sublist -- :math:`O(m)` |
| 45 |
|
| 46 |
4. A sublist may be split and inserted into the top-level list -- |
| 47 |
:math:`O(m+\frac{n}{m})` |
| 48 |
|
| 49 |
Consider how often step #4 may occur if we begin with a :doc:`sortedlist` of |
| 50 |
`n` elements and add `n` more elements. The top-level list length will grow |
| 51 |
from :math:`\frac{n}{m}` to :math:`2*\frac{n}{m}` meaning that step #4 was |
| 52 |
executed :math:`\frac{n}{m}` times. |
| 53 |
|
| 54 |
Altogether, growing a :doc:`sortedlist` of `n` elements by `n` more elements |
| 55 |
has the time complexity: |
| 56 |
|
| 57 |
.. math:: |
| 58 |
|
| 59 |
T_{add}(n, m) = O(n * \log_2{\frac{n}{m}} + n * \log_2{m} + n * m + |
| 60 |
\frac{n}{m} * (m + \frac{n}{m})) |
| 61 |
|
| 62 |
Now consider an extreme case: |
| 63 |
|
| 64 |
.. math:: |
| 65 |
|
| 66 |
T_{add}(n|m \propto 1) = O(n * \log_2{\frac{n}{1}} + n * \log_2{1} + n * 1 + |
| 67 |
\frac{n}{1} * (1 + \frac{n}{1})) |
| 68 |
|
| 69 |
T_{add}(n|m \propto 1) = O(n * \log_2{n} + n + n + n * n) |
| 70 |
|
| 71 |
T_{add}(n|m \propto 1) = O(n^2) |
| 72 |
|
| 73 |
The above is why some consider :doc:`Sorted Containers<index>` |
| 74 |
unscalable. Because the load is not chosen dynamically, in theoretical |
| 75 |
analysis, it must be treated as a constant with respect to `n` which makes it |
| 76 |
proportional to 1. |
| 77 |
|
| 78 |
In practice, the default load is 1,000 which is generally closer to the square |
| 79 |
root or cube root of `n`. Consider where `m` is proportional to the square root |
| 80 |
of `n`: |
| 81 |
|
| 82 |
.. math:: |
| 83 |
|
| 84 |
T_{add}(n|m \propto n^\frac{1}{2}) = O(n * \log_2{\frac{n}{n^\frac{1}{2}}} + |
| 85 |
n * \log_2{n^\frac{1}{2}} + n * n^\frac{1}{2} + \frac{n}{n^\frac{1}{2}} * |
| 86 |
(n^\frac{1}{2} + \frac{n}{n^\frac{1}{2}})) |
| 87 |
|
| 88 |
T_{add}(n|m \propto n^\frac{1}{2}) = O(n * \log_2{n^\frac{1}{2}} + n * |
| 89 |
\log_2{n^\frac{1}{2}} + n*n^\frac{1}{2} + n^\frac{1}{2}*n^\frac{1}{2}) |
| 90 |
|
| 91 |
T_{add}(n|m \propto n^\frac{1}{2}) = O(n * n^\frac{1}{2}) |
| 92 |
|
| 93 |
The amortized cost of adding an individual item is therefore proportional to |
| 94 |
the square root of `n`. |
| 95 |
|
| 96 |
Our best bounds will be to use the cube root of `n`: |
| 97 |
|
| 98 |
.. math:: |
| 99 |
|
| 100 |
T_{add}(n|m \propto n^\frac{1}{3}) = O(n * \log_2{\frac{n}{n^\frac{1}{3}}} + |
| 101 |
n * \log_2{n^\frac{1}{3}} + n * n^\frac{1}{3} + \frac{n}{n^\frac{1}{3}} * |
| 102 |
(n^\frac{1}{3} + \frac{n}{n^\frac{1}{3}})) |
| 103 |
|
| 104 |
T_{add}(n|m \propto n^\frac{1}{3}) = O(n * \log_2{n^\frac{2}{3}} + n * |
| 105 |
\log_2{n^\frac{1}{3}} + n*n^\frac{1}{3} + n^\frac{2}{3}*n^\frac{1}{3}) |
| 106 |
|
| 107 |
T_{add}(n|m \propto n^\frac{1}{3}) = O(n * n^\frac{1}{3}) |
| 108 |
|
| 109 |
Now the amortized cost of adding an individual item is proportional to the cube |
| 110 |
root of `n`. |
| 111 |
|
| 112 |
Alternative tree-based implementations have a runtime complexity proportional |
| 113 |
to :math:`log_2{n}` for adding elements. The logarithm grows much more slowly |
| 114 |
than the cube root for large values of `n`. However, in practice we never reach |
| 115 |
those large values and the constant factors involved have a significant |
| 116 |
impact. Consider a billion elements: |
| 117 |
|
| 118 |
.. math:: |
| 119 |
|
| 120 |
\log_2{1,000,000,000} \approx 33 |
| 121 |
|
| 122 |
(1,000,000,000)^\frac{1}{3} \approx 1,000 |
| 123 |
|
| 124 |
The constant factor between those is :math:`1,000 / 33 \approx 33`. So if the |
| 125 |
operations for tree-based implementations are more than 33 times slower, then |
| 126 |
:doc:`Sorted Containers<index>` may be faster. Below I'll make an argument for |
| 127 |
why that occurs in practice. |
| 128 |
|
| 129 |
Now deleting an element using :func:`SortedList.__delitem__` requires these |
| 130 |
steps: |
| 131 |
|
| 132 |
1. Build the index if not present -- :math:`O(\frac{n}{m})` |
| 133 |
|
| 134 |
2. Traverse the index to resolve the internal location -- |
| 135 |
:math:`O(\log_2{\frac{n}{m}})` |
| 136 |
|
| 137 |
3. Delete the element in the sublist -- :math:`O(m)` |
| 138 |
|
| 139 |
4. Update the index -- :math:`O(\log_2{\frac{n}{m}})` |
| 140 |
|
| 141 |
5. A sublist may be combined with a neighboring sublist if it becomes too |
| 142 |
small. When this happens, the index is deleted -- :math:`O(m+\frac{n}{m})` |
| 143 |
|
| 144 |
Consider how often steps #1 and #5 may occur if we begin with a |
| 145 |
:doc:`sortedlist` of `n` elements and delete all `n` elements. The top-level |
| 146 |
list will shrink from :math:`\frac{n}{m}` to zero meaning that steps #1 and #5 |
| 147 |
were executed :math:`\frac{n}{m}` times. |
| 148 |
|
| 149 |
Altogether, deleting `n` elements from a :doc:`sortedlist` of `n` elements has |
| 150 |
the time complexity: |
| 151 |
|
| 152 |
.. math:: |
| 153 |
|
| 154 |
T_{del}(n, m) = O(\frac{n}{m} * \frac{n}{m} + n * \log_2{\frac{n}{m}} + n * |
| 155 |
m + n * \log_2{\frac{n}{m}} + \frac{n}{m} * (m + \frac{n}{m})) |
| 156 |
|
| 157 |
Most terms are the same for adding and deleting elements. But the first term is |
| 158 |
different. Rebuilding the index takes: |
| 159 |
|
| 160 |
.. math:: |
| 161 |
|
| 162 |
T_{index}(n, m) = O(\frac{n}{m} * \frac{n}{m}) |
| 163 |
|
| 164 |
Furthermore index lookups and updates are proportional to :math:`n * |
| 165 |
\log_2{\frac{n}{m}}`. All these terms are minimized with :math:`n = m`. However |
| 166 |
that maximizes the cost of step (3), :math:`O(n * m)`. |
| 167 |
|
| 168 |
Once again our best bounds will be to use the cube root of `n`: |
| 169 |
|
| 170 |
.. math:: |
| 171 |
|
| 172 |
T_{del}(n|m \propto n^\frac{1}{3}) = O(\frac{n}{n^\frac{1}{3}} * |
| 173 |
\frac{n}{n^\frac{1}{3}} + n * \log_2{\frac{n}{n^\frac{1}{3}}} + n * |
| 174 |
n^\frac{1}{3} + n * \log_2{\frac{n}{n^\frac{1}{3}}} + |
| 175 |
\frac{n}{n^\frac{1}{3}} * (n^\frac{1}{3} + \frac{n}{n^\frac{1}{3}})) |
| 176 |
|
| 177 |
T_{del}(n|m \propto n^\frac{1}{3}) = O(n^\frac{2}{3} * n^\frac{2}{3} + |
| 178 |
n * \log_2{n^\frac{2}{3}} + n * n^\frac{1}{3} + n * \log_2{n^\frac{2}{3}} + |
| 179 |
n^\frac{2}{3} * (n^\frac{1}{3} + n^\frac{2}{3})) |
| 180 |
|
| 181 |
T_{del}(n|m \propto n^\frac{1}{3}) = O(n * n^\frac{1}{3}) |
| 182 |
|
| 183 |
When deleting elements by index, the amortized time complexity is proportional |
| 184 |
to the cube root of `n`. |
| 185 |
|
| 186 |
Although using :math:`m \propto \sqrt[3]{n}` is the best theoretical time |
| 187 |
complexity, index lookups, updates, and building are composed of expensive |
| 188 |
operations. In practice, the square root of `n` works better when doing a lot |
| 189 |
of numerical indexing. |
| 190 |
|
| 191 |
Python Implementations |
| 192 |
---------------------- |
| 193 |
|
| 194 |
I've now said that some operations are more expensive than others while still |
| 195 |
considering each to take :math:`O(1)` time. To understand this, we have to look |
| 196 |
at how Python is implemented. |
| 197 |
|
| 198 |
The most popular implementation of Python is `CPython`_. CPython implements |
| 199 |
lists as arrays of pointers and integers as allocated memory objects. This |
| 200 |
means that shifting elements in lists is very fast. It's akin to a mem-move |
| 201 |
operation for which modern processors are well optimized. The memory access |
| 202 |
pattern is entirely sequential. |
| 203 |
|
| 204 |
In 64-bit builds of CPython, integers require approximately thirty bytes |
| 205 |
each. This severely limits the number of integers we can hold in memory. In |
| 206 |
2016, the largest commercial servers support up to terabytes of memory which |
| 207 |
can hold only hundreds of billions of integers. While large in practice, the |
| 208 |
number is small in theory. Doing integer math in CPython requires a memory |
| 209 |
allocation which, while still :math:`O(1)`, is quite a bit more costly than a |
| 210 |
processor-supported integer. |
| 211 |
|
| 212 |
An optimized implementation of Python is `PyPy`_. PyPy improves on CPython in |
| 213 |
many ways but one of the most important to our discussion is the use of "tagged |
| 214 |
pointers." Tagged pointers are capable of storing integers within the pointer |
| 215 |
itself. This greatly reduces memory consumption so that many integers in PyPy |
| 216 |
take only eight bytes. |
| 217 |
|
| 218 |
Lists of integers in PyPy are therefore packed densely together. When storing |
| 219 |
integers in a sorted list, both the "maxes" index and positional index are |
| 220 |
densely packed lists of integers. This improves locality for various processor |
| 221 |
cache features. |
| 222 |
|
| 223 |
The access pattern of both indexes is also optimized for modern |
| 224 |
processors. Traversing both the "maxes" index and the sublist uses bisect which |
| 225 |
while initially random, narrows locality with each iteration. Likewise the |
| 226 |
positional index is a tree, densely stored in a list. The memory access pattern |
| 227 |
locality is very good initially and then becomes random, the exact opposite of |
| 228 |
bisect. |
| 229 |
|
| 230 |
The benchmarks below use PyPy, without loss of generality, to maximize memory |
| 231 |
utilization and performance. |
| 232 |
|
| 233 |
.. _`CPython`: https://www.python.org/ |
| 234 |
.. _`PyPy`: http://pypy.org/ |
| 235 |
|
| 236 |
Sampling |
| 237 |
-------- |
| 238 |
|
| 239 |
As described in the theory section above, some costs in sorted lists are |
| 240 |
amortized over many operations. Amortized algorithms present unique |
| 241 |
difficulties in measuring performance as, by design, expensive operations are |
| 242 |
avoided. |
| 243 |
|
| 244 |
For example, consider measuring the expected value of the lottery without |
| 245 |
knowing the total jackpot. Purchasing a thousand tickets may still result in no |
| 246 |
winnings which would conclude incorrectly an expected value of zero. |
| 247 |
|
| 248 |
More practically, consider the list data type in CPython. Lists grow and shrink |
| 249 |
as necessary but the underlying implementation is restricted to static |
| 250 |
allocations. For this reason, lists are often over-allocated so that most |
| 251 |
appends may occur immediately. Occasionally, the list must be reallocated and |
| 252 |
possibly copied, which takes linear time. If we sampled performance by |
| 253 |
initializing lists of various sizes and appending an element, we may never |
| 254 |
observe a resize operation and so over-estimate performance. |
| 255 |
|
| 256 |
One solution for both Python lists and :doc:`Sorted Containers<index>` sorted |
| 257 |
lists would be to double the size or remove all elements from the initialized |
| 258 |
list as was done in the Theory section above. Unfortunately, that method is too |
| 259 |
expensive to be practical. Doing so would require weeks and months of time |
| 260 |
incurring hundreds and thousands of dollars in machine costs. |
| 261 |
|
| 262 |
To shorten the measured time, two techniques are used. The first constructs |
| 263 |
sorted lists very quickly by initializing private member variables |
| 264 |
directly. The latter uses sampling in representative scenarios to perform a |
| 265 |
hundredth of the operations needed to double the size or remove all elements. |
| 266 |
|
| 267 |
The problem solved here is similar to that faced by binary tree |
| 268 |
implementations, like red-black trees, which do not maintain a binary tree in |
| 269 |
perfect balance. In fact, the maximum height of a red-black tree is :math:`2 * |
| 270 |
log_2{n + 1}`. While still :math:`O(log_2{n})`, the constant factor can have a |
| 271 |
big impact on performance analysis. When using sampling to measure the |
| 272 |
performance of red-black trees, trees of various shapes and heights must be |
| 273 |
used. The same is done here with :doc:`Sorted Containers<index>`. |
| 274 |
|
| 275 |
Consider a sorted list initialized from an iterable of random values. Those |
| 276 |
values are sorted using the "sorted" builtin function and the resulting list is |
| 277 |
chopped into sublists of the given load. The "maxes" index is simply the last |
| 278 |
element of each sublist. If we plotted sublist length as a histogram, there |
| 279 |
would be one tall bar at the load size. In this scenario, all sublists are the |
| 280 |
same length which works very well in practice but is misleading for sampling |
| 281 |
performance. |
| 282 |
|
| 283 |
To more accurately measure performance, we must consider sublist lengths as |
| 284 |
random values are added individually. The video below displays a histogram of |
| 285 |
sublist lengths as random values are added to a sorted list. The load is one |
| 286 |
thousand. |
| 287 |
|
| 288 |
.. raw:: html |
| 289 |
|
| 290 |
<style> |
| 291 |
.video-wrapper { |
| 292 |
position: relative; |
| 293 |
padding-bottom: 56.25%; /* 16:9 */ |
| 294 |
padding-top: 25px; |
| 295 |
height: 0; |
| 296 |
} |
| 297 |
.video-wrapper iframe { |
| 298 |
position: absolute; |
| 299 |
top: 0; |
| 300 |
left: 0; |
| 301 |
width: 100%; |
| 302 |
height: 100%; |
| 303 |
} |
| 304 |
</style> |
| 305 |
<div class="video-wrapper"> |
| 306 |
<iframe src="https://www.youtube.com/embed/5RR89BVoiSI?rel=0&showinfo=0&fs=0&controls=1" frameborder="0" allowfullscreen=""></iframe> |
| 307 |
</div> |
| 308 |
|
| 309 |
The histogram of sublist lengths is in blue while a normal curve fitted to the |
| 310 |
histogram is plotted in green. The size of the sorted list grows to millions of |
| 311 |
elements. Notice the fit of the normal curve improves with time. The sublist |
| 312 |
lengths grow from one to two thousand elements, at which point sublists are |
| 313 |
split and the process repeats. At the boundaries, bimodal distributions occur |
| 314 |
which may be approximated as a normal distribution that wraps-around at the |
| 315 |
limits. |
| 316 |
|
| 317 |
Consider also a sorted list with a million values each of which is removed at |
| 318 |
random. The video below displays a histogram of sublist lengths as values are |
| 319 |
random deleted from the sorted list. The load is again one thousand. |
| 320 |
|
| 321 |
.. raw:: html |
| 322 |
|
| 323 |
<div class="video-wrapper"> |
| 324 |
<iframe src="https://www.youtube.com/embed/jh4EWHm1_RM?rel=0&showinfo=0&fs=0&controls=1" frameborder="0" allowfullscreen=""></iframe> |
| 325 |
</div> |
| 326 |
|
| 327 |
The sorted list was initialized with a million values. Notice all sublists |
| 328 |
begin with the same length represented by a spike in the histogram. As elements |
| 329 |
are deleted, the spike moves toward five hundred. When sublists become too |
| 330 |
small they are combined with neighboring sublists. Those neighboring sublists |
| 331 |
may be any size between 500 and 2,000. This behavior results in new peaks at |
| 332 |
1,000, 1,500, and 2,000. Each of those peaks then begins traversing left and |
| 333 |
the process repeats. The overall effect is like watching ripples. Over time |
| 334 |
each ripple starts as a sharp-looking normal curve and then flattens out. |
| 335 |
|
| 336 |
In modeling each of the above cases, a normal curve is used to represent the |
| 337 |
sublist lengths. When adding elements, the range of the curve is bounded by |
| 338 |
:math:`load` and :math:`load * 2`. While deleting elements the curve is bounded |
| 339 |
by :math:`\frac{load}{2}` and :math:`load`. The curve wraps-around these |
| 340 |
limits. Normal distributions have two parameters: :math:`\mu` and |
| 341 |
:math:`\sigma` which are the mean and standard deviation. Several means are |
| 342 |
tested to improve sampling, each called a "moment." When adding elements, there |
| 343 |
are ten moments evenly distributed in the range. And when deleting elements, |
| 344 |
there are five moments evenly distributed in the range. The parameter, |
| 345 |
:math:`\sigma`, is given as a tenth of the load, :math:`\frac{load}{10}`. |
| 346 |
|
| 347 |
With this information about the distribution of sublist lengths, we can very |
| 348 |
quickly construct large sorted lists. To do so, we sample lengths from a normal |
| 349 |
distribution and construct the sublists from sequential integers up to the |
| 350 |
desired size. The "maxes" index is simply given as the last element in each |
| 351 |
sublist. Because sequential integers are used, sublists are already sorted. |
| 352 |
|
| 353 |
After constructing a sorted list at each of the moments in the sublist length |
| 354 |
range, operations are performed to total a hundredth of the total size. The |
| 355 |
total time is the sum of the time at each moment. This process is repeated five |
| 356 |
times and the median is selected from the measurements. The median is often |
| 357 |
more accurate than the minimum due to cache effects. Details of the memory |
| 358 |
cache hierarchy are described below. |
| 359 |
|
| 360 |
Benchmarks |
| 361 |
---------- |
| 362 |
|
| 363 |
Two benchmarks are measured. The first is adding random values to a sorted list |
| 364 |
and the second is deleting random indices from a sorted list. When adding |
| 365 |
values, the load is set to the cube root of the list size. And when deleting |
| 366 |
random indices, the load is set to the square root of the list size. |
| 367 |
|
| 368 |
Each table below displays: the method used, the initial size of the sorted |
| 369 |
list, the number of operations performed, the sum of the times at each moment |
| 370 |
as the total time, the operations completed per second, and the ratio of the |
| 371 |
previous Ops/Sec to the current Ops/Sec. |
| 372 |
|
| 373 |
In theory, using a load equal to the cube root of the list size should yield an |
| 374 |
algorithmic time complexity of :math:`n * \sqrt[3]{n}`. With a bit of math, we |
| 375 |
can calculate the expected Ops/Sec ratio as 2.154. Similarly, with a load equal |
| 376 |
to the square root of the list size, the time complexity should be :math:`n * |
| 377 |
\sqrt{n}`. In that scenario, the Ops/Sec ratio should be 3.162. |
| 378 |
|
| 379 |
Tree-based sorted list implementations often advertise :math:`n * \log_2{n}` |
| 380 |
time complexity for which, at extremely large sizes, the Ops/Sec ratio would |
| 381 |
approach one. However, at the sizes discussed below, the ratio is closer to |
| 382 |
1.136. This means that as we grow from one million to one billion elements, we |
| 383 |
expect a net ratio of ~2. By comparison, the cubic root time complexity would |
| 384 |
expect a net ratio of ~10. In practice, :doc:`Sorted Containers<index>` is |
| 385 |
often five to ten times faster at smaller list sizes. So the total effect is |
| 386 |
for performance to be equal at large list sizes. Also, tree-based |
| 387 |
implementations have difficulty trying to realize the theoretical ratio and so |
| 388 |
remain slower even at scale. |
| 389 |
|
| 390 |
Local Results |
| 391 |
............. |
| 392 |
|
| 393 |
Measurements were made locally on a MacBook Pro (Retina, Late 2013) with 2.6 |
| 394 |
GHz Intel Core i7 processor and 16 GB of 1,600 MHz DDR3 memory. Sorted list |
| 395 |
sizes ranged from one million to one billion elements. The benchmark required |
| 396 |
approximately twelve gigabytes of memory. |
| 397 |
|
| 398 |
====== ======= ============ ============== ============= ========= |
| 399 |
Method Size Operations Time Ops/Sec Ratio |
| 400 |
====== ======= ============ ============== ============= ========= |
| 401 |
add 1e+06 1e+04 0.01501 666045.025 nan |
| 402 |
add 1e+07 1e+05 0.26612 375764.681 1.773 |
| 403 |
add 1e+08 1e+06 4.69080 213183.298 1.763 |
| 404 |
add 1e+09 1e+07 83.01831 120455.358 1.770 |
| 405 |
====== ======= ============ ============== ============= ========= |
| 406 |
|
| 407 |
The above table displays the performance of adding elements to a sorted |
| 408 |
list. Notice the particularly good ratio, approximately 1.77, out-performed the |
| 409 |
theoretically expected 2.154. This is mainly due to the different constant |
| 410 |
times required for various operations, of which memory plays a large role and |
| 411 |
is discussed below. |
| 412 |
|
| 413 |
====== ======= ============ ============== ============= ========= |
| 414 |
Method Size Operations Time Ops/Sec Ratio |
| 415 |
====== ======= ============ ============== ============= ========= |
| 416 |
del 1e+06 1e+04 0.00827 1208897.485 nan |
| 417 |
del 1e+07 1e+05 0.13309 751393.836 1.609 |
| 418 |
del 1e+08 1e+06 3.79143 263752.866 2.849 |
| 419 |
del 1e+09 1e+07 124.59184 80262.081 3.286 |
| 420 |
====== ======= ============ ============== ============= ========= |
| 421 |
|
| 422 |
When deleting elements, the ratio starts by out-performing the theoretically |
| 423 |
expected 3.162 but increases with size. The limited processor caches at these |
| 424 |
large sizes play a significant role in the performance. Traversing the |
| 425 |
positional index will evict elements of the top-level list and sublists. |
| 426 |
|
| 427 |
Virtual Machine Results |
| 428 |
....................... |
| 429 |
|
| 430 |
Virtual machine results were made on a Google Compute Engine, Haswell |
| 431 |
generation, 2.3 GHZ Intel Xeon processor with 208 GB of memory. Sorted list |
| 432 |
sizes ranged from one million to ten billion elements. The benchmark required |
| 433 |
approximately 128 gigabytes of memory. |
| 434 |
|
| 435 |
====== ======= ======= ============== ============= ========= |
| 436 |
Method Size Ops Time Ops/Sec Ratio |
| 437 |
====== ======= ======= ============== ============= ========= |
| 438 |
add 1e+06 1e+04 0.02133 468884.826 nan |
| 439 |
add 1e+07 1e+05 0.38629 258872.924 1.811 |
| 440 |
add 1e+08 1e+06 6.20695 161109.825 1.607 |
| 441 |
add 1e+09 1e+07 120.24735 83161.919 1.937 |
| 442 |
add 1e+10 1e+08 2416.60713 41380.330 2.010 |
| 443 |
====== ======= ======= ============== ============= ========= |
| 444 |
|
| 445 |
As with local results, the ratio out-performed the theoretically expected 2.154 |
| 446 |
at small list sizes. At very large sizes processor caches played more |
| 447 |
significant roles and the ratio approached the theoretically expected value. |
| 448 |
|
| 449 |
====== ======= ======= ============== ============= ========= |
| 450 |
Method Size Ops Time Ops/Sec Ratio |
| 451 |
====== ======= ======= ============== ============= ========= |
| 452 |
del 1e+06 1e+04 0.01791 558289.343 nan |
| 453 |
del 1e+07 1e+05 0.26171 382097.449 1.461 |
| 454 |
del 1e+08 1e+06 6.11150 163626.036 2.335 |
| 455 |
del 1e+09 1e+07 171.58899 58278.798 2.808 |
| 456 |
del 1e+10 1e+08 5493.95076 18201.838 3.202 |
| 457 |
====== ======= ======= ============== ============= ========= |
| 458 |
|
| 459 |
The virtual machine results are again similar to local measurements. At smaller |
| 460 |
list sizes the ratio out-performs the expected 3.162 but increases at larger |
| 461 |
sizes. |
| 462 |
|
| 463 |
Total cost of the rented virtual machine was $33.97 for 1,011 minutes of |
| 464 |
use. Anyone interested in funding further scaling to one hundred billion |
| 465 |
elements should contact the :doc:`project lead<development>`. |
| 466 |
|
| 467 |
Memory |
| 468 |
------ |
| 469 |
|
| 470 |
Modern processors use multiple caches to improve memory performance. Caches are |
| 471 |
organized into individual levels: L1, L2, and L3. Each successive level is |
| 472 |
larger and slower than the previous level. For example, the size and average |
| 473 |
latency for random memory accesses on Intel's latest Skylake i7-6700 processor: |
| 474 |
|
| 475 |
* L1 Cache, 64 KB, ~4 cycles |
| 476 |
* L2 Cache, 256 KB, ~12 cycles |
| 477 |
* L3 Cache, 8 MB, ~42 cycles |
| 478 |
* RAM, 16 GB, ~446 cycles |
| 479 |
|
| 480 |
The exact size and latency are not important but the ratios are significant. L2 |
| 481 |
cache is about four times larger and slower than L1 cache. L3 cache is thirty |
| 482 |
times larger and four times slower than L2 cache. And memory is two thousand |
| 483 |
times larger and ten times slower than L3 cache. These ratios are approximate |
| 484 |
but illustrative of the slowdowns. |
| 485 |
|
| 486 |
Also important is the memory access pattern. These advertised latencies are |
| 487 |
averages for random memory access. But there are two other patterns often seen |
| 488 |
in practice: sequential and data-dependent. |
| 489 |
|
| 490 |
Sequential memory access is faster than random due to its predictable |
| 491 |
nature. The speedup varies but about five times faster is a reasonable |
| 492 |
guess. Striding through memory sequentially will incur almost zero cycle |
| 493 |
stalls. |
| 494 |
|
| 495 |
Data-dependent memory access is slower than random because no parallelization |
| 496 |
can occur. Each successive memory access depends on the previous and so stalls |
| 497 |
the processor. The pattern is typical of dereferencing pointers. Again its |
| 498 |
difficult to quantify the slowdown but five times slower is a reasonable |
| 499 |
guess. Altogether, jumping around memory with data-dependent accesses could be |
| 500 |
one thousand times slower than sequential accesses. |
| 501 |
|
| 502 |
Given the size and latencies for memory in modern processors, consider the |
| 503 |
typical cost of adding an element to a sorted list with size one billion. First |
| 504 |
the "maxes" index will be bisected. The index will be one million integers |
| 505 |
densely packed in a list. Using PyPy, the entire index could fit in the L3 |
| 506 |
cache. As the list is bisected, nearby indexes will be pulled into the L2 and |
| 507 |
L1 cache and lookups will accelerate a hundred times. Once the sublist is |
| 508 |
found, it too will be bisected. The sublist will contain only one thousand |
| 509 |
integers and those too will be quickly pulled from memory into L3, L2, and L1 |
| 510 |
caches. Once bisected, the new value will be inserted and memory will be |
| 511 |
traversed sequentially to make space. |
| 512 |
|
| 513 |
For comparison, consider traversing an AVL-binary tree with one billion |
| 514 |
elements. A highly optimized implementation will require at least 24 gigabytes |
| 515 |
of memory. The binary tree will likely traverse thirty levels, each of which is |
| 516 |
a data-dependent lookup. Some lookups will have good locality but most will |
| 517 |
not. Each lookup could be hundreds to thousands of times slower than sequential |
| 518 |
accesses. These slow lookups are why :doc:`Sorted Containers<index>` can afford |
| 519 |
to shift a thousand sequential elements in memory and have most additions take |
| 520 |
less time than binary tree implementations. |
| 521 |
|
| 522 |
Due to the memory cache hierarchy, :doc:`Sorted Containers<index>` scales |
| 523 |
extremely well. Each element in a :doc:`sortedlist` has little overhead which |
| 524 |
increases cache utilization. Data is randomly accessed and related data is |
| 525 |
stored together. These patterns in computing have held for decades which |
| 526 |
promises :doc:`Sorted Containers<index>` a bright future. |