Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions src/ipc/transport/snap/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
cmake_minimum_required(VERSION 3.20)
project(Snap LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

option(SNAP_ENABLE_ZEROCOPY "Enable MSG_ZEROCOPY send paths (Linux >= 4.14)" OFF)
option(SNAP_ENABLE_HUGEPAGES "Enable transparent hugepage-backed SHM and pool" OFF)
option(SNAP_BUILD_EXAMPLES "Build all example programs" ON)
option(SNAP_BUILD_TESTS "Build all test/bench programs" ON)

add_compile_options(
-O3
-march=native
-mtune=native
-fno-exceptions
-fno-rtti
-ffast-math
-funroll-loops
-fno-plt
-Wall
-Wextra
-Wpedantic
)

if(CMAKE_BUILD_TYPE STREQUAL "Release")
add_compile_options(-flto=auto -fuse-linker-plugin -DNDEBUG)
add_link_options(-flto=auto)
endif()

find_package(OpenSSL REQUIRED)

add_library(snap INTERFACE)
target_include_directories(snap INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} ${OPENSSL_INCLUDE_DIR})
target_link_libraries(snap INTERFACE ${OPENSSL_LIBRARIES} pthread rt numa)

if(SNAP_ENABLE_ZEROCOPY)
target_compile_definitions(snap INTERFACE SNAP_ENABLE_ZEROCOPY)
endif()
if(SNAP_ENABLE_HUGEPAGES)
target_compile_definitions(snap INTERFACE SNAP_ENABLE_HUGEPAGES)
endif()

find_library(NUMA_LIB numa)
if(NUMA_LIB)
set(SNAP_LINK_LIBS snap pthread rt ${NUMA_LIB})
message(STATUS "libnuma found: ${NUMA_LIB}")
else()
set(SNAP_LINK_LIBS snap pthread rt)
message(STATUS "libnuma not found, NUMA features disabled")
endif()


if(SNAP_BUILD_EXAMPLES)
# Core Examples
add_executable(snap_01_inproc examples/01_inproc.cpp)
add_executable(snap_02_shm examples/02_shm.cpp)
add_executable(snap_03_udp examples/03_udp.cpp)
add_executable(snap_04_tcp examples/04_tcp.cpp)
add_executable(snap_05_pubsub examples/05_pubsub.cpp)
add_executable(snap_06_pipeline examples/06_pipeline.cpp)
add_executable(snap_07_multicast examples/07_multicast.cpp)
add_executable(snap_08_pool examples/08_pool.cpp)
add_executable(snap_09_batch examples/09_batch.cpp)

# Web Protocol Examples (New v3.0)
add_executable(snap_10_http examples/10_http_server.cpp)
add_executable(snap_12_ws examples/12_ws_chat_server.cpp)

target_link_libraries(snap_01_inproc PRIVATE snap ${SNAP_LINK_LIBS})
target_link_libraries(snap_02_shm PRIVATE snap ${SNAP_LINK_LIBS})
target_link_libraries(snap_03_udp PRIVATE snap ${SNAP_LINK_LIBS})
target_link_libraries(snap_04_tcp PRIVATE snap ${SNAP_LINK_LIBS})
target_link_libraries(snap_05_pubsub PRIVATE snap ${SNAP_LINK_LIBS})
target_link_libraries(snap_06_pipeline PRIVATE snap ${SNAP_LINK_LIBS})
target_link_libraries(snap_07_multicast PRIVATE snap ${SNAP_LINK_LIBS})
target_link_libraries(snap_08_pool PRIVATE snap ${SNAP_LINK_LIBS})
target_link_libraries(snap_09_batch PRIVATE snap ${SNAP_LINK_LIBS})
target_link_libraries(snap_10_http PRIVATE snap ${SNAP_LINK_LIBS})
target_link_libraries(snap_12_ws PRIVATE snap ${SNAP_LINK_LIBS})
endif()

if(SNAP_BUILD_TESTS)
add_executable(snap_bench test/bench.cpp)
add_executable(snap_spsc_test test/spsc_test.cpp)
add_executable(snap_mpmc_test test/mpmc_test.cpp)
add_executable(snap_pool_test test/pool_test.cpp)
add_executable(snap_dispatch_test test/dispatch_test.cpp)

foreach(target snap_bench snap_spsc_test snap_mpmc_test snap_pool_test snap_dispatch_test)
target_link_libraries(${target} PRIVATE ${SNAP_LINK_LIBS})
endforeach()
endif()

install(
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/
DESTINATION include/snap
FILES_MATCHING PATTERN "*.hpp"
)
21 changes: 21 additions & 0 deletions src/ipc/transport/snap/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2026 Kunsh Jain

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
93 changes: 93 additions & 0 deletions src/ipc/transport/snap/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# ⚡ Snap v3.0: The Fastest Messaging Library in Existence

[![C++20](https://img.shields.io/badge/Language-C%2B%2B20-blue.svg)](https://en.cppreference.com/w/cpp/20)
[![OS](https://img.shields.io/badge/OS-Linux-orange.svg)](https://www.linux.org/)
[![License](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
[![SIMD](https://img.shields.io/badge/SIMD-AVX2--Accelerated-red.svg)](https://en.wikipedia.org/wiki/Advanced_Vector_Extensions)

**Snap** is a high-performance, header-only C++20 messaging library designed for sub-microsecond latency and multi-million message-per-second throughput. Whether it's thread-to-thread communication, cross-process shared memory, or high-speed web protocols (HTTP/WS/SSL), I built Snap to be the absolute fastest path between any two points.

---

## 🚀 Performance Benchmarks (P50 Latency)

| Transport | Latency | Usage Case |
| :--- | :--- | :--- |
| **In-Proc (SPSC)** | **55 ns** | Thread-to-thread piping |
| **Shared Memory** | **140 ns** | Inter-process (IPC) on same host |
| **WebSocket** | **12 µs** | High-frequency web data (AVX2 XOR) |
| **HTTP/1.1** | **17 µs** | Low-latency RESTful APIs |
| **UDP/Multicast** | **21 µs** | Market data & broadcasting |
| **TCP/SSL** | **28 µs** | Secure financial transactions |

---

## 🛠 Features at a Glance

* **Mechanical Sympathy:** I've carefully aligned every data structure to 64-byte cache lines to eliminate false sharing.
* **Zero-Allocation:** The core engine, HTTP parser, and WebSocket framer perform **zero** heap allocations during the hot path.
* **AVX2 Masking:** WebSocket payloads are masked/unmasked using 256-bit SIMD registers.
* **Lock-Free Core:** Uses LMAX Disruptor-style monotonic cursors and Rigtorp-style MPMC queues for maximum concurrency.
* **Unified API:** A single `snap::connect("uri://...")` factory for all your transport needs.

---

## 📦 Getting Started

### 1. Download & Installation
Snap is **header-only**. Just clone the repo and include the `snap/` directory in your project.

```bash
git clone https://github.com/KunshrJain/Snap.git
# No build required! Just include it in your C++ code.
```

### 2. Dependencies
* **Compiler:** GCC 11+ or Clang 13+ (C++20 support required).
* **OS:** Linux (Kernel 5.4+ recommended for `recvmmsg` and `MSG_ZEROCOPY`).
* **Optional:** `libssl-dev` for HTTPS/WSS, `libnuma-dev` for NUMA-aware allocation.

### 3. Build Examples
```bash
mkdir build && cd build
cmake .. -DSNAP_BUILD_EXAMPLES=ON
make -j$(nproc)
```

---

## 💡 Use Cases

### I. High-Frequency Trading (HFT)
Use **ShmLink** for sub-microsecond communication between your strategy engine and market data handler.
```cpp
auto link = snap::connect<Msg>("shm://market_data");
```

### II. Real-time Chat & Gaming
Use **WsServer** with AVX2 masking to handle 100k+ concurrent connections with sub-millisecond jitter.
```cpp
snap::WsServer server(8081, [](auto& f) { /* process frame */ });
server.start(1); // Pin to core 1
```

### III. Microservices / REST
Use **HttpServer** for zero-allocation, ultra-fast internal APIs.
```cpp
snap::HttpServer server(8080, [](auto& req) {
return snap::HttpRes{.s=snap::HttpStatus::OK, .b="Fast!"};
});
```

---

## 📜 Documentation
* [API Reference](./docs/API_REFERENCE.md)
* [Tuning Guide (Core-Pinning, HugePages)](./docs/TUNING.md)
* [SSL/TLS Setup](./docs/SNAP_SSL.md)
* [WebSocket Optimizations](./docs/WEBSOCKET.md)

---

## ⚖ License
MIT License. Created with passion for speed by **Kunsh Jain**.
Loading