The same conversion to a plain positional rectangle cost nothing on one table and doubled peak memory on another — why?
answer
- window, or fresh allocation
- depends on how it was already held
- one slab, one representation, no copy
- separate runs and chunks must be copied
- source and result both resident during the copy
basics
~20 sHow the table was already held decides it. Where every field shares one representation in a single contiguous slab, the conversion can hand back a window over existing bytes; otherwise it allocates a new rectangle and copies every value.
solid answer
~50 sThree storage designs are in play. Where a table packs every field sharing a representation into one in-memory slab, and the whole table happens to be one representation, a rectangle already exists — the conversion can return a window over those bytes and allocate nothing. Where each field is its own contiguous run, or a sequence of separately allocated chunks, no such rectangle exists: the conversion allocates one and copies every value into it. During that copy the source table and the new rectangle are both resident, which is where the peak doubles. A single text field makes it worse, because the one representation covering the result then has to be one that can hold text. So neither "the conversion is cheap" nor "the conversion always copies" is the answer: it is a window only when the table is already one representation in one slab.
go deeper
Recall that the conversion sometimes has to build a whole new rectangle and fill it. When it does, the original and the new one are both in memory at the same time, which is why memory can spike at that line.
Explain which storage designs allow a window and which force a copy: one run per field and a sequence of chunks both force one, a single slab of a single representation does not. Tie the peak to both holdings being resident.
Demonstrate you would predict this rather than discover it. Check whether the fields share a representation, narrow the selection so the allocation is bounded by what you need, and account for anything still referencing the source afterwards.
The tradeoff to own is that the same line of code has two cost profiles and the source does not reveal which. A codebase that cares about peak memory needs the conversion to be a place with a known answer, not a line anyone may write.
## "Is the conversion expensive?" has no single answer The conversion from a labelled table to a plain positional rectangle is one line of code with two completely different cost profiles behind it. In one case it hands back a **window over bytes that already exist** and allocates nothing at all. In the other it **allocates a new rectangle and copies every value into it**, and while that copy runs the source table and the destination rectangle are both fully resident. The difference is not how big the data is. It is how the table was already holding its fields. ## Three storage designs, three outcomes | how the table holds a field | can the conversion return a window? | why | | --- | --- | --- | | one contiguous run per field | no | there are as many runs as fields; a rectangle is one buffer, so one has to be allocated and filled | | an ordered sequence of separately allocated chunks | no | there is no single buffer to point at, even for one field | | one in-memory slab per representation | only when the whole table is one representation | that slab already *is* a rectangle of that representation; anything else means more than one slab | The third row is where the belief "the conversion is free, it just hands you the buffer underneath" comes from. It is a true statement about a table that happens to be entirely one representation held in one slab, and a false statement about every other table — including the same table after somebody adds a field of a different representation, which can split it into two slabs and turn yesterday's free conversion into today's full copy. ## Why the copy shows up as a doubled peak During a copying conversion there is a moment when both holdings are alive at once: 1. The destination rectangle is allocated at full size, before a single value has moved into it. 2. Values are copied in, while the source is still needed to read from. 3. Only after the copy completes can the source be released — and only if nothing else still refers to it. So the peak is roughly **the source plus the result**, not the larger of the two. That is also why the doubling is invisible in an average and obvious in a peak: by the time anyone measures afterwards, one of the two holdings is gone. Two things make it worse than a straight doubling: - **A text field in the table.** The one representation covering the whole result then has to be one that can hold text, so the destination is not the same size as the source's values — it is larger, sometimes considerably. - **Something still holding the source.** If the table is referenced elsewhere, step 3 never happens and the peak becomes the new steady level. ## What a window changes besides cost A window and a freshly allocated rectangle differ in one more way that matters at a boundary: a window shares storage with the table it came from, so the two are not independent, while a copy leaves two unrelated holdings. Code that is correct against a copying conversion and subtly wrong against a window-returning one is a real hazard, and it is one you cannot see in the source line, because the source line reads identically in both cases. What does **not** differ is what crosses the boundary. The field names and any per-row key the table carried are absent from the rectangle either way. Whether bytes were copied is a cost question; what the rectangle knows about your data is the same answer in both. ## Predicting it before you run it - Ask whether every field shares one representation. If not, assume a copy. - Ask how the table was assembled. Built up by adding fields one at a time tends to leave more separate runs or more slabs behind than read in one shot. - Ask how much of the table you actually need in the rectangle. Converting a selection of fields, or a slice of rows, bounds the allocation by what you asked for rather than by the whole table. The conversion still copies; the copy is just smaller. - Ask what is still holding a reference to the source once the rectangle exists. ## The claim to avoid making Both universal statements are wrong. "The conversion is cheap, it just exposes the buffer underneath" is one design's behaviour generalised to the class. "The conversion always allocates and copies" is the opposite over-correction, and it will make you avoid a conversion that costs nothing. The accurate sentence carries its own precondition: **the conversion is a window only when the table is already one representation in one slab; otherwise it allocates and copies, and a single text field sets the representation for everything that lands in the result.** A candidate who states it that way has told you they have met more than one of these designs, which is the signal the question is really testing for.
- What would make an expensive conversion cheap without changing the data itself?Narrow what crosses. Convert only the fields that already share a representation, and only the rows the receiving code needs. The conversion still copies, but the allocation is bounded by what you asked for rather than by the whole table, so the peak stops tracking the table's size.
- Does the row identity survive the conversion when it returns a window rather than a copy?No. A window is over the values only. The field names and any per-row key the table carried are absent from the rectangle in both cases. Whether bytes were copied is purely a cost question; what crosses the boundary is identical either way.
saying these in an interview costs you the question
- Says the conversion is always cheap because it just exposes the buffer.
- Says the conversion always copies, whatever the table's storage design.
- Ignores that source and result are both resident during the copy.
- Assumes a table of mixed representations can still yield a window.
- Blames the difference on table size rather than on how it was held.