What the libraries do

The head each piece will meet

A rule that priced a sorted file's late tail two ways before moving anything chose the dearer end at a buffer of a 256th, because it priced every piece against the head as it stood before any piece joined it. Each piece also crosses the pieces before it, and those are known in advance: one binary search of each earlier, already sorted piece counts them. Priced against the head each piece will meet, the pieces come within 6% of what they cost at every buffer, and the rule chooses the cheaper end at all seven buffers and all ten reaches of the tail. A third of what each piece crosses turns out to be other pieces, as the arithmetic of the band says it must.

The rotation that could be priced gave a sort a way to decide, before moving anything, how to finish a file that arrives almost sorted. The file is 65,536 keys, sorted except for a last tenth appended in random order from the top fifth of the keys, a run whose shape the input itself declares. Sorting that tail a buffer’s worth at a time leaves sorted pieces, and the sort can send each piece straight into the head or merge the pieces to one group first and cross the head once. The rule priced both ends from measurements of the library merge on synthetic runs: a table of what one sorted run costs to cross a head it interleaves with, by the run’s length, the buffer and the head keys above the run’s smallest, and the measured cost of merging random pieces to one group. The long crossing came within 3% at every buffer, and the rule chose the cheaper end at six of seven buffers and nine of ten reaches of the tail.

Where it failed, it failed the same way. It priced every piece against the head as it stood before any piece joined it, and every piece after the first also crosses the pieces merged before it, so the pieces’ price fell further behind their cost the more pieces there were: 89% of the cost at a 16th, 67% at a 1,024th. At a 256th that sent the sort to the dearer end, and so it did at a 1,024th with a tail drawn from the top tenth of the keys, where the original head has no keys above any piece at all.

Its closing section proposed counting what each piece will really cross. The pieces are sorted before any of them is merged, so the number of keys in an earlier piece above a later piece’s smallest is one binary search of the earlier piece. A piece’s true count is the original head’s keys above its smallest plus the sum of those searches over every piece merged before it. The prediction was that, priced at that count, the pieces would come within 5% of their cost at every buffer, and the rule would choose the cheaper end at all seven buffers and all ten reach settings. It named two risks: the searches, eighty thousand of them at a 4,096th, and the table, which prices one run’s crossing and might not price the many short crossings a sequence of pieces makes.

The prediction holds, nearly to the number, and the more interesting result is how regular the thing it repairs turns out to be.

The head a piece will meet

The file, the buffers, the two ends and the price tables are the earlier essay’s, and every move is the library merge’s own count. The rule changes in one place. For the ii-th piece in the order the pieces go into the head, it reads by binary search how many of the original head’s keys lie above the piece’s smallest, as before, and then, for each of the i−1i - 1 pieces before it, how many of that piece’s keys lie above the same smallest; the sum is the piece’s crossing count, and the crossing table prices the piece at it. Merging first is priced exactly as before. The rule takes the cheaper end, and the end it chose is compared with the ends’ measured costs.

The table is still read for a head the length of the whole file, although the head a piece meets grows by a buffer with every piece merged. That is a simplification of the earlier essay’s that this one keeps.

Within six per cent at every buffer

Priced against the head each piece will meet, the pieces' crossings come to 94%–104% of what they cost from the crossing table, where the head as it stood gave 63%–89%; counted as shifts at that head, each piece's keys copied into the buffer and back, they are exact to the move at every bufferAt each buffer, the price of the pieces' crossings over what they cost when sent straight in: each piece priced at the head keys above its smallest before any piece is merged (dashed), and at those plus the keys of every earlier piece above its smallest (solid). 1/16: 0.462 paid, 0.411 and 0.472 priced from the table, 89% and 102%; 0.462 as shifts at the met count with each piece copied into the buffer, 100.0%; 1/32: 0.787 paid, 0.614 and 0.804 priced from the table, 78% and 102%; 0.787 as shifts at the met count with each piece copied into the buffer, 100.0%; 1/64: 1.227 paid, 0.928 and 1.264 priced from the table, 76% and 103%; 1.227 as shifts at the met count with each piece copied into the buffer, 100.0%; 1/128: 2.105 paid, 1.553 and 2.178 priced from the table, 74% and 103%; 2.105 as shifts at the met count with each piece copied into the buffer, 100.0%; 1/256: 4.051 paid, 2.912 and 4.212 priced from the table, 72% and 104%; 4.051 as shifts at the met count with each piece copied into the buffer, 100.0%; 1/1024: 15.385 paid, 10.342 and 15.441 priced from the table, 67% and 100%; 15.385 as shifts at the met count with each piece copied into the buffer, 100.0%; 1/4096: 58.088 paid, 36.386 and 54.542 priced from the table, 63% and 94%; 58.088 as shifts at the met count with each piece copied into the buffer, 100.0%.0.5000.7501buffer, as a share of the filethe pieces' crossings: priced over paid1/161/321/641/1281/2561/10241/4096table, the head each meetsshifts, the head each meetstable, the head as it stoodpieces straight into the headone: priced exactly
Fig. 1 The pieces’ crossings, priced over what they cost, at each buffer: priced against the head each piece will meet (solid), and against the head as it stood (dashed). Met: 102% at a 16th, 102% at a 32nd, 103% at a 64th, 103% at a 128th, 104% at a 256th, 100% at a 1,024th, 94% at a 4,096th. As it stood: 89%, 78%, 76%, 74%, 72%, 67%, 63%.

Priced against the head each piece will meet, the pieces’ crossings come to between 94% and 104% of what they cost at every buffer, where the head as it stood priced them at 63% to 89%. The error that grew with the number of pieces is gone. At a 256th the price is 4% high, at a 1,024th it is within half a per cent, and only at a 4,096th, with 410 pieces of sixteen keys each, is it 6% low. The prediction’s 5% holds at six buffers of seven and misses by one point at the last.

What changed is only the count. The crossing table was right about what a crossing costs given how many head keys it passes; the earlier rule had asked it the wrong question, a count taken before the head changed. With the right count the same table, measured on single runs crossing a static head, prices a sequence of a hundred or four hundred merges into a head that each one enlarges, to within a few per cent. The second risk the closing section named, that many short crossings might not behave like the single crossing the table measured, does not appear until the pieces are sixteen keys long, and then as a 6% shortfall.

Exact, without the table

The table is not needed for the pieces at all, and the reason is in what a piece is. A piece is a buffer’s worth of keys, so it always fits the buffer, and a merge whose shorter side fits the buffer does not rotate, which is the guarantee the library makes only when its allocation succeeds: the library merge copies the piece into the buffer, then walks the head from the piece’s smallest key upward, moving each head key it passes once and each piece key back once. Its cost is the head keys above the piece’s smallest plus twice the piece. The earlier rule’s shift price had two mistakes in it, the count of head keys, which ignored the pieces before, and the piece’s keys, counted once instead of twice.

With both mended, the shift price of the pieces sent straight in is the moves the sort makes, exactly, at every buffer: 30,306 moves at a 16th, 137,923 at a 128th, 1,008,296 at a 1,024th, 3,806,847 at a 4,096th, priced and paid. The one exception the counting must allow is a key of a piece that lies above everything merged before it, which the merge’s trim leaves where it is; on this file that happens to no key at any buffer, and on a file of 16,384 keys of the same shape it happens to one, and the count that leaves such keys out is exact there too. The crossing table, measured on single runs, gets within 4% of these at six buffers and 6% at the seventh, so its errors were the table’s own interpolation, not a property of merging pieces in sequence. The second risk the closing section named, that many short crossings might not behave like one long one, is answered more sharply than it asked: a short crossing is a sequence of shifts, and shifts are counted exactly.

The rotation, then, is the only part of either end that needs measuring; everything else is moves that can be counted in advance, as the least any sort must move can be. The long crossing of the tail merged first does not fit the buffer and rotates, and only a measured table prices it; the rounds before it rotate too, wherever a group outgrows the buffer. Everything that fits the buffer can be priced by counting, and the counting is what the earlier rule got wrong.

The cheaper end, every time

With every piece priced against the head it will meet, the rule chooses the cheaper end at all 7 buffers: at a 256th it now prices sending the pieces in at 5.16 moves a key against their 5.00, above the 4.38 it prices merging first at, and merges them first, as it shouldMoves a key of the file at each buffer, for each end paid (solid) and priced in advance (dashed), every piece priced at the original head's keys above its smallest plus every earlier piece's keys above it. 1/16: straight in paid 1.98, priced 1.99; merged first paid 2.10, priced 2.10; chose straight in, the cheaper. 1/32: straight in paid 2.16, priced 2.18; merged first paid 2.47, priced 2.47; chose straight in, the cheaper. 1/64: straight in paid 2.46, priced 2.50; merged first paid 2.96, priced 2.96; chose straight in, the cheaper. 1/128: straight in paid 3.20, priced 3.27; merged first paid 3.55, priced 3.60; chose straight in, the cheaper. 1/256: straight in paid 5.00, priced 5.16; merged first paid 4.35, priced 4.38; chose merged first, the cheaper. 1/1024: straight in paid 16.04, priced 16.09; merged first paid 6.46, priced 6.46; chose merged first, the cheaper. 1/4096: straight in paid 58.46, priced 54.91; merged first paid 8.87, priced 8.85; chose merged first, the cheaper.buffer, as a share of the filemoves a key of the file (log scale)12481632641/161/321/641/1281/2561/10241/4096pieces straight in, paidpieces straight in, pricedmerged to one first, paidmerged to one first, priced65,536 keys, a late tenth from the top fifthdashed: priced in advance
Fig. 2 Moves a key of the file at each buffer, for each end paid (solid) and priced in advance (dashed). Sent straight in: 1.98, 2.46, 3.20, 5.00 and 16.0 paid at a 16th, 64th, 128th, 256th and 1,024th; priced 1.99, 2.50, 3.27, 5.16 and 16.1. Merged first: 2.10, 2.96, 3.55, 4.35 and 6.46 paid; priced 2.10, 2.96, 3.60, 4.38 and 6.46.

With every piece priced against the head it will meet, the rule chooses the cheaper end at all seven buffers. At a 256th, where the earlier rule sent the pieces in at 5.00 moves a key, it now prices sending them in at 5.16, above the 4.38 it prices merging first at, and merges them first, which costs 4.35. At every other buffer it makes the choice the earlier rule made, now for the right reason: at a 128th, where the two ends differ by a third of a move a key, the pieces are priced at 3.27 against their cost of 3.20 and merging first at 3.60 against 3.55.

The two price curves now lie along the two cost curves at the scale of the plate, and cross where the cost curves cross, between a 128th and a 256th. That crossing is what the earlier essay’s question was about, the boundary between the ends, and it can now be found from prices alone. A sort that knows its buffer and can binary-search its pieces knows, before it moves a key of the tail, which way to finish.

A third of every crossing is other pieces

Of the keys the pieces cross, 24% at a 16th and 32% to 33% at every smaller buffer are keys of the pieces merged before them — the third the band's arithmetic gives when the head and the tail each hold half of it; counting them costs one binary search of every earlier piece, from one search at a 16th to 83,845 at a 4,096thAt each buffer, of all the keys the pieces sent straight in cross, the share that belong to earlier pieces rather than to the original head (bar), and the binary searches of earlier pieces that count them before anything is merged. 1/16: 24%, 1 searches; 1/32: 32%, 6 searches; 1/64: 32%, 21 searches; 1/128: 32%, 78 searches; 1/256: 33%, 325 searches; 1/1024: 33%, 5,253 searches; 1/4096: 33%, 83,845 searches.0%25%50%a third1/161 searches1/326 searches1/6421 searches1/12878 searches1/256325 searches1/10245,253 searches1/409683,845 searchesshare of crossed keys from earlier pieces (scale to one half)a late tenth from the top fifthpieces straight in
Fig. 3 Of the keys the pieces sent straight in cross, the share that belong to earlier pieces rather than to the original head: 24% at a 16th, and 32% to 33% at every smaller buffer. Counting them costs one binary search of every earlier piece: one at a 16th, 78 at a 128th, 5,253 at a 1,024th, 83,845 at a 4,096th.

At every buffer from a 32nd down, 32% to 33% of the keys the pieces cross belong to pieces merged before them; at a 16th, with two pieces, 24%. The share is nearly constant because it is arithmetic. The tail is drawn from the top fifth of the keys, 13,107 of them, and takes half, so the head holds the other half of the band, about 6,553 keys, as many as the tail. A piece whose smallest key lies near the bottom of the band crosses nearly all the head’s band, and of the pieces before it, the ii-th piece crosses about i/pi/p of the tail. Averaged over pp pieces, the earlier pieces contribute about half the tail’s keys to each crossing and the head the whole of its band, so the pieces’ share approaches a half over one and a half, a third. With two pieces it is one in five by the same reckoning; the 24% measured is close.

That regularity explains the earlier rule’s failure pattern exactly. Leaving out a third of every crossing after the first should leave the price at about two thirds of the cost once there are many pieces, and the earlier rule’s ratio fell from 89% with two pieces towards 63% with 410. The two numbers are the same fact.

The searches that count the pieces’ keys grow as the square of the number of pieces: one at a 16th, 78 at a 128th, 83,845 at a 4,096th. Each is a binary search of a sorted piece of bb keys, about log⁡2b\log_2 b comparisons, the search galloping makes when it pays, so at a 4,096th they cost about 335,000 comparisons, against 3.8 million moves for the sort. Where the choice is close, at a 128th and a 256th, they cost a few hundred searches; where they are many, at the smallest buffers, the choice was never close, and a rule could skip them by noticing that the head-only price already exceeds merging first.

As the tail reaches further

As the late tenth reaches further into the keys, the rule that prices each piece against the head it will meet chooses the cheaper end at 10 of 10 settings, where pricing against the head as it stood chose it at 9: at a 1,024th with the tail from the top tenth, it prices the pieces at 5.95 moves a key against their 5.90At buffers of a 1,024th and a 128th and reaches of 0.1, 0.2, 0.4, 0.7, 1: the moves a key of each end, and the end chosen by the rule pricing pieces against the head they will meet and by the rule pricing them against the head as it stood. 1/1024, reach 0.1: straight in 5.90 (priced 5.95), merged first 4.33; met chose merged first (right), as it stood straight in (wrong); 1/1024, reach 0.2: straight in 16.04 (priced 16.09), merged first 6.46; met chose merged first (right), as it stood merged first (right); 1/1024, reach 0.4: straight in 36.34 (priced 36.61), merged first 8.58; met chose merged first (right), as it stood merged first (right); 1/1024, reach 0.7: straight in 66.64 (priced 67.21), merged first 11.77; met chose merged first (right), as it stood merged first (right); 1/1024, reach 1: straight in 97.19 (priced 98.08), merged first 14.95; met chose merged first (right), as it stood merged first (right); 1/128, reach 0.1: straight in 1.89 (priced 1.92), merged first 2.33; met chose straight in (right), as it stood straight in (right); 1/128, reach 0.2: straight in 3.20 (priced 3.27), merged first 3.55; met chose straight in (right), as it stood straight in (right); 1/128, reach 0.4: straight in 5.79 (priced 6.02), merged first 4.82; met chose merged first (right), as it stood merged first (right); 1/128, reach 0.7: straight in 9.69 (priced 9.70), merged first 6.62; met chose merged first (right), as it stood merged first (right); 1/128, reach 1: straight in 13.58 (priced 13.64), merged first 8.42; met chose merged first (right), as it stood merged first (right).reach 0.1reach 0.2reach 0.4reach 0.7reach 1each cell: the end chosen, filled when it was the cheaper1/1024, head it meetsmergedmergedmergedmergedmerged1/1024, head as it stoodstraightmergedmergedmergedmerged1/128, head it meetsstraightstraightmergedmergedmerged1/128, head as it stoodstraightstraightmergedmergedmerged65,536 keys, a late tenthunfilled: the dearer end
Fig. 4 At a 1,024th and a 128th, the tail drawn from the top tenth of the keys up to all of them: the end chosen by pricing against the head each piece will meet and against the head as it stood, filled where it was the cheaper. Met: the cheaper end at all ten settings. As it stood: nine, missing at a 1,024th with the tail from the top tenth.

As the late tenth is drawn from the top tenth of the keys up to all of them, the rule that prices each piece against the head it will meet chooses the cheaper end at all ten settings. The setting the earlier rule missed is the one where the original head holds no keys in the tail’s band, so that every key a piece crosses is another piece’s. Priced against the original head, the pieces looked almost free; priced against the head they will meet, they cost 5.95 moves a key by the rule’s estimate and 5.90 in fact, and the rule merges them first, at 4.33.

At the other extreme, the tail from the whole range of keys, the pieces’ crossings are almost all of original head keys, since the head holds nine tenths of every part of the range, and the two prices nearly agree: 98.1 moves a key priced against 97.2 paid at a 1,024th. The share that earlier pieces contribute falls as the head’s share of the band rises, which is the same arithmetic as before with the head’s band larger than the tail.

What the prediction got right

The closing section predicted that each piece, priced at its true count of keys above its smallest, would come within 5% of its cost at every buffer, and that the rule would then choose the cheaper end at all seven buffers and all ten reach settings. The pieces come within 4% at six buffers and 6% at the seventh, and the rule chooses the cheaper end everywhere. It named two risks: the searches, about eighty thousand at a 4,096th, of which there are 83,845; and the table, which prices one run’s crossing and might not price many short ones, which it does to within 6% even at pieces of sixteen keys, and which the pieces turn out not to need, since a piece that fits the buffer costs exactly the shifts it makes once they are counted against the right head.

The question it asked was whether a sequence of merges can be priced as a sum of single merges once each is given the head it will really meet. On this input it can: a table measured once per buffer on single crossings of a static head, read at counts that one binary search per pair of pieces supplies, prices a hundred merges into a growing head to within a few per cent and makes every choice correctly. The earlier essay’s failure was a failure to count, and the counting is cheap where it matters.

What the price leaves out

A head of fixed length in the table. The table is read for a head the length of the whole file, though the head a piece meets is shorter by the pieces still to come. At small buffers the shortfall at a 4,096th may be partly this; the table was not re-measured for each head length.

Pieces in their arrival order. The pieces go into the head in the order they were cut from the tail. Another order would change every piece’s count and the total, and nothing here asks which order is cheapest.

One file shape. A late tenth drawn from the top fifth of the keys, with the reach varied on two buffers. The third of every crossing that belongs to other pieces is a consequence of the head and the tail holding equal halves of the band; a tail from the top tenth puts the whole of every crossing after the first in other pieces, and the rule still prices it, because it counts rather than assumes.

Two ends only. As in the earlier essay, the rule chooses between sending every piece in and merging them all first.

Still open: every number of rounds, priced

The rule now prices both ends of the choice correctly, and the earlier essays left the middle untested by prices: merging the pieces in rounds, pairs into fours, fours into eights, for some number of rounds short of all of them, and sending the groups that result into the head. How many pieces the head can afford found, by trying every number of rounds, that stopping partway was never the cheapest, and its own rule for choosing rounds, priced as shifts, was wrong in both directions.

The measurement that follows prices every number of rounds from the same two tables, the rounds’ cost from a measured merge of random pieces to that many rounds and each group’s crossing at the count of keys it will meet, and lets the rule choose the cheapest number, then compares it with the cost of every number tried. The prediction is that the priced rule never chooses a partway number at any buffer, matching the earlier essay’s finding that the ends are always cheaper, and that its estimate of every partway number lies within 6% of its cost. It could fail at the buffers near the crossing, a 128th and a 256th, where one round costs the pieces less than it saves the head on some inputs, and where the earlier essay’s single input may have hidden a partway optimum that a different seed of the tail would show. The question is whether the boundary between the ends is a sharp one, or a narrow band of buffers where merging a little is cheapest and no fixed rule of all or nothing can be right.

Named alongside this one

Essays reaching for the same objects. Nobody chose these; they are what the concept index makes visible.

The objects this essay names

Each one links to every other essay that touches it.

Auxiliary spaceBinary searchData movementMerge sortPredictionRotationStable sort