惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
阮一峰的网络日志
阮一峰的网络日志
Last Week in AI
Last Week in AI
V
Visual Studio Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
The Cloudflare Blog
博客园 - 【当耐特】
博客园 - 叶小钗
量子位
博客园 - 聂微东
S
SegmentFault 最新的问题
美团技术团队
Hugging Face - Blog
Hugging Face - Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
宝玉的分享
宝玉的分享
小众软件
小众软件
罗磊的独立博客
有赞技术团队
有赞技术团队
Stack Overflow Blog
Stack Overflow Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
How I Built a C++ Market Data Parser That Processes 5.5 M...
Piyush Kumar · 2026-04-26 · via DEV Community

Piyush Kumar

Architectural Patterns for a 5.5M msgs/sec Market Data Parser in C++20

Processing raw market data feeds (like NASDAQ ITCH 5.0 or NSE FO) requires strict adherence to low-latency principles. Recently, I built a C++20 parser to ingest these feeds, normalize them, and manage a Limit Order Book (LOB).

By strictly controlling memory allocation and maximizing CPU cache locality, the parser achieves a throughput of ~5.5 Million messages/sec (169 MB/s) on an Apple Silicon (M-Series) processor, with a P50 latency of 84 nanoseconds per message.

This write-up covers the three primary technical patterns used to achieve this throughput.

High-Performance Market Data Parser

A production-grade, low-latency C++ market data engine designed for High-Frequency Trading (HFT) applications. It ingests raw exchange feeds (NASDAQ ITCH 5.0, NSE FO), standardizes them efficiently, and maintains a clean Limit Order Book (LOB) State of the World.

Current Benchmarks

Hardware Environment: Apple Silicon M-Series (Tested on 10-core CPU)

Component Throughput Items per Second Description
NSE Parser ~381 MB/s ~247k msgs/sec Zero-Copy Parser
NASDAQ ITCH ~169 MB/s ~5.5 Million msgs/sec Zero-Copy Parser
Order Book (Add) - ~9M to 22M ops/sec Core Engine insertion latency (varies by book size)
Order Book (Match) - ~236M to 291M ops/sec Core Engine exact match latency

Achieved via direct buffer casting, custom memory resources (PMR), and branch-free endian conversion.

End-to-End Execution (Real-World Data)

Processing a full 11.24 GB historical NASDAQ ITCH 5.0 file (01302019.NASDAQ_ITCH50):

  • Total Messages Parsed: 368,366,634
  • Execution Time: 97.48 seconds
  • Throughput: 3.77 Million…

1. Zero-Copy Parsing and Direct Buffer Casting

In high-throughput systems, copying data from an I/O buffer into application-level structs is prohibitively expensive.

To eliminate data copying during file ingestion, the parser uses memory-mapped files (mmap). This maps the entire binary PCAP/exchange file directly into the application's virtual address space.

Instead of parsing fields sequentially, we rely on direct buffer casting. Because exchange protocols like ITCH define strict, fixed-length binary message formats, we can define packed C++ structs that perfectly mirror the wire protocol.

// Example of direct casting from the mapped memory pointer
const auto* message = reinterpret_cast<const ItchAddOrderMessage*>(mapped_ptr);

Enter fullscreen mode Exit fullscreen mode

For string fields (like 8-byte stock tickers), the parser uses std::string_view. This avoids heap allocations entirely by simply wrapping a pointer to the mapped memory and a length.

Finally, to prevent instruction pipeline stalls during endianness conversion (ITCH uses Big-Endian), the parser relies on branch-free bitwise operations (__builtin_bswap or std::byteswap in C++23) to swap byte orders in registers.

2. Eliminating Heap Allocations with std::pmr

Updating the Limit Order Book requires generating standard Order objects. Using standard new or malloc here causes unacceptable non-deterministic latency due to OS context switching and heap fragmentation.

To bypass the standard heap, the parser uses Polymorphic Memory Resources (PMR) introduced in C++17, specifically the std::pmr::monotonic_buffer_resource.

This acts as an Arena Allocator. At initialization, we allocate a massive, contiguous block of memory.

std::array<std::byte, 1024 * 1024 * 500> buffer; // 500MB pre-allocated arena
std::pmr::monotonic_buffer_resource pool{buffer.data(), buffer.size()};

Enter fullscreen mode Exit fullscreen mode

When a new order arrives, memory is "allocated" simply by bumping a pointer forward within this arena. Deallocation is a no-op; the entire arena is simply discarded or reset at the end of the trading session. This guarantees O(1) allocation time and ensures that newly created objects reside in contiguous memory addresses.

3. Cache-Aligned Hash Maps (DenseMap)

When processing an order execution or cancellation, the engine must look up the original order by its unique Order ID.

Standard std::unordered_map uses separate chaining (arrays of linked lists). Traversing a linked list destroys CPU cache locality. A single L1 cache miss (fetching from main memory) can cost ~100ns, which is longer than our entire target P50 latency.

To solve this, the parser implements DenseMap, a custom open-addressing hash map.

In open-addressing, all key-value pairs are stored inline within a single flat std::vector. When a hash collision occurs, the map linearly probes the adjacent memory slots.

Because modern CPUs fetch memory in 64-byte cache lines, a linear probe almost guarantees that the probed memory address is already sitting in the L1 or L2 cache. This transforms a potentially expensive main-memory fetch into an ultra-fast L1 cache hit, keeping the instruction pipeline saturated.


Conclusion

Maximizing single-thread throughput is largely an exercise in mechanical sympathy. By utilizing memory-mapping, arena allocators (std::pmr), and cache-friendly data structures, you can bypass the OS and the heap entirely on the hot path.

You can view the full implementation and run the benchmarks yourself here: GitHub Repository.

Feedback on the architecture or suggestions for further micro-optimizations are welcome.