Alle boeken

Professioneel

Apps Over Coach Inloggen Begin met lezen

Quantitative Finance · Begrippenlijst

Wat is Structure padding?

Definition 6.1 Low-Latency Software · Hoofdstuk 6 — C++ for Latency I: Memory

Every scalar type has an alignment, equal to its size on x86-64 (a double starts at a multiple of 8). The compiler lays out a structure’s fields in declaration order and inserts structure padding, unused bytes, before any field that would otherwise be misaligned and at the end, so that the structure’s size is a multiple of its largest alignment.

The order of  laid out byte by byte: the first draft carries 17 bytes of padding (grey) in 40; the same fields sorted by size need 24, with the side, flag and time in force in the three bytes after the quantity.
Figure 6.1. The order of Example 6.2 laid out byte by byte: the first draft carries 17 bytes of padding (grey) in 40; the same fields sorted by size need 24, with the side, flag and time in force in the three bytes after the quantity.

Voorbeelden

Example 6.2 (Ordering fields)

An order written as a first draft declares a char side, a double price, a char flag, a 64-bit id, a 32-bit quantity and a char time in force: 23 bytes of data in 40 bytes, 17 of them padding. Ordered by size (id, price, quantity, then the three characters) the same fields take 24 bytes: eight orders occupy three 64-byte cache lines instead of five. The compiler will not reorder fields itself: the order is part of the type’s interface.

Lees in het hoofdstuk →