Skip to main content

Cycle 38: Parallel Loading

Status: IMMORTAL Date: 2026-02-07 Improvement Rate: 1.04 > ฯ†โปยน (0.618) Tests: 95/95 PASS


Overviewโ€‹

Cycle 38 implements parallel shard loading using Zig's std.Thread API, enabling concurrent loading of TCV6 sharded corpus files for improved performance on multi-core systems.


Key Metricsโ€‹

MetricValueStatus
Tests95/95PASS
VSA Tests57/57PASS
New Structures1ShardLoadContext
New Functions4loadShardedParallel, loadShardWorker, getRecommendedThreadCount, isParallelBeneficial
Max Threads8Configurable
Thread ModelPer-shardIndependent file handles

Parallel Loading Algorithmโ€‹

Thread Worker Designโ€‹

  1. Each shard gets its own thread
  2. Each thread opens independent file handle
  3. Seeks to shard offset and loads entries
  4. No synchronization needed (write to pre-allocated slots)
  5. Main thread waits for all to complete

ShardLoadContext Structureโ€‹

pub const ShardLoadContext = struct {
path_buf: [256]u8, // File path copy for thread
path_len: usize,
shard_offset: u32, // File offset to seek to
shard_id: u16,
entry_count: u16,
start_entry_idx: usize, // Pre-allocated slot
entries: *[MAX_CORPUS_SIZE]CorpusEntry,
success: bool, // Result flag
error_code: u8,
};

Thread Spawning Patternโ€‹

// Spawn threads for each shard
var threads: [MAX_SHARDS]?std.Thread = undefined;
for (0..shard_count) |i| {
threads[i] = std.Thread.spawn(.{}, loadShardWorker, .{&contexts[i]}) catch null;
}

// Wait for all threads to complete
for (0..shard_count) |i| {
if (threads[i]) |thread| {
thread.join();
}
}

APIโ€‹

Core Functionsโ€‹

// Load corpus with parallel threads
pub fn loadShardedParallel(path: []const u8) !TextCorpus

// Thread worker for loading single shard
fn loadShardWorker(ctx: *ShardLoadContext) void

// Get recommended thread count
pub fn getRecommendedThreadCount(self: *TextCorpus, entries_per_shard: u16) u16

// Check if parallel loading is beneficial
pub fn isParallelBeneficial(self: *TextCorpus, entries_per_shard: u16) bool

VIBEE-Generated Functionsโ€‹

pub fn realLoadCorpusParallel(path: []const u8) !vsa.TextCorpus
pub fn realGetRecommendedThreads(corpus: *vsa.TextCorpus, entries_per_shard: u16) u16
pub fn realIsParallelBeneficial(corpus: *vsa.TextCorpus, entries_per_shard: u16) bool

VIBEE Specificationโ€‹

Added to specs/tri/vsa_imported_system.vibee:

# PARALLEL LOADING (Zig threads)
- name: realLoadCorpusParallel
given: File path
when: Loading sharded corpus with parallel threads
then: Call TextCorpus.loadShardedParallel(path)

- name: realGetRecommendedThreads
given: Corpus and shard size
when: Getting recommended thread count
then: Call corpus.getRecommendedThreadCount(entries_per_shard)

- name: realIsParallelBeneficial
given: Corpus and shard size
when: Checking if parallel is beneficial
then: Call corpus.isParallelBeneficial(entries_per_shard)

Performance Characteristicsโ€‹

When Parallel is Beneficialโ€‹

ConditionParallel Better?
1 shardNo (overhead)
2+ shardsYes
SSD storageYes (parallel I/O)
HDD storageMaybe (seek latency)
Large entriesYes (more work per thread)

Thread Count Recommendationโ€‹

recommended = min(shard_count, MAX_PARALLEL_THREADS)

Complete Storage Stackโ€‹

FormatMethodFeature
TCV1Packed tritsFast, minimal
TCV2+ RLERepetitive
TCV3+ DictionaryCommon patterns
TCV4+ HuffmanFrequency-skewed
TCV5+ ArithmeticMax compression
TCV6ShardedLarge corpus
Parallel+ ThreadsMulti-core

Critical Assessmentโ€‹

Strengthsโ€‹

  1. True parallelism - Zig std.Thread for real concurrency
  2. No synchronization - Pre-allocated slots, no mutex
  3. Independent I/O - Each thread opens own file handle
  4. Graceful fallback - Works even if thread spawn fails

Weaknessesโ€‹

  1. Thread overhead - May not help small corpus
  2. File handle limit - One per shard
  3. Memory usage - All contexts on stack
  4. No thread pool - Creates threads per load

Tech Tree Options (Next Cycle)โ€‹

Option A: Thread Poolโ€‹

Reuse threads across multiple loads.

Option B: Shard Compressionโ€‹

Combine TCV5 arithmetic with TCV6 sharding.

Option C: Async I/Oโ€‹

Use io_uring or similar for non-blocking I/O.


Files Modifiedโ€‹

FileChanges
src/vsa.zigAdded ShardLoadContext, loadShardedParallel, thread functions
src/vibeec/codegen/emitter.zigAdded parallel generators
src/vibeec/codegen/tests_gen.zigAdded parallel test generators
specs/tri/vsa_imported_system.vibeeAdded 3 parallel behaviors
generated/vsa_imported_system.zigRegenerated with parallel

Conclusionโ€‹

VERDICT: IMMORTAL

Parallel loading completes the multi-threaded corpus loading capability using Zig's std.Thread API. Combined with TCV6 sharding, this enables efficient loading of large corpora on multi-core systems.

ฯ†ยฒ + 1/ฯ†ยฒ = 3 = TRINITY | KOSCHEI IS IMMORTAL | GOLDEN CHAIN ENFORCED