
In this blog post I give a handful of miniatures for validating CSV that illustrate common approaches. This post can be thought of as an extension of Matcher Redux Part 1: Exact Matching I.
I always meant to write more about the topic, but I never found the time nor the motivation. I recently had to implement a validator for CSV, so I thought that documenting my thought process would be a reasonable extension.
I recommend giving the previous post from December 2025 a skim first (holy shit, it's been almost a year!). We will continue using GNU C99 with appropriate headers #include'd throughout.
RFC 4180 specifies the CSV file format with a small ABNF grammar. The language it describes is regular: a simple state machine with 6 states (one of them a dead state) and transitions between them is enough to recognise it. The grammar in full:
file = [header CRLF] record *(CRLF record) [CRLF]
header = name *(COMMA name)
record = field *(COMMA field)
name = field
field = (escaped / non-escaped)
escaped = DQUOTE *(TEXTDATA / COMMA / CR / LF / 2DQUOTE) DQUOTE
non-escaped = *TEXTDATA
COMMA = %x2C
CR = %x0D ; as per section 6.1 of RFC 2234 [2]
DQUOTE = %x22 ; as per section 6.1 of RFC 2234 [2]
LF = %x0A ; as per section 6.1 of RFC 2234 [2]
CRLF = CR LF ; as per section 6.1 of RFC 2234 [2]
TEXTDATA = %x20-21 / %x23-2B / %x2D-7E
Since name = field, the header is syntactically just another record, and the optional trailing CRLF is already covered by an empty last record. Note that this is strict: printable ASCII only, and CRLF line endings only.
Naively ⎇
We can naively validate the CSV format as follows, for a modest and readable implementation:
int csv_scalar(const uint8_t * p, size_t n) {
unsigned prev = ',', quoted = 0;
while (n--) {
unsigned c = *p++;
if (c >= 127 || (c < 32 && c != '\r' && c != '\n'))
return 0;
if (!quoted) {
if ((prev == '\r') != (c == '\n'))
return 0;
if (c == '"' && prev != '"' && prev != ',' && prev != '\n')
return 0;
if (prev == '"' && c != '"' && c != ',' && c != '\r')
return 0;
}
quoted ^= c == '"'; prev = c;
}
return !quoted && prev != '\r';
}
The first improvement is to use our knowledge of the fact that branchy code is bad for the CPUs, and thus applying some code golf/branchless programming tricks: compute every condition as a 0/1 flag and fold all failures into one error word instead of returning early:
int csv_branchless(const uint8_t * p, size_t n) {
unsigned in = 0, ok = 1, q = 0, r = 0, err = 0, b, Q, C, L, R;
for (; n; ++p, --n) {
b = *p; Q = b == 34; C = b == 44; R = b == 13; L = b == 10;
err |= ((b < 32) & !R & !L) | (b >= 127);
err |= ~in & ((Q & !ok) | (q & !(Q | C | R)) | (r ^ L));
in ^= Q; ok = Q | C | L; q = Q; r = R;
}
return !(err | in | r);
}
It already looks horrible. That said, it is a mixed blessing. The running time no longer depends on the data (a flat 0.32 GB/s with GCC 13 and 0.40 GB/s with Clang 18 on Zen 4), but every byte now costs 45 to 50 instructions, depending on the compiler, so it only beats the branchy loop on input whose structure the branch predictor cannot learn. Testing on distributions skewed towards errors will also cost us on speed that would otherwise have been saved by short-circuiting.
The flags pay off once we evaluate them for many bytes at a time: classic SWAR (SIMD-Within-A-Register) yields a rather demonic but markedly faster solution with 8-way slicing and a familiar scalar tail:
#define ONES UINT64_C(0x0101010101010101)
#define HIGH UINT64_C(0x8080808080808080)
#define LOW UINT64_C(0x7f7f7f7f7f7f7f7f)
/* High bit of each byte of v that equals c. Also accepts c | 0x80,
which is harmless here, since every byte >= 0x80 is rejected anyway. */
static inline uint64_t eq(uint64_t v, unsigned c) {
return ~(((v ^ (ONES * c)) & LOW) + LOW) & HIGH;
}
int csv_swar(const uint8_t * p, size_t n) {
uint64_t carry = 0, oklast = 128, qlast = 0, rlast = 0, err = 0;
for (; n >= 8; p += 8, n -= 8) {
uint64_t v = *(uint64_t *) p;
uint64_t q = eq(v, 34), c = eq(v, 44), r = eq(v, 13), l = eq(v, 10);
uint64_t invalid =
(v & HIGH) | ((~((v & LOW) + ONES * 96) & HIGH) & ~(r | l)) | eq(v, 127);
uint64_t ok = q | c | l;
uint64_t bad =
(q & ~((ok << 8) | oklast))
| (((q << 8) | qlast) & ~(q | c | r))
| (((r << 8) | rlast) ^ l);
uint64_t a = q; a ^= a << 8; a ^= a << 16; a ^= a << 32; a ^= carry;
err |= invalid | (bad & ~((a << 8) | (carry & 128)));
carry = (0ull - (a >> 63)) & HIGH;
oklast = ok >> 56; qlast = q >> 56; rlast = r >> 56;
}
unsigned in = !!carry, ok = !!oklast, q = !!qlast, r = !!rlast, b, Q, C, L, R;
for (; n; ++p, --n) {
b = *p; Q = b == 34; C = b == 44; R = b == 13; L = b == 10;
if ((b < 32 && !R && !L) || b >= 127)
return 0;
if (!in && ((Q && !ok) || (q && !(Q || C || R)) || (r != L)))
return 0;
in ^= Q; ok = Q | C | L; q = Q; r = R;
}
return !err && !in && !r;
}
This runs at 1.59 GB/s (or so) regardless of the data, 2.7x - 6.3x the speed of csv_scalar.
Automata ⎇
We can also attack the problem by implementing a straightforward, byte-level shift-DFA with a compact (1KiB) lookup table. Each state is a shift amount; the six explicit states that we read from the grammar are:
- start of a field (5),
- inside an unquoted field (10),
- inside a quoted field (15),
- ust after a quote inside a quoted field (20),
- just after a CR (25), and
- dead (0).
States 5, 10 and 20 accept.
static const uint32_t T[256] = {
[0x20 ... 0x21] = 0x0007a940,
[0x23 ... 0x2b] = 0x0007a940,
[0x2d ... 0x7e] = 0x0007a940,
['"'] = 0x00fa01e0, ['\n'] = 0x0a078000,
[','] = 0x005794a0, ['\r'] = 0x0197e720
};
int csv_dfa(const uint8_t * p, size_t n) {
uint32_t s = 5;
for (size_t i = 0; i < n; ++i) s = T[p[i]] >> (s & 31);
return (0x100420 >> (s & 31)) & 1;
}
Once we set off to minimise the table size (down to 448 bytes) and process two bytes at once, we observe an elegant, emergent solution based on 6 character classes that avoids the duplication of the transition table that would otherwise be incurred. An odd trailing byte is paired with a virtual comma, which maps accepting states to accepting states and rejecting ones to rejecting ones:
static const uint8_t c[256] = {
0,0,0,0,0,0,0,0,0,0,5,0,0,4,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,2,1,1,1,1,1,1,1,1,1,3,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0
};
static const uint32_t d[6][8] = {
{0},
{0,0x0007a940,0x000a0000,0x000794a0,0x0007e720,0x00078000},
{0,0x00f001e0,0x01478280,0x00f281e0,0x00fc81e0,0x00f001e0},
{0,0x00a7a940,0x00fa3de0,0x005794a0,0x0197e720,0x00078000},
{0,0x00078000,0x000a0000,0x00078000,0x00078000,0x005794a0},
{0,0x14078000,0x1e0a0000,0x0a078000,0x32078000,0x00078000}
};
int csv_class(const uint8_t * p, size_t n) {
uint32_t s = 5;
for (; n >= 2; p += 2, n -= 2)
s = d[c[p[0]]][c[p[1]]] >> (s & 31);
if (n) s = d[c[*p]][3] >> (s & 31);
return (0x100420u >> (s & 31)) & 1;
}
Consuming two bytes per step halves the serial dependency chain, and the table is admittedly smaller than csv_dfa's, but this is currently not faster: each step now issues five loads, hence the load ports become the bottleneck instead.
Table minimisation can be pushed even further, down to only 120 bytes. Here cls packs the classes of four 16-byte rows of the ASCII table into 3-bit fields of lo, and hi picks the field for the high nibble; a shift of 12 lands on zero bits for control characters 0x10-0x1F and for non-ASCII bytes. States are 3-bit fields this time, with the state number times 3 as the shift:
static const uint16_t lo[16] = {
577,577,641,577,577,577,577,577,
577,577,617,577,705,609,577,65
};
static const uint8_t hi[16] = {
3,12,6,0,0,0,0,9,12,12,12,12,12,12,12,12
};
static inline uint16_t cls(uint16_t x) {
return (lo[x & 15] >> hi[x >> 4]) & 7;
}
static const uint16_t d16[6][6] = {
{0},
{0,0x00d2,0x0100,0x00c9,0x00ed,0x00c0},
{0,0x0603,0x08c4,0x0643,0x0743,0x0603},
{0,0x04d2,0x071b,0x02c9,0x0aed,0x00c0},
{0,0x00c0,0x0100,0x00c0,0x00c0,0x02c9},
{0,0x20c0,0x3100,0x10c0,0x50c0,0x00c0}
};
int csv_tiny(const uint8_t * p, size_t n) {
uint16_t s = 3;
for (; n >= 2; p += 2, n -= 2)
s = 3 * ((((uint32_t)d16[cls(p[0])][cls(p[1])] << 3) >> s) & 7);
if (n)
s = 3 * ((((uint32_t)d16[cls(*p)][3] << 3) >> s) & 7);
return (0x1048u >> s) & 1;
}
This one trades speed for size: the class computation now sits on the critical path, and it runs at about a third of the speed of csv_dfa, because it is chock-full of stalls in the load ports. But nonetheless perhaps it is interesting for some low-memory embedded applications where the branchy version is stalled and the branchless version is too full of arithmetic.
Going in the opposite direction, increasing the size of the transition table up to 8KiB (6KiB of transitions and 2KiB of row pointers on a 64-bit target) opens up room for a modest improvement (+9% over csv_dfa with GCC, +14% with Clang), with 4-way unrolled transitions processing 8 bytes per iteration, independently fetching successor states and quickly folding them. Here, csv_init has to be called once before the first use (or the tables have to be hardcoded):
static uint32_t D[6][256];
static const uint32_t * P[256];
void csv_init(void) {
static const uint32_t t[6][6] = {
{0},
{0,0x0007a940,0x000a0000,0x000794a0,0x0007e720,0x00078000},
{0,0x00f001e0,0x01478280,0x00f281e0,0x00fc81e0,0x00f001e0},
{0,0x00a7a940,0x00fa3de0,0x005794a0,0x0197e720,0x00078000},
{0,0x00078000,0x000a0000,0x00078000,0x00078000,0x005794a0},
{0,0x14078000,0x1e0a0000,0x0a078000,0x32078000,0x00078000}
};
uint8_t c[256] = {0};
for (unsigned i = 32; i < 127; ++i) c[i] = 1;
c['"'] = 2; c[','] = 3; c['\r'] = 4; c['\n'] = 5;
for (unsigned i = 0; i < 256; ++i) {
P[i] = D[c[i]];
for (unsigned j = 0; j < 6; ++j)
D[j][i] = t[j][c[i]];
}
}
int csv_row(const uint8_t * p, size_t n) {
uint32_t s = 5;
for (; n >= 8; p += 8, n -= 8) {
s = P[p[0]][p[1]] >> (s & 31);
s = P[p[2]][p[3]] >> (s & 31);
s = P[p[4]][p[5]] >> (s & 31);
s = P[p[6]][p[7]] >> (s & 31);
}
for (; n >= 2; p += 2, n -= 2)
s = P[p[0]][p[1]] >> (s & 31);
if (n)
s = P[*p][','] >> (s & 31);
return (0x100420u >> (s & 31)) & 1;
}
I estimate the largest feasible table size, without incurring too much d-cache bloat on downstream users, to be a little over 20KB; the tables below weigh 22,056 bytes. Within this regime, it appears that this idea is the fastest:
static const uint8_t pair[128][128] = {
[10] = {[10] = 1, [13] = 64, [32 ... 33] = 1, [34] = 67, [35 ... 126] = 1},
[13] = {[10] = 4, [13] = 1, [32 ... 33] = 128, [34] = 69, [35 ... 43] = 128,
[44] = 129, [45 ... 126] = 128},
[32 ... 33] = {[10] = 3, [13] = 1, [32 ... 33] = 65, [34] = 67, [35 ... 43] = 65,
[44] = 66, [45 ... 126] = 65},
[34] = {[10] = 6, [13] = 5, [32 ... 33] = 5, [34] = 71, [35 ... 43] = 5,
[44] = 70, [45 ... 126] = 5},
[35 ... 43] = {[10] = 3, [13] = 1, [32 ... 33] = 65, [34] = 67, [35 ... 43] = 65,
[44] = 66, [45 ... 126] = 65},
[44] = {[10] = 2, [13] = 1, [32 ... 33] = 7, [34] = 68, [35 ... 43] = 7,
[44] = 64, [45 ... 126] = 7},
[45 ... 126] = {[10] = 3, [13] = 1, [32 ... 33] = 65, [34] = 67, [35 ... 43] = 65,
[44] = 66, [45 ... 126] = 65}
};
static const uint32_t map[1162] = {
[9 ... 12] = 0x78000u, [13 ... 14] = 0xa0000u, [15] = 0x78000u, [17 ... 20] = 0x78000u,
[21 ... 22] = 0xa0000u, [23] = 0xa078000u, [25 ... 28] = 0x78000u, [29 ... 30] = 0xa0000u,
[31] = 0xa078000u, [33] = 0x78000u, [34] = 0xa078000u, [35] = 0x14078000u,
[36] = 0x32078000u, [37] = 0xa0000u, [38] = 0x1e0a0000u, [39] = 0x78000u,
[49 ... 52] = 0x1e000000u, [53 ... 54] = 0x28000000u, [55] = 0x1e000000u,
[57 ... 60] = 0x78000u, [61 ... 62] = 0xa0000u, [63] = 0x794a0u, [72 ... 74] = 0x78000u,
[76] = 0x28000u, [77] = 0xc8000u, [78] = 0xa0000u, [79] = 0x78000u, [80] = 0xa078000u,
[81 ... 82] = 0x14078000u, [83] = 0x1e000000u, [84] = 0x1e028000u, [85] = 0x1e0c8000u,
[86] = 0x1e0a0000u, [87] = 0x28078000u, [88] = 0xa078000u, [89 ... 90] = 0x14078000u,
[92] = 0x28000u, [93] = 0xc8000u, [94] = 0x1e0a0000u, [95 ... 98] = 0x78000u,
[100] = 0x28000u, [101] = 0xc8000u, [102] = 0xa0000u, [103] = 0x78000u, [104] = 0x28000u,
[106] = 0x50000u, [107 ... 110] = 0x78000u, [111] = 0xa0000u, [112] = 0x1e028000u,
[113] = 0x1e000000u, [114] = 0x1e050000u, [115] = 0x78000u, [116] = 0xa078000u,
[117] = 0x32078000u, [118] = 0x28078000u, [119] = 0x1e0a0000u, [120] = 0x794a0u,
[121 ... 122] = 0x7a940u, [123] = 0x3de0u, [124] = 0x2bde0u, [125] = 0xcbde0u,
[126] = 0xa3de0u, [127] = 0x7d280u, [136 ... 137] = 0x78000u, [144 ... 145] = 0x32078000u,
[152 ... 153] = 0x32078000u, [160 ... 161] = 0x78000u, [169] = 0xc8000u,
[176] = 0x1e000000u, [177] = 0x1e0c8000u, [184 ... 185] = 0x7e720u,
[513 ... 516] = 0x78000u, [517 ... 518] = 0xa0000u, [519] = 0x5794a0u,
[521 ... 524] = 0x78000u, [525 ... 526] = 0xa0000u, [527] = 0x794a0u,
[529 ... 532] = 0x78000u, [533 ... 534] = 0xa0000u, [535] = 0x5794a0u,
[537 ... 540] = 0xf001e0u, [541 ... 542] = 0x1400280u, [543] = 0xf001e0u,
[545 ... 548] = 0xf001e0u, [549 ... 550] = 0x1400280u, [551] = 0xf281e0u,
[553] = 0xf001e0u, [554] = 0xf281e0u, [555] = 0xf501e0u, [556] = 0xfc81e0u,
[557] = 0x1400280u, [558] = 0x1478280u, [559] = 0xf001e0u, [561 ... 564] = 0xf03de0u,
[565 ... 566] = 0x1405280u, [567] = 0xf03de0u, [569 ... 572] = 0x78000u,
[573 ... 574] = 0xa0000u, [575] = 0x78000u, [576] = 0x5794a0u, [577 ... 578] = 0xa7a940u,
[579] = 0xf03de0u, [580] = 0xf2bde0u, [581] = 0xfcbde0u, [582] = 0xfa3de0u,
[583] = 0x147d280u, [584] = 0x794a0u, [585 ... 586] = 0x7a940u, [588] = 0x28000u,
[589] = 0xc8000u, [590] = 0xa3de0u, [591] = 0x78000u, [592] = 0x5794a0u,
[593 ... 594] = 0xa7a940u, [596] = 0x28000u, [597] = 0xc8000u, [598] = 0xfa3de0u,
[599] = 0x78000u, [600 ... 602] = 0xf001e0u, [604] = 0x5000a0u, [605] = 0x1900320u,
[606] = 0x1400280u, [607] = 0xf001e0u, [608] = 0xf281e0u, [609 ... 610] = 0xf501e0u,
[611] = 0x78000u, [612] = 0x5780a0u, [613] = 0x1978320u, [614] = 0x1478280u,
[615] = 0xfa01e0u, [616 ... 618] = 0xf001e0u, [620] = 0x5000a0u, [621] = 0x1900320u,
[622] = 0x1400280u, [623] = 0xf001e0u, [624] = 0xf2bde0u, [625] = 0xf03de0u,
[626] = 0xf53de0u, [627] = 0x78000u, [628] = 0x5794a0u, [629] = 0x197e720u,
[630] = 0x147d280u, [631] = 0xfa3de0u, [632] = 0x5780a0u, [633] = 0x78000u,
[634] = 0xa78140u, [635] = 0xf001e0u, [636] = 0xf281e0u, [637] = 0xfc81e0u,
[638] = 0xfa01e0u, [639] = 0x1478280u, [640 ... 641] = 0x197e720u,
[648 ... 649] = 0x7e720u, [656 ... 657] = 0x197e720u, [664 ... 665] = 0xf001e0u,
[672 ... 673] = 0xfc81e0u, [680 ... 681] = 0xf001e0u, [688] = 0xf03de0u, [689] = 0xfcbde0u,
[696] = 0x78000u, [697] = 0x1978320u, [1025] = 0x78000u, [1026] = 0x794a0u,
[1027] = 0x7a940u, [1028] = 0x7e720u, [1029] = 0xa0000u, [1030] = 0xa3de0u,
[1031] = 0x78000u, [1033] = 0x78000u, [1034] = 0x5794a0u, [1035] = 0xa7a940u,
[1036] = 0x197e720u, [1037] = 0xa0000u, [1038] = 0xfa3de0u, [1039] = 0x78000u,
[1088 ... 1090] = 0x78000u, [1092] = 0x28000u, [1093] = 0xc8000u, [1094] = 0xa0000u,
[1095 ... 1098] = 0x78000u, [1100] = 0x28000u, [1101] = 0xc8000u, [1102] = 0xa0000u,
[1103] = 0x78000u, [1152 ... 1153] = 0x78000u, [1160 ... 1161] = 0x78000u,
};
static const uint32_t tail[256] = {
[10] = 0xa078000u, [13] = 0x197e720u, [32 ... 33] = 0x7a940u, [34] = 0xfa01e0u,
[35 ... 43] = 0x7a940u, [44] = 0x5794a0u, [45 ... 126] = 0x7a940u,
};
static inline uint32_t block(uint32_t s, uint64_t w) {
const unsigned char * p = (const unsigned char *) &pair;
w = (w & 0x007f007f007f007full) | ((w >> 1) & 0x3f803f803f803f80ull);
s = map[8u * p[(uint16_t)w] + p[(uint16_t) (w >> 16)]] >> (s & 31);
s = map[8u * p[(uint16_t) (w >> 32)] + p[w >> 48]] >> (s & 31);
return s;
}
int csv_auto32(const uint8_t * p, size_t n) {
uint32_t s = 5;
uint64_t high = 0;
for (; n >= 32; p += 32, n -= 32) {
uint64_t w0 = *(uint64_t *)(p + 0);
uint64_t w1 = *(uint64_t *)(p + 8);
uint64_t w2 = *(uint64_t *)(p + 16);
uint64_t w3 = *(uint64_t *)(p + 24);
high |= w0 | w1 | w2 | w3;
s = block(s,w0);
s = block(s,w1);
s = block(s,w2);
s = block(s,w3);
}
for (; n >= 8; p += 8, n -= 8) {
uint64_t w = *(uint64_t *)(p);
high |= w;
s = block(s,w);
}
if (high & 0x8080808080808080ull) return 0;
while (n--) s = tail[*p++] >> (s & 31);
return (0x100420u >> (s & 31)) & 1;
}
At 5.85 GB/s with GCC it is 60% faster than csv_dfa, and, as we will see, the fastest of all methods on short inputs.
SIMD ⎇
To efficiently leverage wide vector instruction sets we have to scrap the load-constrained finite state machines and go back to the SWAR implementation. Elementary tweaks combined with the clever idea of using PCLMUL for implementing quotes gets us here:
#define MASK64(a, b) ((uint64_t)(uint32_t)_mm256_movemask_epi8(a) | \
(uint64_t)(uint32_t)_mm256_movemask_epi8(b) << 32)
int csv_avx2(const uint8_t * p, size_t n) {
const uint8_t * start = p;
const __m256i Q = _mm256_set1_epi8('"'), C = _mm256_set1_epi8(',');
const __m256i R = _mm256_set1_epi8('\r'), L = _mm256_set1_epi8('\n');
const __m256i one = _mm256_set1_epi8(1), lo = _mm256_set1_epi8(32);
const __m128i ones = _mm_set1_epi32(-1);
uint64_t carry = 0, ok = 1, pq = 0, pr = 0, err = 0, q, c, r, l, v, k, m, bad, pref;
for (; n >= 64; p += 64, n -= 64) {
__m256i a = _mm256_loadu_si256((const __m256i *) p);
__m256i b = _mm256_loadu_si256((const __m256i *) (p + 32));
q = MASK64(_mm256_cmpeq_epi8(a,Q), _mm256_cmpeq_epi8(b,Q));
c = MASK64(_mm256_cmpeq_epi8(a,C), _mm256_cmpeq_epi8(b,C));
r = MASK64(_mm256_cmpeq_epi8(a,R), _mm256_cmpeq_epi8(b,R));
l = MASK64(_mm256_cmpeq_epi8(a,L), _mm256_cmpeq_epi8(b,L));
v = MASK64(_mm256_cmpgt_epi8(_mm256_add_epi8(a,one),lo),
_mm256_cmpgt_epi8(_mm256_add_epi8(b,one),lo));
k = q | c | l; m = q | c | r;
bad = (q & ~((k << 1) | ok)) | (((q << 1) | pq) & ~m) | (((r << 1) | pr) ^ l);
pref = _mm_cvtsi128_si64(_mm_clmulepi64_si128(_mm_cvtsi64_si128(q),ones,0));
err |= ~(v | r | l) | (bad & ~(pref ^ q ^ carry));
carry ^= 0ull - (pref >> 63);
ok = k >> 63; pq = q >> 63; pr = r >> 63;
}
if (err) return 0;
unsigned in = !!carry, prev = p == start ? ',' : p[-1];
while (n--) {
unsigned c = *p++;
if (c >= 127 || (c < 32 && c != '\r' && c != '\n')) return 0;
if (!in && (((prev == '\r') != (c == '\n')) ||
(c == '"' && prev != '"' && prev != ',' && prev != '\n') ||
(prev == '"' && c != '"' && c != ',' && c != '\r')))
return 0;
in ^= c == '"'; prev = c;
}
return !in && prev != '\r';
}
The first instinct is getting rid of the pesky move-masks. Unfortunately, AVX2 alone is insufficient for this purpose. AVX512-BW, however, has everything that we need:
int csv_avx512bw(const uint8_t * p, size_t n) {
const uint8_t * start = p;
const __m512i Q = _mm512_set1_epi8('"'), C = _mm512_set1_epi8(',');
const __m512i R = _mm512_set1_epi8('\r'), L = _mm512_set1_epi8('\n');
const __m512i lo = _mm512_set1_epi8(32), hi = _mm512_set1_epi8(94);
const __m128i ones = _mm_set1_epi32(-1);
uint64_t carry = 0, ok = 1, pq = 0, pr = 0, err = 0, q, c, l, r, invalid, k, m, bad, pref;
for (; n >= 64; p += 64, n -= 64) {
__m512i v = _mm512_loadu_si512(p);
q = _mm512_cmpeq_epi8_mask(v,Q); c = _mm512_cmpeq_epi8_mask(v,C);
r = _mm512_cmpeq_epi8_mask(v,R); l = _mm512_cmpeq_epi8_mask(v,L);
invalid = _mm512_cmpgt_epu8_mask(_mm512_sub_epi8(v,lo),hi) & ~(r | l);
k = q | c | l; m = q | c | r;
bad = (q & ~((k << 1) | ok)) | (((q << 1) | pq) & ~m) | (((r << 1) | pr) ^ l);
pref = _mm_cvtsi128_si64(_mm_clmulepi64_si128(_mm_cvtsi64_si128(q),ones,0));
err |= invalid | (bad & ~(pref ^ q ^ carry));
carry ^= 0ull - (pref >> 63);
ok = k >> 63; pq = q >> 63; pr = r >> 63;
}
if (err) return 0;
unsigned in = !!carry, prev = p == start ? ',' : p[-1];
while (n--) {
unsigned c = *p++;
if (c >= 127 || (c < 32 && c != '\r' && c != '\n'))
return 0;
if (!in && (((prev == '\r') != (c == '\n')) ||
(c == '"' && prev != '"' && prev != ',' && prev != '\n') ||
(prev == '"' && c != '"' && c != ',' && c != '\r')))
return 0;
in ^= c == '"'; prev = c;
}
return !in && prev != '\r';
}
The final change is to notice an automaton-like structure in the SIMD version once more, and commit to the somewhat unorthodox choice of performing two unaligned loads, in order to end up with a much leaner hot loop that assumes AVX-512BW, VBMI and PCLMUL:
static const uint8_t prev_flags[128] = {
[0 ... 9] = 9, [10] = 8, [11 ... 12] = 9, [13] = 5, [14 ... 33] = 9, [34] = 10,
[35 ... 43] = 9, [44] = 8, [45 ... 127] = 9
};
static const uint8_t curr_flags[128] = {
[0 ... 9] = 70, [10] = 10, [11 ... 12] = 70, [13] = 4, [14 ... 31] = 70, [32 ... 33] = 6,
[34] = 133, [35 ... 43] = 6, [44] = 4, [45 ... 126] = 6, [127] = 70
};
int csv_avx512vbmi(const uint8_t * p, size_t n) {
unsigned prev = ',', in = 0;
if (n >= 65) {
unsigned first = *p++; --n;
if (first >= 127 || (first < 32 && first != '\r'))
return 0;
uint64_t carry = 0ull - (first == '"'), err = 0, bad, invalid, q;
const __m512i pl = _mm512_loadu_si512(prev_flags), ph = _mm512_loadu_si512(prev_flags+64);
const __m512i cl = _mm512_loadu_si512(curr_flags), ch = _mm512_loadu_si512(curr_flags+64);
const __m512i inv = _mm512_set1_epi8(64); const __m128i ones = _mm_set1_epi32(-1);
for (; n >= 64; p += 64, n -= 64) {
__m512i a = _mm512_loadu_si512(p-1), b = _mm512_loadu_si512(p);
__m512i pf = _mm512_permutex2var_epi8(pl,a,ph), cf = _mm512_permutex2var_epi8(cl,b,ch);
bad = _mm512_test_epi8_mask(pf,cf), q = _mm512_movepi8_mask(cf);
invalid = _mm512_test_epi8_mask(cf,inv) | _mm512_movepi8_mask(b);
uint64_t pref = _mm_cvtsi128_si64(_mm_clmulepi64_si128(_mm_cvtsi64_si128(q),ones,0));
err |= invalid | (bad & ~(pref ^ q ^ carry));
carry ^= 0ull - (pref >> 63);
}
if (err) return 0;
in = !!carry; prev = p[-1];
}
while (n--) {
unsigned c = *p++;
if (c >= 127 || (c < 32 && c != '\r' && c != '\n'))
return 0;
if (!in && (((prev == '\r') != (c == '\n')) ||
(c == '"' && prev != '"' && prev != ',' && prev != '\n') ||
(prev == '"' && c != '"' && c != ',' && c != '\r')))
return 0;
in ^= c == '"'; prev = c;
}
return !in && prev != '\r';
}
Early exit ⎇
None of the table-driven or SIMD validators above stop at the first error (and neither, by design, does csv_branchless). They run into the dead state or accumulate an error mask, and only look at the result once the whole buffer has been consumed.
For the SIMD validators, we fix it by checking each block's error mask and stopping instead of OR-ing it into an accumulator. The same change applies to csv_avx512bw and, with ~(v | r | l) in place of invalid, to csv_avx2:
- int csv_avx512vbmi(const uint8_t * p, size_t n) {
+ int csv_avx512vbmi_ee(const uint8_t * p, size_t n) {
unsigned prev = ',', in = 0;
...
return 0;
- uint64_t carry = 0ull - (first == '"'), err = 0, bad, invalid, q;
+ uint64_t carry = 0ull - (first == '"'), bad, invalid, q;
const __m512i pl = _mm512_loadu_si512(prev_flags), ph = _mm512_loadu_si512(prev_flags+64);
...
uint64_t pref = _mm_cvtsi128_si64(_mm_clmulepi64_si128(_mm_cvtsi64_si128(q),ones,0));
- err |= invalid | (bad & ~(pref ^ q ^ carry));
+ if (invalid | (bad & ~(pref ^ q ^ carry)))
+ return 0;
carry ^= 0ull - (pref >> 63);
}
- if (err) return 0;
in = !!carry; prev = p[-1];
For the automata, the dead state plays the same role. Checking for it (and for non-ASCII bytes) once per 32-byte iteration of csv_auto32 is enough:
- int csv_auto32(const uint8_t * p, size_t n) {
+ int csv_auto32_ee(const uint8_t * p, size_t n) {
uint32_t s = 5;
...
s = block(s,w3);
+ if (!s || (high & 0x8080808080808080ull)) return 0;
}
While the cost on bad inputs is obvious, on valid inputs we only have to pay for one extra branch per block that is never taken. Whether that is free depends mostly on the compiler more than on the CPU and it's mostly independent of annotations like [[unlikely]]. On Zen 4, csv_avx512vbmi_ee is as fast as csv_avx512vbmi (+2% with GCC 13, +1% with Clang 18), and csv_avx512bw_ee is too with Clang 18 (+1%), but with GCC 13 it loses 25%, csv_avx2_ee loses 8%, and csv_auto32_ee 6% (with Clang 18, the latter gains 4%).
Early exit also suggests a particularly interesting speculative variant: if a block has no suspicious byte pair at all (invalid | bad is zero), the quote mask cannot matter and the CLMUL can be skipped, with the carry updated from the parity of the quote count instead:
- int csv_avx512vbmi(const uint8_t * p, size_t n) {
+ int csv_avx512vbmi_lazy(const uint8_t * p, size_t n) {
unsigned prev = ',', in = 0;
...
return 0;
- uint64_t carry = 0ull - (first == '"'), err = 0, bad, invalid, q;
+ uint64_t carry = 0ull - (first == '"'), bad, invalid, q;
const __m512i pl = _mm512_loadu_si512(prev_flags), ph = _mm512_loadu_si512(prev_flags+64);
...
invalid = _mm512_test_epi8_mask(cf,inv) | _mm512_movepi8_mask(b);
- uint64_t pref = _mm_cvtsi128_si64(_mm_clmulepi64_si128(_mm_cvtsi64_si128(q),ones,0));
- err |= invalid | (bad & ~(pref ^ q ^ carry));
- carry ^= 0ull - (pref >> 63);
+ if (invalid | bad) {
+ uint64_t pref = _mm_cvtsi128_si64(_mm_clmulepi64_si128(_mm_cvtsi64_si128(q),ones,0));
+ if (invalid | (bad & ~(pref ^ q ^ carry)))
+ return 0;
+ }
+ carry ^= 0ull - (uint64_t)(__builtin_popcountll(q) & 1);
}
- if (err) return 0;
in = !!carry; prev = p[-1];
This is quite fast on data without quotes. However, as soon as we start running into files with short strings inside of them, the performance collapses.
How fast? ⎇
I used one computer to run some benchmarks.
- CPU: AMD EPYC 9534 (x2)
- µarch: Zen 4
- Cores / caches: 64 C / socket, 1 MiB L2 per core, 32 MiB L3 per CCD
- OS: Linux 6.8
- Clock: 3.36 GHz
All benchmarks are built with -falign-functions=64. Without specifying this flag, the address of a function in the final binary broadly depends on link order, resulting in mysterious slowdowns when tight loops happen to straddle a 64-byte line and run at half speed.
We consider six synthetic but realistic 16MiB datasets that adhere to RFC 4180:
| dataset | contents | size | quotes | commas | LF | avg field |
|---|---|---|---|---|---|---|
numeric | IDs, integers, decimals, dates, times, scientific notation; no quotes | 16 MiB | 0.0% | 11.5% | 1.27% | 7.9 |
text | 6 long unquoted text fields of 3-14 words per row | 16 MiB | 0.0% | 1.7% | 0.34% | 48.7 |
quoted | 8 quoted fields per row with embedded , "" CRLF and LF | 16 MiB | 7.9% | 4.1% | 1.68% | 17.3 |
mixed | order export: names, e-mails, addresses (some quoted, with commas), amounts, optional quoted notes | 16 MiB | 2.2% | 8.3% | 0.83% | 10.9 |
dense | 32 fields of 0-2 bytes per row: empty, x, "", "x" | 16 MiB | 28.1% | 45.5% | 1.47% | 2.1 |
walk | random walk of the DFA over random TEXTDATA bytes | 16 MiB | 22.9% | 15.4% | 15.43% | 3.2 |
...and four further scenarios: L2-128K validates a 128KiB prefix of mixed over and over (L2-resident); DRAM validates a 512MiB mixed-style file (DRAM-resident); rows validates each record of mixed (CRLF included) as a separate buffer, 138,931 rows of 121 on average; and reject@1K is mixed with a NUL byte at offset 1000, timed per call.
Aggregate ⎇
AMD EPYC 9534 (Zen 4), -O2. .text gives each function's size; for csv_row the runtime-generated table size is counted in to the overall measured table size. SLoC counts non-blank lines, including the tables and helpers. Throughput is the geometric mean over the six datasets. Two numbers separated by a slash were gathered by compiling with GCC and Clang respectively. Cycles per byte (CPB), DRAM GB/s, and reject@1K were measured only on GCC.
| Method | .text(B) | Tables(B) | SLoC | GB/s | CPB | rows GB/s | DRAM GB/s | reject@1K |
|---|---|---|---|---|---|---|---|---|
scalar | 206 / 205 | 0 | 18 | 0.47 / 0.50 | 7.77 | 0.56 / 0.60 | 0.57 | 1.6 µs |
branchless | 230 / 230 | 0 | 10 | 0.32 / 0.40 | 11.63 | 0.31 / 0.39 | 0.32 | 52.98 ms |
swar (L) | 848 / 748 | 0 | 39 | 1.59 / 1.63 | 2.32 | 1.26 / 1.26 | 1.59 | 10.56 ms |
dfa | 70 / 199 | 1,024 | 12 | 3.66 / 3.67 | 1.01 | 2.81 / 2.83 | 3.65 | 4.59 ms |
class | 170 / 277 | 448 | 26 | 3.67 / 3.04 | 1.00 | 2.84 / 2.55 | 3.67 | 4.57 ms |
tiny | 282 / 220 | 120 | 26 | 1.25 / 1.24 | 2.95 | 1.14 / 1.14 | 1.25 | 13.47 ms |
row | 279 / 281 | 8,192 | 34 | 3.97 / 4.17 | 0.93 | 2.87 / 2.90 | 3.97 | 4.23 ms |
auto32 (L) | 748 / 951 | 22,056 | 103 | 5.85 / 5.10 | 0.63 | 3.48 / 3.11 | 5.85 | 2.87 ms |
auto32_ee (L) | 785 / 934 | 22,056 | 104 | 5.53 / 5.33 | 0.67 | 3.31 / 3.12 | 5.52 | 3.04 ms |
avx2 (*1) | 783 / 621 | 0 | 38 | 11.1 / 12.2 | 0.33 | 1.45 / 1.78 | 11.1 | 1.51 ms |
avx2_ee (*1) | 748 / 625 | 0 | 38 | 10.2 / 11.9 | 0.36 | 1.47 / 1.74 | 10.2 | 102.0 ns |
avx512bw (*2) | 736 / 569 | 0 | 33 | 14.3 / 15.1 | 0.26 | 1.48 / 1.81 | 14.3 | 1.17 ms |
avx512bw_ee (*2) | 701 / 571 | 0 | 33 | 10.8 / 15.3 | 0.34 | 1.48 / 1.80 | 10.8 | 94.0 ns |
vbmi (*3) | 595 / 501 | 256 | 42 | 25.0 / 30.4 | 0.15 | 1.52 / 1.80 | 23.5 | 671.0 µs |
vbmi_ee (*3) | 689 / 485 | 256 | 42 | 25.5 / 30.7 | 0.14 | 1.52 / 1.81 | 24.5 | 40.0 ns |
vbmi_lazy (*3) | 569 / 506 | 256 | 44 | 21.3 / 24.6 | 0.17 | 1.45 / 1.75 | 7.79 | 37.0 ns |
Method: L stands for Little Endian;
- AVX2, PCLMUL,
- AVX-512BW, PCLMUL,
- AVX-512BW+VBMI, PCLMUL
GB/s per dataset ⎇
AMD EPYC 9534 (Zen 4), GCC 13.3, -O2, clock 3.68 GHz:
| method | numeric | text | quoted | mixed | dense | walk | L2-128K | DRAM | rows | reject@1K |
|---|---|---|---|---|---|---|---|---|---|---|
scalar | 0.59 | 0.60 | 0.68 | 0.58 | 0.31 | 0.25 | 0.60 | 0.57 | 0.56 | 1.6 µs |
branchless | 0.32 | 0.32 | 0.32 | 0.32 | 0.32 | 0.32 | 0.32 | 0.32 | 0.31 | 52.98 ms |
swar | 1.59 | 1.59 | 1.59 | 1.59 | 1.59 | 1.59 | 1.59 | 1.59 | 1.26 | 10.56 ms |
dfa | 3.65 | 3.66 | 3.65 | 3.66 | 3.66 | 3.66 | 3.66 | 3.65 | 2.81 | 4.59 ms |
class | 3.67 | 3.67 | 3.67 | 3.67 | 3.67 | 3.67 | 3.68 | 3.67 | 2.84 | 4.57 ms |
tiny | 1.25 | 1.25 | 1.25 | 1.24 | 1.25 | 1.25 | 1.25 | 1.25 | 1.14 | 13.47 ms |
row | 3.98 | 3.97 | 3.97 | 3.97 | 3.97 | 3.97 | 3.98 | 3.97 | 2.87 | 4.23 ms |
auto32 | 5.89 | 5.84 | 5.85 | 5.85 | 5.85 | 5.84 | 5.88 | 5.85 | 3.48 | 2.87 ms |
auto32_ee | 5.55 | 5.52 | 5.53 | 5.53 | 5.54 | 5.51 | 5.55 | 5.52 | 3.31 | 3.04 ms |
avx2 | 11.1 | 11.1 | 11.1 | 11.1 | 11.1 | 11.1 | 11.0 | 11.1 | 1.45 | 1.51 ms |
avx2_ee | 10.2 | 10.2 | 10.2 | 10.2 | 10.2 | 10.2 | 10.2 | 10.2 | 1.47 | 102.0 ns |
avx512bw | 14.3 | 14.3 | 14.4 | 14.4 | 14.4 | 14.3 | 14.2 | 14.3 | 1.48 | 1.17 ms |
avx512bw_ee | 10.8 | 10.8 | 10.8 | 10.8 | 10.8 | 10.8 | 10.7 | 10.8 | 1.48 | 94.0 ns |
vbmi | 24.9 | 25.0 | 25.0 | 25.0 | 25.0 | 25.0 | 24.8 | 23.5 | 1.52 | 671.0 µs |
vbmi_ee | 25.4 | 25.5 | 25.4 | 25.4 | 25.5 | 25.5 | 25.1 | 24.5 | 1.52 | 40.0 ns |
vbmi_lazy | 29.3 | 29.3 | 25.4 | 8.03 | 25.0 | 21.3 | 26.1 | 7.79 | 1.45 | 37.0 ns |
AMD EPYC 9534 (Zen 4), Clang 18.1, -O2, clock 3.67 GHz:
| method | numeric | text | quoted | mixed | dense | walk | L2-128K | DRAM | rows | reject@1K |
|---|---|---|---|---|---|---|---|---|---|---|
scalar | 0.59 | 0.60 | 0.82 | 0.60 | 0.33 | 0.26 | 0.62 | 0.60 | 0.60 | 1.6 µs |
branchless | 0.40 | 0.40 | 0.40 | 0.40 | 0.40 | 0.40 | 0.40 | 0.40 | 0.39 | 41.86 ms |
swar | 1.63 | 1.63 | 1.63 | 1.63 | 1.63 | 1.63 | 1.61 | 1.63 | 1.26 | 10.29 ms |
dfa | 3.67 | 3.67 | 3.67 | 3.67 | 3.67 | 3.67 | 3.67 | 3.67 | 2.83 | 4.57 ms |
class | 3.04 | 3.04 | 3.04 | 3.04 | 3.04 | 3.04 | 3.24 | 3.04 | 2.55 | 5.51 ms |
tiny | 1.24 | 1.24 | 1.24 | 1.24 | 1.24 | 1.24 | 1.23 | 1.23 | 1.14 | 13.58 ms |
row | 4.18 | 4.16 | 4.18 | 4.17 | 4.17 | 4.16 | 4.08 | 4.17 | 2.90 | 4.03 ms |
auto32 | 5.13 | 5.09 | 5.09 | 5.10 | 5.10 | 5.09 | 5.13 | 5.09 | 3.11 | 3.28 ms |
auto32_ee | 5.35 | 5.33 | 5.33 | 5.33 | 5.32 | 5.31 | 5.36 | 5.33 | 3.12 | 3.15 ms |
avx2 | 12.2 | 12.1 | 12.2 | 12.2 | 12.2 | 12.2 | 12.2 | 12.1 | 1.78 | 1.38 ms |
avx2_ee | 11.9 | 11.9 | 11.9 | 11.9 | 11.9 | 11.9 | 12.0 | 11.9 | 1.74 | 88.0 ns |
avx512bw | 15.2 | 15.1 | 15.1 | 15.1 | 15.1 | 15.1 | 15.0 | 15.0 | 1.81 | 1.11 ms |
avx512bw_ee | 15.3 | 15.3 | 15.3 | 15.3 | 15.3 | 15.3 | 15.2 | 15.2 | 1.80 | 67.0 ns |
vbmi | 30.8 | 30.4 | 30.4 | 30.4 | 30.4 | 30.4 | 29.9 | 29.0 | 1.80 | 551.8 µs |
vbmi_ee | 30.9 | 30.8 | 30.8 | 30.0 | 30.8 | 30.8 | 30.2 | 28.7 | 1.81 | 35.0 ns |
vbmi_lazy | 38.3 | 38.4 | 28.4 | 7.95 | 27.9 | 23.9 | 30.7 | 7.85 | 1.75 | 33.0 ns |
Everything except csv_scalar and csv_avx512vbmi_lazy is data-oblivious on valid input. csv_scalar runs at 0.68 GB/s on quoted but only 0.25 GB/s on walk, crafted to make the branches inside of it unpredictable. On the EPYC, even DRAM-512M stays within 6% of the in-cache performance numbers. As it turns out, streaming from memory is very fast!
Compiler variance ⎇
Geometric mean of the six datasets in GB/s on the EPYC 9534. Best per row in bold.
-O2:
| method | GCC 13.3 | GCC 15.2 | GCC 16.2 | Clang 18.1 | Clang 21.1 | max/min |
|---|---|---|---|---|---|---|
scalar | 0.47 | 0.55 | 0.55 | 0.50 | 0.56 | 1.18x |
branchless | 0.32 | 0.33 | 0.33 | 0.40 | 0.40 | 1.27x |
swar | 1.59 | 1.55 | 1.55 | 1.63 | 1.63 | 1.05x |
dfa | 3.65 | 3.65 | 3.65 | 3.66 | 3.67 | 1.00x |
class | 3.67 | 3.67 | 3.67 | 3.04 | 3.04 | 1.21x |
tiny | 1.25 | 1.25 | 1.25 | 1.23 | 1.23 | 1.01x |
row | 3.97 | 3.97 | 3.97 | 4.16 | 4.17 | 1.05x |
auto32 | 5.85 | 5.81 | 4.97 | 5.09 | 4.93 | 1.19x |
auto32_ee | 5.53 | 5.18 | 4.32 | 5.32 | 5.33 | 1.28x |
avx2 | 11.1 | 11.5 | 11.5 | 12.1 | 12.1 | 1.09x |
avx2_ee | 10.2 | 9.86 | 9.86 | 11.9 | 12.1 | 1.23x |
avx512bw | 14.3 | 13.8 | 13.6 | 15.1 | 15.1 | 1.11x |
avx512bw_ee | 10.8 | 10.7 | 11.6 | 15.3 | 15.1 | 1.43x |
vbmi | 25.0 | 25.4 | 25.5 | 30.0 | 30.4 | 1.21x |
vbmi_ee | 25.4 | 25.4 | 23.2 | 30.1 | 30.3 | 1.31x |
vbmi_lazy | 21.2 | 21.0 | 21.2 | 24.4 | 24.4 | 1.16x |
-O3 -march=znver4:
| method | GCC 13.3 | GCC 15.2 | GCC 16.2 | Clang 18.1 | Clang 21.1 | max/min |
|---|---|---|---|---|---|---|
scalar | 0.48 | 0.55 | 0.59 | 0.50 | 0.55 | 1.23x |
branchless | 0.32 | 0.33 | 0.34 | 0.44 | 0.39 | 1.36x |
swar | 1.63 | 1.66 | 1.66 | 1.87 | 1.73 | 1.14x |
dfa | 3.47 | 3.46 | 3.44 | 3.67 | 3.67 | 1.07x |
class | 3.67 | 3.86 | 3.86 | 3.62 | 3.63 | 1.07x |
tiny | 1.33 | 1.33 | 1.33 | 1.50 | 1.45 | 1.13x |
row | 4.22 | 4.23 | 4.23 | 4.08 | 3.99 | 1.06x |
auto32 | 5.74 | 5.49 | 5.02 | 6.01 | 6.21 | 1.24x |
auto32_ee | 5.48 | 4.30 | 4.41 | 5.10 | 5.13 | 1.27x |
avx2 | 10.5 | 10.9 | 10.9 | 12.9 | 16.9 | 1.61x |
avx2_ee | 9.62 | 9.94 | 9.94 | 12.6 | 16.1 | 1.67x |
avx512bw | 13.4 | 14.7 | 13.4 | 14.6 | 14.8 | 1.10x |
avx512bw_ee | 14.4 | 14.5 | 13.4 | 15.0 | 15.1 | 1.13x |
vbmi | 27.9 | 27.8 | 28.3 | 31.5 | 31.9 | 1.15x |
vbmi_ee | 28.2 | 28.3 | 24.8 | 31.9 | 32.2 | 1.30x |
vbmi_lazy | 21.1 | 21.4 | 23.0 | 25.5 | 25.5 | 1.21x |
Change from -O2 to -O3 -march=znver4, per compiler:
| method | GCC 13.3 | GCC 15.2 | GCC 16.2 | Clang 18.1 | Clang 21.1 |
|---|---|---|---|---|---|
scalar | +1% | -1% | +7% | +1% | -0% |
branchless | +2% | +0% | +0% | +9% | -2% |
swar | +3% | +7% | +7% | +15% | +6% |
dfa | -5% | -5% | -6% | +0% | +0% |
class | +0% | +5% | +5% | +19% | +19% |
tiny | +7% | +7% | +7% | +22% | +18% |
row | +6% | +7% | +7% | -2% | -4% |
auto32 | -2% | -5% | +1% | +18% | +26% |
auto32_ee | -1% | -17% | +2% | -4% | -4% |
avx2 | -6% | -5% | -5% | +6% | +39% |
avx2_ee | -6% | +1% | +1% | +6% | +32% |
avx512bw | -7% | +7% | -1% | -3% | -2% |
avx512bw_ee | +34% | +35% | +16% | -2% | +0% |
vbmi | +12% | +9% | +11% | +5% | +5% |
vbmi_ee | +11% | +11% | +7% | +6% | +6% |
vbmi_lazy | -0% | +2% | +9% | +5% | +4% |
We observe the following:
- No compiler is broadly good. Clang produces the faster SIMD code, while GCC produces the faster automata.
- There's plenty of regressions to be seen as one upgrades compilers. GCC 16 is 15% slower than GCC 13 on
csv_auto32and 22% slower oncsv_auto32_ee. - At
-O2, GCC shuffles masks between general-purpose andkregisters in the AVX-512BW loop. GCC 13 issues 12kmovinstructions per 64-byte block (14 with early exit), while Clang 18 issues 7. GCC 13 also foldsbad & ~maskincsv_avx512vbmiinto a maskedvptestmb. Early exit incsv_avx512bw_eeslows down every GCC build between 15% and 25%. In the case of Clang, this is broadly free. With-O3 -march=znver4all three penalties disappear. - With
-march=znver4, Clang 21 folds the code ofcsv_avx2into AVX-512VL compares into mask registers, replacing all tenvpmovmskb, which accelerates it from 12.1 to 16.9 GB/s, pastcsv_avx512bw. The resulting "AVX2" binary of course no longer runs on AVX2-only machines. GCC 16 is oblivious. - Otherwise
-march=znver4mostly helps Clang's automata (csv_auto32+26%,csv_class+19%) and GCC'scsv_avx512vbmi(+12%), and costs GCC between 5% and 6% on plaincsv_dfa.
Code placement ⎇
We will further investigate the effect of code placement on these programs. We consider same objects again (GCC 13 and Clang 18, -O2), but with every function starting 0, 16, 32 or 48 bytes past a 64-byte boundary instead of on one, tested on mixed, in GB/s:
| method | GCC 13: +0 / +16 / +32 / +48 | max/min | Clang 18: +0 / +16 / +32 / +48 | max/min |
|---|---|---|---|---|
scalar | 0.57 / 0.57 / 0.57 / 0.58 | 1.01x | 0.61 / 0.71 / 0.70 / 0.70 | 1.16x |
branchless | 0.32 / 0.32 / 0.32 / 0.32 | 1.00x | 0.40 / 0.40 / 0.40 / 0.40 | 1.00x |
swar | 1.59 / 1.59 / 1.59 / 1.59 | 1.00x | 1.63 / 1.63 / 1.63 / 1.63 | 1.00x |
dfa | 3.65 / 1.84 / 3.66 / 3.66 | 1.99x | 3.67 / 3.67 / 3.67 / 3.67 | 1.00x |
class | 3.67 / 3.67 / 3.67 / 3.67 | 1.00x | 3.04 / 3.05 / 3.04 / 3.04 | 1.00x |
tiny | 1.25 / 1.24 / 1.24 / 1.25 | 1.00x | 1.23 / 1.24 / 1.23 / 1.24 | 1.00x |
row | 3.97 / 3.97 / 3.97 / 3.97 | 1.00x | 4.17 / 4.17 / 4.17 / 4.17 | 1.00x |
auto32 | 5.84 / 5.85 / 5.85 / 5.85 | 1.00x | 5.09 / 5.10 / 5.11 / 5.11 | 1.00x |
auto32_ee | 5.52 / 5.53 / 5.53 / 5.53 | 1.00x | 5.33 / 5.33 / 5.33 / 5.33 | 1.00x |
avx2 | 11.1 / 11.1 / 11.1 / 11.1 | 1.00x | 12.1 / 12.2 / 12.2 / 12.2 | 1.01x |
avx2_ee | 10.2 / 10.2 / 10.2 / 10.2 | 1.00x | 11.9 / 12.0 / 12.0 / 11.9 | 1.01x |
avx512bw | 14.3 / 14.3 / 14.3 / 14.3 | 1.00x | 15.1 / 15.1 / 15.1 / 15.1 | 1.00x |
avx512bw_ee | 10.8 / 10.8 / 10.8 / 10.8 | 1.00x | 15.2 / 15.2 / 15.2 / 15.2 | 1.00x |
vbmi | 24.9 / 25.5 / 25.5 / 25.5 | 1.02x | 32.1 / 32.1 / 32.1 / 32.3 | 1.01x |
vbmi_ee | 25.4 / 25.4 / 25.4 / 25.3 | 1.00x | 31.7 / 31.7 / 31.7 / 31.7 | 1.00x |
vbmi_lazy | 8.01 / 7.73 / 7.48 / 7.71 | 1.07x | 7.86 / 7.81 / 7.78 / 7.67 | 1.02x |
