@@ -167,6 +167,34 @@ Comparison with Python's Samplesort Hybrid
167167 But timsort "should be" slower than samplesort on ~sort, so it's hard
168168 to count that it isn't on some boxes as a strike against it <wink>.
169169
170+ + Here's the highwater mark for the number of heap-based temp slots (4
171+ bytes each on this box) needed by each test, again with arguments
172+ "15 20 1":
173+
174+
175+ 2**i *sort \sort /sort 3sort +sort %sort ~sort =sort !sort
176+ 32768 16384 0 0 6256 0 10821 12288 0 16383
177+ 65536 32766 0 0 21652 0 31276 24576 0 32767
178+ 131072 65534 0 0 17258 0 58112 49152 0 65535
179+ 262144 131072 0 0 35660 0 123561 98304 0 131071
180+ 524288 262142 0 0 31302 0 212057 196608 0 262143
181+ 1048576 524286 0 0 312438 0 484942 393216 0 524287
182+
183+ Discussion: The tests that end up doing (close to) perfectly balanced
184+ merges (*sort, !sort) need all N//2 temp slots (or almost all). ~sort
185+ also ends up doing balanced merges, but systematically benefits a lot from
186+ the preliminary pre-merge searches described under "Merge Memory" later.
187+ %sort approaches having a balanced merge at the end because the random
188+ selection of elements to replace is expected to produce an out-of-order
189+ element near the midpoint. \sort, /sort, =sort are the trivial one-run
190+ cases, needing no merging at all. +sort ends up having one very long run
191+ and one very short, and so gets all the temp space it needs from the small
192+ temparray member of the MergeState struct (note that the same would be
193+ true if the new random elements were prefixed to the sorted list instead,
194+ but not if they appeared "in the middle"). 3sort approaches N//3 temp
195+ slots twice, but the run lengths that remain after 3 random exchanges
196+ clearly has very high variance.
197+
170198
171199A detailed description of timsort follows.
172200
@@ -460,13 +488,13 @@ Galloping with a Broken Leg
460488---------------------------
461489So why don't we always gallop? Because it can lose, on two counts:
462490
463- 1. While we're willing to endure small per-run overheads, per-comparison
491+ 1. While we're willing to endure small per-merge overheads, per-comparison
464492 overheads are a different story. Calling Yet Another Function per
465493 comparison is expensive, and gallop_left() and gallop_right() are
466494 too long-winded for sane inlining.
467495
468- 2. Ignoring function-call overhead, galloping can-- alas --require more
469- comparisons than linear one-at-time search, depending on the data.
496+ 2. Galloping can-- alas --require more comparisons than linear one-at-time
497+ search, depending on the data.
470498
471499#2 requires details. If A[0] belongs before B[0], galloping requires 1
472500compare to determine that, same as linear search, except it costs more
0 commit comments