What is a load-in-place asset layout, and how are its internal pointers fixed up after the file is read into memory?
answer
- the file is the memory image
- load address unknown at cook time
- offsets plus a relocation table
- self-relative offsets need no pass
basics
~20 sA load-in-place asset is cooked as a byte image of its runtime structures, so loading is one read into one allocation; internal pointers are stored as offsets and either patched to addresses through a fix-up table or kept as self-relative offsets.
solid answer
~50 sA **load-in-place** layout skips parsing and per-object allocation: the cooker writes structures exactly as the target's compiler lays them out in memory, with the same sizes, alignment, padding, endianness and pointer width, so the loader reads the blob into one aligned allocation and uses it as is. Pointers cannot be written as addresses because the load address is unknown at cook time, so the cooker writes each one as an **offset from the blob's start** and appends a **relocation table** listing where those fields are; after the read, a fix-up pass adds the base address to each. The alternative is **self-relative offsets**, measured from the field itself and resolved on every access: no fix-up pass, and the blob can be moved or memory-mapped. The price: the layout is tied to one platform and code version, so a struct change forces a recook.
code
pseudocode · 22 lines// Cooked file: header, blob bytes, then relocation entries.
// Each pointer field in the blob holds an offset from the blob's start,
// or NULL_OFFSET for a null pointer.
function load_in_place(file):
header = read_header(file)
if header.format_version != EXPECTED_FORMAT_VERSION:
return error("stale cooked data: recook required")
blob = allocate(header.blob_size, header.alignment)
read_into(file, blob, header.blob_size)
for i from 0 to header.fixup_count - 1:
field_offset = read_word(file) // where the pointer field sits
if field_offset > header.blob_size - WORD_SIZE:
return error("corrupt relocation entry")
field_address = blob + field_offset
stored = load_word(field_address) // the cooked offset
if stored == NULL_OFFSET:
store_word(field_address, NULL_POINTER)
else if stored >= header.blob_size:
return error("pointer outside blob")
else:
store_word(field_address, blob + stored)
return blob // root structure starts herego deeper
Recall that some engines cook assets as exact memory images so loading is one read instead of a parse.
Explain why pointers are stored as offsets and what a relocation table lists, and what the cooker must match about the target platform.
Compare fix-up passes with self-relative offsets, including memory mapping and moving blobs, and show how version checks keep stale cooked data from loading.
Decide which data deserves this rigid format and which stays parsed, given how often structures change and what a recook costs the team.
## Why load in place Loading an asset the conventional way means reading a file, parsing its records, allocating an object for each one and copying fields across. For a level with tens of thousands of objects that is many small allocations and a lot of CPU spent before anything appears. A **load-in-place** layout, also called a memory image or a relocatable blob, moves that work into the cook. The cooker writes the asset as the exact bytes the runtime structures will occupy in memory. Loading becomes: 1. read a small header; 2. allocate one block of the size and alignment the header states; 3. read the rest of the file straight into that block; 4. fix up internal pointers, if the format needs it; 5. treat the start of the block as the root structure. There is no parse and no per-object allocation, and the whole asset can be freed with one call. ## What the cooker must match For the bytes to be usable without conversion, the cooker must write them the way the target's compiler would lay out the same structures: - **field sizes and pointer width**, which differ between 32-bit and 64-bit targets; - **alignment and padding**, so each field sits where the runtime code expects it; - **endianness**, so multi-byte values read correctly without swapping; - **plain data only**, because objects carrying hidden pointers, such as a dispatch table for virtual methods, would need those patched as well, so such layouts usually avoid them or rebuild them on load. This is one reason the cook runs per platform. ## Fixing up pointers Inside the blob, structures point to each other: a mesh header points to its vertex array, a level points to its object list. An absolute address cannot be written at cook time because the block's runtime address is unknown. Two schemes solve this. **Offsets plus a relocation table.** The cooker writes each pointer field as an offset from the blob's start and records, in a relocation table, the location of every such field. After reading, the loader walks the table and replaces each stored offset with the base address plus that offset. Afterwards the fields are ordinary pointers and cost nothing extra to follow. The block must then stay where it was loaded, because moving it would invalidate the patched addresses. **Self-relative offsets.** Each pointer field stores the distance from the field's own address to the target. Following it computes the field's address plus the stored offset. There is no fix-up pass and the blob stays valid if it is moved, copied or memory-mapped, at the cost of one addition per dereference and a reserved value, often zero, for null. | | Offsets plus relocation table | Self-relative offsets | |---|---|---| | Work after the read | one pass over the table | none | | Cost per dereference | none | one addition | | Blob can move afterwards | no | yes | | Works with memory mapping | only with a writable copy | yes | | Null representation | a sentinel offset | a reserved value, often zero | ## The price of the format Load-in-place data is fast because it is rigid: - **Code and data are coupled.** Adding a field to a structure changes its size and every offset after it, so all cooked data containing it must be recooked. A **format version** or a hash of the structure definitions in the header lets the loader reject stale data with a clear error instead of reading garbage. - **Platform-specific.** A blob cooked for one target is unusable on another with different pointer width or endianness. - **Hard to patch or edit.** Changing a single value is easy, but inserting data shifts offsets, so tools regenerate the whole blob. - **Trust.** The loader assumes the bytes are well formed. Bounds checks on offsets during fix-up, or a checksum, protect against corrupted files. Teams often combine approaches: large homogeneous data such as vertex and animation arrays is load-in-place, while small, frequently changing gameplay data uses a parsed format that tolerates added fields. Choosing the general serialization format is a separate question; this layout is the engine's tool for the bulk of cooked data.
- Why does the offsets-plus-relocation-table scheme sit badly with memory-mapping a cooked file?The fix-up pass writes real addresses into the blob, which requires writable memory and turns each touched page into a private copy, so a read-only mapping cannot be used. Self-relative offsets need no writes, so a mapped file is usable immediately and its pages stay shared and can be dropped and re-read by the operating system.
- How does a loader detect cooked data made by an older version of the structures?The cooker writes a format version, or a hash of the structure definitions it used, into the header. The runtime compares it with the value compiled into its own code and refuses a mismatch with a clear error that asks for a recook. Without the check, a shifted field silently reads the wrong bytes.
- Cooked pointers can simply store the addresses the cooker's own process used.
- A load-in-place blob can be shared unchanged between platforms with different pointer widths.
- Appending a field to a struct keeps old cooked data valid.
- Self-relative offsets still need a fix-up pass after every load.
- Load-in-place means no version check is needed, since the bytes are already final.