Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Reading plugins with queries

Every command that reads plugins declares what it needs as a PluginQuery: which record types, which of them to parse, and what to keep. The query runs in one walk per plugin. The layout it walks is in Plugin file format.

Code: CreationEngineFormats/Plugin/PluginFile.cs, PluginWalker.cs, RecordTypeSet.cs and Plugin/Query/.

Where the time goes

Measured on Skyrim.esm (238 MiB, about 750,000 records in the world and cell groups):

StepCost
Reading a record headerAlmost free: walking every header of the file takes ~30 ms
Reading and decompressing a record’s dataExpensive: zlib is the largest CPU cost of a worldspace read
Keeping a parsed record aliveThe most expensive: 700,000 parsed REFRs keep the GC busy for the rest of the run

A query makes the last two visible: a record is only parsed if a clause parses it, and only kept if a clause keeps it.

An example

var query = new PluginQuery();
var worldspaces = query.Records(Tags.WRLD).Parse(WorldspaceReader.Parse);
var cells = query.Records(Tags.CELL).Parse(CellReader.Parse);
var landscapeCells = query.Records(Tags.LAND).Distinct((in WalkedRecord r) => r.Context);

using (var file = PluginFile.Open(pluginFile))
    query.Run(file);

// worldspaces.Items, cells.Items, landscapeCells.Items

landscapeCells never reads a LAND’s data: the clause only uses the header and the location. The walker also skips every group this query doesn’t need (every REFR group, every top group other than WRLD and CELL) with a single jump over the group.

Clauses

query.Records(types) matches records of these types wherever they sit in the GRUP tree. query.TopGroupRecords() matches every record directly inside a top group other than WRLD and CELL, whatever its type. It is what a FormId resolver uses, since a REFR’s base object can be any placeable type and the code keeps no list of them.

FilterEffect
.Where(record => ...)Keeps the records the filter accepts. It sees the header and location only, so a rejected record’s data is never read
.IncludeDeleted()Deleted records are dropped by default. A merge that must see a deleted override asks for them
TerminalReturnsReads the data
.Headers(record => value)Collected<T>, one value per recordNo
.Distinct(record => value)CollectedSet<T>, each value onceNo
.Parse(Reader.Parse)Collected<Record<T>> through an existing readerYes
.Select((record, raw, isLocalized) => value)Collected<T>, for anything a reader alone doesn’t coverYes

Parse and Select take parallel: true: the records are noted during the walk and parsed on every core once it is done. Results keep the file order. Use it for record types with many records, like REFR.

A result read before Run throws. An empty result therefore always means “no such record in the plugin”, never “not asked” or “not run yet”.

How a dataset declares its clauses

The exporter keeps the clauses of one model in a query class next to it: BookQuery, LocationQuery, WorldspaceQuery, WorldspaceMapQuery, VmadQuery. A dataset attaches that class to the plugin’s query and returns its result:

public sealed record BookRecords : PluginDataset<PluginBooks, BookSet>
{
    public override Func<PluginBooks> Attach(PluginQuery query) => new BookQuery(query).Result;
    // Merge, Report
}

The session builds one PluginQuery per plugin and hands it to every dataset loaded together, so export:books reads each plugin once for its books and its locations. A lookup (BaseObjects, NamedRecords) also gets the plugin’s masters: its filter resolves FormIds during the walk, so it only keeps the wanted records. See Architecture.

To read something new, add a clause to the query class that owns the model, or write a new query class and a dataset for it in the domain’s folder of Datasets/. When a query reads some parts only on demand, like WorldspaceMapReads does for the map’s playable cells and heights, a part not asked for is null in the model, never an empty list: a caller can’t mistake “not read” for “none in the plugin”.

The file

PluginFile.Open maps the plugin into memory. The walker and the readers see it as one ReadOnlySpan<byte>: a record’s data is a slice of the file, never a copy, and pages are only read from disk when a byte of them is used. PluginFile.FromBytes wraps a byte array for tests.

A span is indexed by an int, so a plugin can’t exceed 2 GiB. The largest vanilla master, Skyrim.esm, is 238 MiB.