Skip to content

Optimization Log & Engineering Journal: Aegis

This log serves as a continuous record of the optimization phases performed on the Aegis packet engine. Every major performance enhancement must be documented here with corresponding benchmark data.


[Phase 1: Synchronization Refactoring]

  • Optimization: Replaced mutex-guarded ThreadSafeQueue with cache-aligned, lock-free SPSC ring buffers.
  • Reasoning: Mutex locks cause execution threads to sleep, prompting context switches. SPSC queues use atomic index pointers to pass references with minimal overhead.
  • Expected Gain: 5x - 10x latency reduction, higher thread scalability.
  • Measured Gain: TBD during execution phase benchmarks.
  • Regression Risk: Queue size is now bounded. Queue overflows must be handled via backpressure.

[Phase 2: Fast-Path Memory Allocation Elimination]

  • Optimization: Replaced heap-allocated std::vector<uint8_t> packet payloads with a pre-allocated PacketBufferPool array of 2048-byte frames.
  • Reasoning: Runtime memory allocations (malloc/new) take system-wide locks, introducing latency spikes. Using a pre-allocated pool reduces buffer acquisition time to a simple index pop.
  • Expected Gain: 3x throughput improvement, elimination of allocator latency spikes (p99 latency).
  • Measured Gain: TBD during execution phase benchmarks.
  • Regression Risk: Fixed capacity limit. If all buffers are allocated, the reader stalls or drops incoming packets.

[Phase 3: Zero-Copy Parsing and String Elimination]

  • Optimization: Removed IP/MAC string formatting inside PacketParser and replaced std::string fields with raw numeric equivalents (uint32_t, std::array). Replaced payload extraction with std::string_view.
  • Reasoning: Creating string objects on every packet parsing triggers memory allocations and string copying. Matching on raw numbers is performed in 1-2 CPU instructions.
  • Expected Gain: 4x parsing speedup.
  • Measured Gain: TBD during execution phase benchmarks.
  • Regression Risk: Low. Logging and reporting must format numbers back to strings, which adds slight overhead outside the hot path.

[Phase 4: Flow Table Heap-to-Flat Redesign]

  • Optimization: Replaced std::unordered_map with a flat open-addressed hash table with pre-allocated slots.
  • Reasoning: std::unordered_map uses pointer-linked buckets, causing CPU cache misses as it traverses memory blocks. Contiguous memory ensures cache prefetching runs optimally.
  • Expected Gain: 2x lookup speedup under high flow load.
  • Measured Gain: TBD during execution phase benchmarks.
  • Regression Risk: Collision resolution becomes slower if the table exceeds its load factor (e.g. > 70%).

[Phase 5: Output Polling Writer Setup]

  • Optimization: Swapped the shared worker-to-writer queue for an array of worker-to-writer SPSC queues. The writer polls these queues in a round-robin loop.
  • Reasoning: An MPSC queue requires atomic CAS loops, causing cache line invalidation and bus contention. A polling writer eliminates producer-producer contention.
  • Expected Gain: Cleaner thread isolation, improved write throughput.
  • Measured Gain: TBD during execution phase benchmarks.
  • Regression Risk: Increased CPU usage on the writer thread due to spin-polling when inactive. (Can be mitigated with yielding).