csveee 0.2.1

A very fast, parallel CSV parser.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//! Turning a user [`Config`] into a [`ResolvedConfig`].
//!
//! Everything here is policy: given the user's settings plus what we can
//! observe about the input (size, host parallelism), decide which backends
//! to use and how big the work units are. Resolution order matters a lot
//! here.

use crate::config::{Config, IoBackend, ParserBackend};
use crate::io::ringbuf::RingBufSettings;
use crate::parser::dfa::DfaChunkParser;
use crate::parser::simd::SimdChunkParser;
use crate::parser::{ChunkParser, FindRecordStart};

/// Fully-resolved scheduler configuration.
#[derive(Debug)]
pub(super) struct ResolvedConfig {
    pub config: Config,
    pub parser_backend: ParserBackend,
    pub io_backend: IoBackend,
    pub thread_count: usize,
    pub chunk_count: usize,
    pub file_size: usize,
}

impl ResolvedConfig {
    /// Resolve auto sentinels into concrete values for a file input.
    pub(super) fn for_file(config: &Config, file_size: usize) -> crate::Result<Self> {
        let parser_backend = resolve_parser_backend(config)?;

        let chunk_size = resolve_chunk_size(config, parser_backend);
        let chunk_count = file_size.div_ceil(chunk_size);
        let io_buffer_limit = resolve_io_buffer_limit(config);
        let thread_count = resolve_thread_count(config, chunk_count, io_buffer_limit);
        let share = per_thread_share(io_buffer_limit, thread_count);
        let io_backend = resolve_io_backend(config, file_size, share, thread_count);

        let mut config = config.clone();
        config.chunk_size = chunk_size;
        config.io_buffer_limit = io_buffer_limit;
        Ok(ResolvedConfig {
            config,
            parser_backend,
            io_backend,
            thread_count,
            chunk_count,
            file_size,
        })
    }

    /// [`ResolvedConfig::for_file`] for a slice input.
    pub(super) fn for_slice(config: &Config, len: usize) -> crate::Result<Self> {
        let parser_backend = resolve_parser_backend(config)?;

        let chunk_size = resolve_chunk_size(config, parser_backend);
        let chunk_count = len.div_ceil(chunk_size);
        let io_buffer_limit = resolve_io_buffer_limit(config);
        let thread_count = resolve_thread_count(config, chunk_count, io_buffer_limit);
        let share = per_thread_share(io_buffer_limit, thread_count);

        let mut config = config.clone();
        config.chunk_size = chunk_size;
        config.io_buffer_limit = io_buffer_limit;
        Ok(ResolvedConfig {
            config,
            parser_backend,
            io_backend: IoBackend::RingBuf(unbacked_ring_settings(share, thread_count)),
            thread_count,
            chunk_count,
            file_size: len,
        })
    }

    /// Determine how `find_record_start` should behave for a given chunk.
    pub(super) fn find_mode(&self, chunk_idx: usize) -> FindRecordStart {
        if chunk_idx == 0 {
            if self.config.has_headers {
                FindRecordStart::SkipHeaders
            } else {
                FindRecordStart::No
            }
        } else {
            FindRecordStart::Strict
        }
    }
}

/// Size below which `Auto` prefers the in-memory backend over the ring-buffer.
const IN_MEMORY_FILE_SIZE_THRESHOLD: usize = 1024 * 1024;

/// Ring-buffer size `Auto` picks below [`MANY_THREADS`].
const RING_BUFFER_SIZE: usize = 64 * 1024;

/// Ring-buffer size `Auto` picks from [`MANY_THREADS`] up.
const MANY_THREADS_RING_BUFFER_SIZE: usize = 16 * 1024;

/// Worker count from which `Auto` shrinks the ring buffer.
const MANY_THREADS: usize = 64;

/// Ring-buffer size `Auto` picks on Apple platforms, whatever the thread
/// count: the XNU kernel serves only several hundred thousand `read` calls
/// per second across all threads, so we keep the buffer large.
const APPLE_RING_BUFFER_SIZE: usize = 256 * 1024;

/// Determine the number of threads to use.
fn resolve_thread_count(
    config: &Config,
    chunk_count: usize,
    io_buffer_limit: Option<usize>,
) -> usize {
    // concurrency == 0 is the sentinel for "auto-detect now".
    let h = if config.concurrency == 0 {
        crate::sys::available_parallelism()
    } else {
        config.concurrency
    };
    let c = chunk_count;
    let sqrt_c = (c as f64).sqrt().ceil() as usize;
    let b = affordable_thread_count(io_buffer_limit);
    h.min(c).min(6 * sqrt_c).min(b)
}

/// How many workers the I/O buffer budget can pay for.
fn affordable_thread_count(io_buffer_limit: Option<usize>) -> usize {
    match io_buffer_limit {
        None => usize::MAX,
        Some(budget) => (budget / min_thread_buffer()).max(1),
    }
}

/// Smallest buffer a worker can be given and still do useful work.
fn min_thread_buffer() -> usize {
    crate::sys::allocation_granularity()
}

/// Select the I/O backend, resolving `Auto` from file size.
fn resolve_io_backend(
    config: &Config,
    file_size: usize,
    share: Option<usize>,
    thread_count: usize,
) -> IoBackend {
    let selected = match config.io_backend {
        IoBackend::Auto => {
            if file_size <= IN_MEMORY_FILE_SIZE_THRESHOLD {
                IoBackend::InMemory
            } else {
                IoBackend::RingBuf(RingBufSettings::default())
            }
        }
        other => other,
    };
    match selected {
        IoBackend::RingBuf(ring) => {
            IoBackend::RingBuf(resolve_ringbuf_settings(ring, share, thread_count))
        }
        other => other,
    }
}

/// Turn caller-stated [`RingBufSettings`] into the concrete values the
/// ring buffer is built from.
fn resolve_ringbuf_settings(
    ring: RingBufSettings,
    share: Option<usize>,
    thread_count: usize,
) -> RingBufSettings {
    let buffer_limit = resolve_buffer_limit(ring.buffer_limit, share);
    RingBufSettings {
        buffer_size: resolve_buffer_size(ring.buffer_size, thread_count, buffer_limit),
        buffer_limit,
    }
}

/// The working size one worker's buffer starts at and reads by.
fn resolve_buffer_size(own: usize, thread_count: usize, limit: Option<usize>) -> usize {
    if own != 0 {
        return own;
    }
    let size = if cfg!(target_vendor = "apple") {
        APPLE_RING_BUFFER_SIZE
    } else if thread_count >= MANY_THREADS {
        MANY_THREADS_RING_BUFFER_SIZE
    } else {
        RING_BUFFER_SIZE
    };
    match limit {
        Some(limit) if limit < size => 1 << (usize::BITS - 1 - limit.max(1).leading_zeros()),
        _ => size,
    }
}

/// The cap one worker's buffer actually gets.
fn resolve_buffer_limit(own: Option<usize>, share: Option<usize>) -> Option<usize> {
    match own {
        None | Some(0) => share,
        Some(own) => match share {
            Some(share) => Some(own.min(share)),
            None => Some(own),
        },
    }
}

/// Ring-buffer settings for the paths that buffer through a ring buffer
/// without selecting a backend: `parse_slice` and `parse_stream`.
fn unbacked_ring_settings(share: Option<usize>, thread_count: usize) -> RingBufSettings {
    resolve_ringbuf_settings(RingBufSettings::default(), share, thread_count)
}

/// Resolve the chunk size, honoring the auto-detect sentinel.
fn resolve_chunk_size(config: &Config, parser_backend: ParserBackend) -> usize {
    if config.chunk_size != 0 {
        return config.chunk_size;
    }
    match parser_backend {
        ParserBackend::Dfa => 256 * 1024,
        ParserBackend::Simd => 1024 * 1024,
        ParserBackend::Auto => unreachable!("called after resolve_parser_backend"),
    }
}

/// Resolve the *total* I/O buffer budget, honoring the auto sentinel.
fn resolve_io_buffer_limit(config: &Config) -> Option<usize> {
    match config.io_buffer_limit {
        Some(0) => Some(crate::sys::total_ram()? / 2),
        other => other,
    }
}

/// Ring-buffer settings for the streaming path.
pub(super) fn stream_ring_settings(config: &Config) -> RingBufSettings {
    let io_buffer_limit = resolve_io_buffer_limit(config);
    let share = per_thread_share(io_buffer_limit, 1);
    unbacked_ring_settings(share, 1)
}

/// Divide the total limit into one worker's share.
fn per_thread_share(io_buffer_limit: Option<usize>, thread_count: usize) -> Option<usize> {
    let total = io_buffer_limit?;
    if thread_count == 0 {
        return None;
    }
    Some(total / thread_count)
}

/// Select the chunk parser backend, resolving `Auto`.
pub(super) fn resolve_parser_backend(config: &Config) -> crate::Result<ParserBackend> {
    match config.parser_backend {
        ParserBackend::Auto => {
            if SimdChunkParser::supports(config).is_ok() {
                return Ok(ParserBackend::Simd);
            }
            DfaChunkParser::supports(config).map_err(crate::Error::InvalidConfig)?;
            Ok(ParserBackend::Dfa)
        }
        ParserBackend::Dfa => {
            DfaChunkParser::supports(config).map_err(crate::Error::InvalidConfig)?;
            Ok(ParserBackend::Dfa)
        }
        ParserBackend::Simd => {
            SimdChunkParser::supports(config).map_err(crate::Error::InvalidConfig)?;
            Ok(ParserBackend::Simd)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn thread_count_capped_by_chunks() {
        let config = Config::default();
        assert!(resolve_thread_count(&config, 2, None) <= 2);
    }

    #[test]
    fn thread_count_capped_by_the_buffer_limit() {
        let mut config = Config::default();
        config.concurrency = 64;
        let floor = min_thread_buffer();

        // A limit covering four worker buffers funds at most four
        // workers, however many chunks and cores are on offer.
        let threads = resolve_thread_count(&config, 1024, Some(floor * 4));
        assert_eq!(threads, 4);
        // ...and each of those four gets its full share, not less.
        assert_eq!(per_thread_share(Some(floor * 4), threads), Some(floor));

        // An unbounded budget leaves the other terms in charge.
        assert_eq!(resolve_thread_count(&config, 1024, None), 64);
    }

    #[test]
    fn a_limit_below_one_buffer_still_runs_one_thread() {
        let mut config = Config::default();
        config.concurrency = 8;
        let floor = min_thread_buffer();

        // Too small for even a single worker: we do not round the share
        // up to the floor (that would overshoot the caller's total), and
        // we do not resolve to zero threads. The backend rejects it with
        // a precise message instead.
        let threads = resolve_thread_count(&config, 1024, Some(floor / 4));
        assert_eq!(threads, 1);
        assert_eq!(per_thread_share(Some(floor / 4), threads), Some(floor / 4));
    }

    #[test]
    fn buffer_limit_is_split_across_workers() {
        // The contract users see: state a total, each worker gets 1/T.
        assert_eq!(per_thread_share(Some(64 * 1024), 4), Some(16 * 1024));
        assert_eq!(per_thread_share(Some(64 * 1024), 1), Some(64 * 1024));
        // Unbounded stays unbounded.
        assert_eq!(per_thread_share(None, 8), None);
    }

    #[test]
    fn the_thread_cap_ignores_the_ring_buffers_working_size() {
        // The floor is backend-independent on purpose: it has to be
        // computable before `Auto` has picked a backend, so no backend's
        // settings may feed into it. A huge ring-buffer working size is
        // that backend's own problem — `RingBuf::new` rejects it by name
        // — and must not silently change how many threads we run.
        let mut config = Config::default();
        config.concurrency = 8;
        let limit = Some(min_thread_buffer() * 8);

        let baseline = resolve_thread_count(&config, 1024, limit);
        config.io_backend =
            IoBackend::RingBuf(RingBufSettings::default().buffer_size(64 * 1024 * 1024));
        assert_eq!(resolve_thread_count(&config, 1024, limit), baseline);
    }

    #[test]
    fn auto_limit_is_half_of_ram() {
        let mut config = Config::default();
        config.io_buffer_limit = Some(0);
        let budget = resolve_io_buffer_limit(&config);

        match crate::sys::total_ram() {
            // Hosts that do not report RAM fall back to unbounded.
            None => assert_eq!(budget, None),
            Some(total_ram) => assert_eq!(budget, Some(total_ram / 2)),
        }

        // Explicit totals and `None` pass through as stated.
        config.io_buffer_limit = Some(4096);
        assert_eq!(resolve_io_buffer_limit(&config), Some(4096));
        config.io_buffer_limit = None;
        assert_eq!(resolve_io_buffer_limit(&config), None);
    }

    #[test]
    fn chunk_count_via_resolve() {
        // for_file picks chunk_size = 256 KiB for DFA, so an empty file
        // yields zero chunks regardless of input size 0.
        let r = ResolvedConfig::for_file(&Config::default(), 0).unwrap();
        assert_eq!(r.chunk_count, 0);
        assert_ne!(r.config.chunk_size, 0, "auto sentinel must be resolved");

        let mut config = Config::default();
        config.chunk_size = 100;
        assert_eq!(
            ResolvedConfig::for_file(&config, 200).unwrap().chunk_count,
            2
        );
        assert_eq!(
            ResolvedConfig::for_file(&config, 250).unwrap().chunk_count,
            3
        );
    }

    #[test]
    fn auto_backend_by_file_size() {
        let config = Config::default();
        let threshold = IN_MEMORY_FILE_SIZE_THRESHOLD;
        let backend = |fsize| resolve_io_backend(&config, fsize, None, 8);
        // Resolution consumes the `Some(0)` sentinel: no parser limit to
        // share out leaves the ring buffer unbounded.
        let ring = IoBackend::RingBuf(resolve_ringbuf_settings(
            RingBufSettings::default(),
            None,
            8,
        ));

        // Small files → InMemory.
        assert_eq!(backend(1), IoBackend::InMemory);
        assert_eq!(backend(threshold), IoBackend::InMemory);
        // Larger files → RingBuf: it matches or beats mmap across
        // dialects, and avoids mmap's COW-fault penalty when escape
        // compaction writes to the buffer (see resolve_io_backend).
        assert_eq!(backend(threshold + 1), ring);
    }

    #[test]
    fn auto_backend_ignores_the_memory_limit() {
        // Selection is a function of file size alone. `Auto` only
        // reaches for InMemory below the threshold, and thread_count is
        // capped by chunk_count, so the exposure it can create is a few
        // MiB — not worth letting a memory setting steer the choice.
        let config = Config::default();
        let tiny = Some(16 * 1024);
        assert_eq!(
            resolve_io_backend(&config, 64 * 1024, tiny, 8),
            IoBackend::InMemory
        );
    }

    #[test]
    fn the_tighter_cap_wins() {
        // Whichever side is tighter, in either direction.
        assert_eq!(resolve_buffer_limit(Some(4096), Some(8192)), Some(4096));
        assert_eq!(resolve_buffer_limit(Some(8192), Some(4096)), Some(4096));
        // Only the backend has an opinion: it stands alone.
        assert_eq!(resolve_buffer_limit(Some(4096), None), Some(4096));
        // Neither side bounds anything: unbounded.
        assert_eq!(resolve_buffer_limit(Some(0), None), None);
    }

    #[test]
    fn no_opinion_takes_the_share_whichever_way_it_is_spelled() {
        // `Some(0)` is the auto sentinel and `None` is treated the same,
        // so neither can widen past the parser-level limit — a
        // per-thread setting only ever tightens.
        for no_opinion in [Some(0), None] {
            assert_eq!(resolve_buffer_limit(no_opinion, Some(1024)), Some(1024));
        }
    }

    #[test]
    fn resolution_consumes_the_sentinel_and_leaves_the_working_size() {
        let stated = RingBufSettings::default().buffer_size(4096);
        assert_eq!(stated.buffer_limit, Some(0), "default is the sentinel");

        let resolved = resolve_ringbuf_settings(stated, Some(1024), 8);
        assert_eq!(resolved.buffer_size, 4096, "working size passes through");
        assert_eq!(resolved.buffer_limit, Some(1024));
    }

    #[test]
    fn the_share_reaches_the_selected_ring_buffer() {
        // The point of the payload: what comes out of resolution is a
        // backend carrying an effective per-thread cap, not a total that
        // something downstream has to remember to divide.
        let config = Config::default();
        let resolved =
            resolve_io_backend(&config, IN_MEMORY_FILE_SIZE_THRESHOLD + 1, Some(4096), 8);
        assert_eq!(
            resolved,
            IoBackend::RingBuf(
                RingBufSettings::default()
                    .buffer_size(4096)
                    .buffer_limit(Some(4096))
            )
        );

        // A caller's own per-thread bound composes with it as a min.
        let mut config = Config::default();
        config.io_backend = IoBackend::RingBuf(RingBufSettings::default().buffer_limit(Some(1024)));
        let resolved = resolve_io_backend(&config, 1, Some(4096), 8);
        assert_eq!(
            resolved,
            IoBackend::RingBuf(
                RingBufSettings::default()
                    .buffer_size(1024)
                    .buffer_limit(Some(1024))
            )
        );
    }
}