Skip to content

Instantly share code, notes, and snippets.

@corporatepiyush
Last active July 23, 2026 03:56
Show Gist options
  • Select an option

  • Save corporatepiyush/6a1a86845d43314a189d02ca278e1e10 to your computer and use it in GitHub Desktop.

Select an option

Save corporatepiyush/6a1a86845d43314a189d02ca278e1e10 to your computer and use it in GitHub Desktop.
C17 Optimization Guide

I. Hardware, DMA & Architecture-Specific I/O (AMD64 vs. ARMv8/v9)

  • PCIe NUMA Affinity Pinning (AMD64/ARMv8): Pin network and storage worker threads directly to the CPU socket and core complex physically wired to the PCIe Root Complex handling the target device (/sys/class/net/<iface>/device/numa_node). This eliminates cross-socket UPI/QPI and CXL interconnect latency under high-throughput I/O.
  • Intel DDIO & AMD TPH (TLP Processing Hint) / L3 Direct Delivery: On x86_64, tune Intel Data Direct I/O (DDIO) or PCIe TLP Processing Hints (TPH) so that NIC/NVMe RX/TX ring descriptors and packet buffers land directly in designated CPU L3 cache lines, avoiding L1/L2 cache pollution while preventing PCIe-to-DRAM memory bottlenecks.
  • ARMv8/v9 Page Size & Folio Alignment (16KB vs. 64KB vs. 4KB): On Apple Silicon (16KB native page size) or Enterprise ARM64 Linux (4KB/64KB pages), align ring buffer memory allocations and kernel page-pinning boundaries (mmap, io_uring mapped rings, UMEM) strictly to native kernel folio boundaries to eliminate silent page-fault translation overhead and TLB thrashing.
  • ARMv8.3+ Acquire-Release Primitives (LDAPR/STLR) & Vector Offloads: Utilize ARMv8.3 LDAPR (Load-Acquire RCpc) instructions in lock-free ring buffers to eliminate explicit DMB ISH (Data Memory Barrier) pipeline stalls. Leverage ARM SVE2/SME and AVX-512/AVX10 vector units for high-bandwidth user-space buffer copies.
  • ARMv8/v9 Explicit Cache Line Maintenance (DC CVAP / DC CIVAC): Issue point-of-persistence and point-of-coherency cache flush operations (DC CVAP for persistent memory/CXL DAX, DC CIVAC for non-coherent DMA) on user-space buffers (e.g., SPDK/DPDK) to enforce hardware memory ordering without forcing full CPU execution fences.
  • NVMe/NIC Doorbell Memory Mapping (Write-Combining): Map MMIO doorbell registers using Write-Combining (pgprot_writecombine on Linux, ARM64_MAIR_ATTR_NC on ARM) to coalesce consecutive 32-bit/64-bit register writes into single PCIe Transaction Layer Packages (TLPs).
  • PCIe Max Payload Size (MPS) & Max Read Request Size (MRRS) Alignment: Align direct DMA memory buffers in user space to the motherboard's active PCIe MPS/MRRS boundary (typically 128, 256, or 512 bytes) to optimize PCIe transaction bus packing and minimize framing overhead.

II. Linux Kernel Deep Mechanics (Linux 6.18 LTS)

  • io_uring Fixed File Registration (IORING_REGISTER_FILES): Register file and socket descriptors upfront with the ring context to bypass atomic reference counting (fget/fput) and VFS file table spinlock lookups on every I/O submission.
  • io_uring Mapped Buffer Rings (IORING_REGISTER_BUF_RING): Replaces fixed static buffers for network I/O. Userspace and kernel share a lock-free circular ring where the kernel dynamically consumes buffer slots upon packet arrival, eliminating pre-allocated memory inflation under massive concurrent connection counts.
  • io_uring Zero-Syscall & Taskrun Deferral (IORING_SETUP_SQPOLL & IORING_SETUP_DEFER_TASKRUN): Combine SQPOLL kernel thread submission with IORING_SETUP_DEFER_TASKRUN to restrict completion processing strictly to the managing thread. This eliminates cross-core Inter-Processor Interrupts (IPIs) and yields a 100% syscall-free, single-threaded hot loop.
  • SLUB Per-CPU "Sheaves" Allocation: Linux 6.18 introduces "sheaves"—a per-CPU slab allocation cache layer. Fast-path kernel objects (VMA nodes, socket structures, sk_buff metadata) are allocated and freed from local per-core sheaves, removing CPU lock contention on memory allocation during heavy I/O workloads.
  • Zero-Copy Kernel Bypass via AF_XDP & Device Memory TCP: Utilize AF_XDP sockets with UMEM memory rings to bypass the TCP/IP stack (sk_buff) entirely for raw Ethernet frames, or use Device Memory TCP (DMTCP) to stream incoming TCP payloads directly into dedicated DMABUF region allocations while metadata lands separately in normal memory.
  • Driver-Level Busy Polling (SO_BUSY_POLL & SO_PREFER_BUSY_POLL): Instruct the socket layer to poll the network device driver's RX queue directly from the application thread, eliminating hardware interrupt context-switching latency.
  • Network Core Affinity via SO_INCOMING_CPU & eBPF: Bind thread execution dynamically to the exact CPU core processing NAPI hardware interrupts using SO_INCOMING_CPU combined with SO_ATTACH_REUSEPORT_EBPF to guarantee total CPU cache locality.
  • Epoll Precision & Thundering Herd Mitigation (epoll_pwait2 & EPOLLEXCLUSIVE): Use epoll_pwait2() for nanosecond-precision timeout structures (struct timespec64) and attach multi-threaded listening sockets using EPOLLEXCLUSIVE to ensure only a single worker thread wakes per incoming connection.
  • Untorn Hardware Atomic Writes (RWF_ATOMIC & STATX_WRITE_ATOMIC): Query block device atomic write capabilities via statx() (STATX_WRITE_ATOMIC) and issue pwritev2() with RWF_ATOMIC (supported for both Direct and Buffered I/O in 6.18) to perform guaranteed untorn 16KB–64KB writes, allowing database engines (e.g., PostgreSQL, MySQL) to safely turn off double-write logging.
  • Direct I/O Alignment Queries (statx with STATX_DIOALIGN): Query statx() using STATX_DIOALIGN to obtain exact memory buffer and file offset alignment rules (stx_dio_mem_align, stx_dio_offset_align) natively without issuing legacy block ioctl(BLKSSZGET) calls or forcing inode synchronization (AT_STATX_DONT_SYNC).

III. macOS (Darwin / Apple Silicon) Low-Level I/O Mechanics

  • Darwin Unified Buffer Cache Bypass (F_NOCACHE): Execute fcntl(fd, F_NOCACHE, 1) on macOS file descriptors to bypass the Unified Buffer Cache (UBC) for high-throughput streaming, preventing system-wide SLC/L3 cache eviction.
  • Durable Storage Flushes (F_FULLFSYNC vs. fdatasync): Standard fsync() and fdatasync() on macOS only flush data to the SSD controller's volatile RAM cache; non-volatile storage durability requires calling fcntl(fd, F_FULLFSYNC) to force an explicit physical flash cell cache flush.
  • Contiguous APFS Space Allocation (F_PREALLOCATE): Reserve contiguous physical disk blocks on APFS using fcntl(fd, F_PREALLOCATE, ...) with ALLOCATECONTIG to prevent Copy-On-Write (COW) extent fragmentation during heavy random write streams.
  • Mach Port Multiplexing in kqueue (EVFILT_MACHPORT): Register Mach Ports directly into the BSD kqueue event loop to handle inter-process communication (IPC), system events, and socket descriptors inside a single unified polling thread.
  • QoS-Driven Core Scheduling (pthread_set_qos_class_self_np): Explicitly tag I/O polling threads with QOS_CLASS_USER_INTERACTIVE or QOS_CLASS_USER_INITIATED to guarantee macOS schedules hot network/disk event loops onto Apple Silicon Performance (P) cores rather than Efficiency (E) cores.
  • Userland Network Stack Acceleration (Network.framework & SO_TRAFFIC_CLASS): Transition performance-critical network code from legacy raw BSD sockets to nw_connection_t pipelines in Network.framework, bypassing BSD kernel context switching and routing TLS/crypto workloads directly to Apple Silicon dedicated hardware crypto blocks.

IV. FreeBSD Kernel & Network Stack Optimization (FreeBSD 14.x / 15.0)

  • Atomic One-Shot Event Handling (EV_DISPATCH in kqueue): Combine EV_DISPATCH and EV_DISABLE flags in kqueue to automatically disable event notifications upon delivery, eliminating follow-up kevent() modification overhead in multi-threaded worker pools.
  • Connection-Count Load Balancing (SO_REUSEPORT_LB): Utilize FreeBSD’s SO_REUSEPORT_LB socket option to achieve true connection-count load distribution across acceptor threads (unlike Linux's 4-tuple hash-based SO_REUSEPORT).
  • Capability-Based Descriptor Sandboxing (Capsicum): Transition worker processes into capability mode via cap_enter() and restrict file descriptors with cap_rights_limit() to eliminate OS privilege verification and global path resolution overhead on fast-path file operations.
  • **Zero-Copy KTLS & sendfile()**: Combine FreeBSD Kernel TLS (KTLS) with sendfile(2) (kern.ipc.zero_copy_send=1) to stream data directly from page cache or the OpenZFS Adaptive Replacement Cache (ARC) through hardware NIC TLS offload engines without touching user memory.
  • Immediate Page Eviction (POSIX_FADV_NOREUSE): Issue posix_fadvise(fd, offset, len, POSIX_FADV_NOREUSE) on FreeBSD to signal the VM system to free page cache buffers immediately following single-pass read/write operations.
  • Kernel Accept Filters (accf_http / accf_data): Attach FreeBSD Accept Filters (setsockopt(..., SOL_SOCKET, SO_ACCEPTFILTER, ...) like accf_http) to hold incoming TCP connections in kernel space until valid application payload data arrives, shielding user-space event loops from connection probes.
  • OpenZFS Direct I/O (O_DIRECT): Native OpenZFS on FreeBSD supports direct, unbuffered storage I/O, allowing database systems to bypass ZFS ARC double-buffering for unbuffered sequential or random reads and writes.

V. Advanced Socket, Transport & Protocol Stack Mechanics

  • Disable Nagle's Algorithm (TCP_NODELAY): Set TCP_NODELAY on latency-sensitive sockets to force immediate packet transmission without buffering micro-payloads into MSS-sized chunks.
  • Explicit Payload Coalescing (TCP_CORK / TCP_NOPUSH): Enable TCP_CORK (Linux) or TCP_NOPUSH (FreeBSD/macOS) prior to streaming multi-part HTTP/framing headers and body segments to force the transport stack to combine data into full Maximum Segment Size (MSS) packets before transmission.
  • Fast-Feedback Congestion Control (AccECN / RFC 9768): Enable Accurate ECN (net.ipv4.tcp_ecn = 3 in Linux 6.18) to feed back accurate Congestion Experienced (CE) mark counts per RTT, allowing TCP senders to fine-tune congestion windows without drastic rate drops.
  • Transport-Layer Encryption Offloading (Google PSP & TCP_QUICKACK): Utilize Google PSP (PSP Encryption over TCP) in Linux 6.18 for data-center hardware-offloaded TCP encryption. Set TCP_QUICKACK after receiving packet bursts to bypass the default 200ms delayed ACK timer and reset the remote peer's congestion window instantly.
  • Vectorized Datagram Batching (recvmmsg / sendmmsg): Receive or transmit arrays of UDP datagrams in a single system call using recvmmsg() and sendmmsg() to minimize context switching and instruction cache misses under high packet rates.
  • UDP Generic Offloads (UDP_GRO & UDP_SEGMENT): Enable UDP Segment Offload (UDP_SEGMENT) on TX and Generic Receive Offload (UDP_GRO) on RX to push large (up to 64KB) UDP datagram chunks to the NIC driver, delegating PMTU fragmentation to hardware.
  • Zero-Copy Transmit (MSG_ZEROCOPY & IORING_OP_SEND_ZC): Pass MSG_ZEROCOPY or issue IORING_OP_SEND_ZC to pin user-space pages and perform direct DMA transmission, tracking completion asynchronously via error queues or io_uring CQEs.
  • Bandwidth-Delay Product Overrides (SO_RCVBUF & SO_SNDBUF): Override kernel socket buffer auto-tuning on fixed low-latency or high-throughput connections by explicitly setting buffer sizes to match the precise Bandwidth-Delay Product (BDP) of the link.
  • TCP Fast Open (TCP_FASTOPEN / TCP_FASTOPEN_CONNECT): Enable TCP_FASTOPEN to transmit initial payload data directly inside the TCP SYN packet, eliminating 1 RTT during connection handshakes.
  • Hardware-Paced Rate Limiting (SO_MAX_PACING_RATE): Set SO_MAX_PACING_RATE on Linux sockets to delegate packet pacing directly to Fair Queueing (FQ) traffic control schedulers or NIC hardware, preventing micro-burst packet drops and bufferbloat.

VI. Direct Block Storage, VFS & File System Mechanics

  • Unbuffered Direct Disk Access (O_DIRECT / O_DIRECTIO): Open files with O_DIRECT to bypass the OS page cache entirely, preventing double-buffering and L3 cache pollution for custom database engines.
  • Strict Sector & Memory Alignment: Ensure memory buffers, file offsets, and I/O sizes passed to O_DIRECT calls are strictly aligned to the hardware sector and memory boundaries exposed via statx() (STATX_DIOALIGN), preventing silent kernel fallbacks to buffered slow-paths.
  • Extent Pre-Allocation (fallocate / posix_fallocate): Reserve contiguous physical disk sectors before issuing write streams using fallocate() to prevent dynamic filesystem allocation locks and metadata write stalls.
  • Sparse File Hole Punching (FALLOC_FL_PUNCH_HOLE): Punch holes in sparse files using fallocate(..., FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE) to release physical blocks back to the OS without altering file metadata length.
  • Directory Descriptor Caching (openat & openat2): Cache directory file descriptors (dfd) and issue relative file operations using openat2() with RESOLVE_NO_SYMLINKS to skip redundant VFS root-to-leaf path resolution and symlink lock contention.
  • Persistent Memory & CXL Caching (dm-pcache): In Linux 6.18+, deploy the dm-pcache device-mapper target to place persistent memory (CXL 2.0/3.0 or DAX devices) as a high-throughput, low-latency persistent write/read cache layer in front of traditional NVMe arrays.
  • Asynchronous Page Prefetching (POSIX_FADV_WILLNEED): Issue posix_fadvise(..., POSIX_FADV_WILLNEED) ahead of sequential access patterns to trigger background OS kernel block prefetching.
  • Suppress Metadata Access Timestamps (O_NOATIME): Open file descriptors with O_NOATIME (or mount storage filesystems with noatime) to prevent the kernel from generating disk write operations to update atime fields during reads.
  • User-Space Storage Drivers (SPDK / VFIO): Bypass the kernel VFS and block layer completely for ultra-low-latency NVMe access by running the Storage Performance Development Kit (SPDK) over PCIe VFIO bindings.
  • Atomic Descriptor Duplication (dup3 with O_CLOEXEC): Use dup3() or fcntl(F_DUPFD_CLOEXEC) to manage file descriptor tables atomically, preventing descriptor leaks and locking across multi-threaded execution environments.

The definitive master list of low-level optimization techniques for ISO C17 programming. It consolidates CPU pipeline mechanics, SIMD vectorization, cache topology, custom memory allocators, lock-free concurrency, and compiler tooling


I. C17 Core Language, Types & Compiler Directives

  1. Annotate Non-Overlapping Pointers with restrict: Tell the compiler that pointers do not alias. This unlocks aggressive loop reordering, register caching, and auto-vectorization.
  2. Apply const Aggressively: Marking variables and parameters const allows compilers to place lookup data in read-only memory (.rodata) and perform constant propagation.
  3. Guide Branch Prediction (__builtin_expect): Annotate likely/unlikely execution branches (e.g., error checks) to push cold code out of the hot instruction prefetch path.
  4. Mark Non-Returning Functions (_Noreturn / [[noreturn]]): Annotate fatal exit handlers so the compiler can prune unreachable dead-code paths and skip stack-saving prologues.
  5. Silence Switch Fallthrough ([[fallthrough]]): Indicate intentional switch-case fallthroughs to assist static analysis and jump table optimization.
  6. Declare __attribute__((pure)) Functions: Mark functions that depend only on their parameters and global state without side effects, allowing the compiler to cache repeated call results.
  7. Declare __attribute__((const)) Functions: Mark functions that depend strictly on parameter values (no global reads), allowing call elimination during common subexpression pass.
  8. Enforce Compile-Time Invariants (_Static_assert): Validate struct sizes, alignments, and offset assumptions at compile-time with zero runtime footprint.
  9. Zero-Overhead Dispatch via _Generic: Use C11/C17 type-generic expressions to select optimized type-specific logic at compile time without function pointer overhead.
  10. Use Exact-Width Integers (int32_t, uint64_t): Match native CPU register widths precisely to eliminate hidden zero/sign-extension instructions.
  11. Leverage Fast Integer Types (int_fast32_t): Let the compiler pick the standard register size that executes fastest on the target platform for local counters.
  12. Eliminate Impossible Code Paths (__builtin_unreachable): Place this directive in unreached switch cases or post-assertions to strip unnecessary checks and save instruction cache space.
  13. Inlining Control (inline vs always_inline): Use static inline for small utility calls; reserve __attribute__((always_inline)) strictly for micro-benchmarked hot paths.
  14. Isolate Hot and Cold Code (__attribute__((hot/cold))): Annotate functions to force the compiler to group critical loops together in memory while moving error handlers far away.

II. Data Structures & Memory Layout Optimization

  1. Minimize Struct Padding: Order struct members strictly from largest to smallest type (e.g., uint64_t $\rightarrow$ uint32_t $\rightarrow$ uint8_t) to eliminate compiler-inserted alignment gaps.
  2. Align Hot Structures to Cache Lines (alignas(64)): Align critical data structures to 64-byte boundaries (<stdalign.h>) to ensure they fit cleanly inside L1 cache lines without straddling boundaries.
  3. Structure of Arrays (SoA) over Array of Structures (AoS): Store fields in parallel arrays (float x[], float y[]) rather than array of structs (Point pts[]) to enable continuous SIMD loads.
  4. Flatten Multi-Dimensional Arrays: Replace dynamic nested pointers (float**) with single continuous 1D arrays indexed as array[y * width + x] for guaranteed contiguous memory access.
  5. Bitfield and Bitmask Packing: Pack multiple logical flags into a single integer using bitwise operations or explicit bitfields to fit state representations in CPU registers.
  6. Zero-Cost Variant Storage (union): Use unions to overlap memory for mutually exclusive object fields, shrinking overall structure footprint.
  7. Prefer 32-bit Array Indices over 64-bit Pointers: Store relative 32-bit offset indices instead of full 64-bit raw pointers to double data density inside cache lines.
  8. Avoid Pointer Chasing (Flat Vectors over Linked Lists): Use continuous dynamic arrays instead of linked nodes to allow hardware prefetchers to predict next-element loads.
  9. Small String Optimization (SSO): Embed short inline character arrays directly inside string structs to eliminate heap allocations for small strings.
  10. Size Objects to Powers of Two: Keep structure dimensions aligned to powers of two to convert array indexing multiplications into fast bit-shifts.
  11. Use Packed Attributes with Caution (__attribute__((packed))): Unaligned accesses forced by packed structs incur performance penalties or hardware traps on architectures like ARM. Use only for wire formats.
  12. Split Hot and Cold Data Fields: Separate frequently read/written structure fields from rarely accessed configuration fields into distinct structures linked by pointer or index.
  13. Eliminate Hidden Padding in Array Collections: Verify structure sizes with _Static_assert(sizeof(T) % alignof(T) == 0) to guarantee arrays do not contain useless padding bytes.
  14. Utilize Cache-Oblivious Data Layouts: Structure binary search trees or graphs using van Emde Boas or B-tree layouts to optimize cache utilization across all cache levels.

III. CPU Pipeline, ILP & Branch Optimization

  1. Branchless Arithmetic Expressions: Replace if/else logic in hot loops with ternary operators, bitwise masks, or conditional moves (CMOV) to prevent branch mispredictions.
  2. Replace Division with Magic-Number Multiplication: Multiply by precalculated fixed-point reciprocals (or let compiler do it for constants) to avoid slow hardware division instructions.
  3. Bitwise Modulo for Power-of-Two Bounds: Replace x % N with x & (N - 1) whenever the modulus N is a power of two.
  4. Strategic Loop Unrolling: Use #pragma GCC unroll N or manual loop unrolling to reduce loop counter compare/branch overhead, ensuring code fits within the Instruction Cache (I-Cache).
  5. Loop Fusion: Combine adjacent loops iterating over the same bound to maximize register reuse and cut loop-branch overhead.
  6. Loop Fission: Split bloated loops performing too many tasks into smaller sequential loops so working sets fit entirely inside L1 data cache.
  7. Loop Invariant Code Motion: Pull invariant computations, array bounds, and constant lookups completely outside loop execution blocks.
  8. Software Pipelining: Interleave independent instructions across consecutive iterations to maximize Instruction-Level Parallelism (ILP) execution ports.
  9. Avoid Dynamic Function Pointers in Hot Loops: Replace virtual dispatch or function pointers in tight loops with switch statements or direct calls to enable static branch prediction and inlining.
  10. Contiguous Switch Keys for Jump Tables: Keep switch case values tight and sequential so the compiler generates $O(1)$ assembly jump tables rather than $O(N)$ comparison chains.
  11. Eliminate Unnecessary Sign-Extensions: Use native unsigned counter types (size_t, uint32_t) for loop bounds to prevent unnecessary assembly sign-extension operations during increments.
  12. Compute Min/Max Without Branches: Calculate min and max values using bitwise operations: r = y ^ ((x ^ y) & -(x < y));.
  13. Hoist Constant Calculations: Ensure constant expressions are evaluated strictly at compile time rather than dynamically during loop startup.

IV. SIMD Vectorization & Floating-Point Optimization

  1. Construct Auto-Vectorization Friendly Loops: Write simple countable loops with contiguous stride-1 accesses and no loop-carried data dependencies to allow auto-vectorization.
  2. Explicit Vector Intrinsics: Use <immintrin.h> (AVX2/AVX-512) or <arm_neon.h> when compiler auto-vectorization fails on complex mathematical loops.
  3. Avoid Mixed-Precision Floating Point: Avoid mixing float and double expressions to prevent expensive runtime CPU conversion instructions.
  4. Mathematical Strength Reduction: Replace pow(x, 2.0) with x * x and pow(x, 0.5) with sqrt(x) to bypass heavy math library invocations.
  5. Hardware Bit Counting Instructions: Use compiler intrinsics like __builtin_popcount(), __builtin_clz(), and __builtin_ctz() which translate to direct single-cycle CPU instructions (POPCNT, LZCNT).
  6. Precomputed Read-Only Lookup Tables (LUTs): Store precomputed expensive mathematical values (trig functions, inversions) in const contiguous arrays.
  7. Leverage Fused Multiply-Add (FMA): Compile with -mfma or use fma() intrinsics to evaluate (a * b) + c in a single CPU clock cycle with higher precision.
  8. Fast Reciprocal Square Root: Utilize hardware approximate reciprocal square root instructions (rsqrt) where ultra-exact floating-point precision is not required.
  9. Hardware-Accelerated CRC and Crypto: Use built-in intrinsics (__builtin_ia32_crc32qi) for hardware-accelerated hashing and checksum calculations.
  10. Ensure Aligned Vector Memory Loads: Use aligned_alloc() to align SIMD vector buffers to 16, 32, or 64-byte boundaries, enabling aligned hardware SIMD instructions (_mm256_load_si256).

V. Memory Hierarchy, Cache & NUMA Optimizations

  1. Issue Non-Blocking Software Prefetching: Use __builtin_prefetch(ptr, rw, locality) to load upcoming memory targets into L1 cache several iterations ahead of processing.
  2. Enforce Stride-1 Sequential Traversal: Always traverse multi-dimensional arrays row-by-row (outer loop row, inner loop col) to match hardware prefetcher behavior.
  3. Loop Tiling / Blocking: Partition large matrices into small blocks (e.g., $32 \times 32$ or $64 \times 64$) that fit completely inside L1/L2 caches during computation.
  4. Eliminate Cache Line False Sharing: Isolate variables or atomic counters modified by different threads onto distinct 64-byte cache lines using alignas(64).
  5. Use Non-Temporal Streaming Stores: Use non-temporal instructions (_mm_stream_si128) when writing large destination buffers to bypass L1/L2 caches and write straight to main memory.
  6. NUMA-Aware Allocations: Bind thread allocations to the specific NUMA node running the target worker thread using OS-specific NUMA APIs.
  7. Utilize Huge Pages (MAP_HUGETLB): Map massive contiguous memory buffers using 2MB or 1GB pages to drastically reduce Translation Lookaside Buffer (TLB) cache misses.
  8. Isolate Mutable State from Read-Only State: Group frequently updated variables away from constant read-only variables to avoid cache invalidation waves across cores.
  9. Prefetch Graph and Tree Child Nodes: Issue explicit prefetch calls on child nodes while executing computation logic on current nodes during graph/tree traversals.
  10. Instruction Cache Alignment: Align hot function entry points to 16-byte or 64-byte boundaries (__attribute__((aligned(16)))) to optimize I-Cache line fetches.
  11. Keep Inner Loop Working Sets Under L1 Size: Keep total data touched by tight inner loops strictly within 32KB to eliminate L2/L3 cache roundtrips.

VI. Custom Dynamic Memory & Allocation Strategies

  1. Zero Dynamic Allocations in Hot Code: Pre-allocate all working memory buffers during initialization; eliminate runtime malloc() and free() calls during hot execution.
  2. Arena / Bump Allocators: Use arena allocators for group object lifecycles, enabling continuous linear memory carving and $O(1)$ bulk deallocation by resetting an offset pointer.
  3. Fixed-Size Slab Pools: Build dedicated free-list slab allocators for fixed-size objects to achieve fast $O(1)$ allocation/freeing with zero heap fragmentation.
  4. Prefer calloc() for Large Clear Allocations: Use calloc() over malloc() + memset(), allowing OS kernels to map pages lazily to pre-zeroed physical memory pages.
  5. Amortize Growth with Geometric Resizing: Scale dynamic array buffer capacities by $1.5\times$ or $2\times$ during reallocations to achieve amortized $O(1)$ push operations.
  6. Standard C17 Aligned Allocation (aligned_alloc): Use standard aligned_alloc(alignment, size) to allocate memory directly aligned to hardware boundary requirements.
  7. Memory-Mapped File I/O (mmap): Map large data files directly into the virtual address space using mmap() to bypass user-space buffering overhead.
  8. Kernel Paging Hints via madvise(): Issue madvise(..., MADV_SEQUENTIAL) or MADV_WILLNEED to instruct the OS kernel to pre-fault pages before worker access.
  9. Lock Critical Pages in RAM (mlock): Use mlock() to prevent real-time performance-critical memory regions from being swapped out to disk by the OS.
  10. Use Stack Allocations (alloca / Local Arrays) Cautiously: Allocate small temporary buffers on the stack to bypass heap overhead, ensuring size limits are strictly validated against stack overflows.
  11. Design API Callers to Pass Output Buffers: Pass destination pointers into functions rather than having functions allocate and return new heap memory blocks.
  12. Recycle and Reuse Memory Structures: Reset internal length counters of existing structs/vectors rather than freeing and reallocating them.
  13. Deallocate Memory in Reverse Allocation Order: Free dynamic memory in reverse order of allocation to assist runtime allocators in merging contiguous free blocks.
  14. Optimize memcpy with Non-Overlapping restrict Hints: Ensure source and destination pointers passed into custom copy wrappers are non-overlapping to allow wide SIMD registers to execute copying.

VII. Concurrency & Atomics Optimization (<stdatomic.h>)

  1. Use Standard C17 Atomics (<stdatomic.h>): Rely on standard <stdatomic.h> types (atomic_int, atomic_uintptr_t) for lock-free multi-threaded code.
  2. Use Relaxed Memory Ordering (memory_order_relaxed): Apply memory_order_relaxed to independent counters, metrics, or telemetry where cross-thread synchronization order is not required.
  3. Acquire-Release Synchronization Semantics: Pair memory_order_acquire (reads) and memory_order_release (writes) to construct synchronization points without expensive full memory barriers.
  4. Avoid Default memory_order_seq_cst Overhead: Avoid relying on default atomic operations without explicit memory ordering parameters, as seq_cst forces expensive global bus locks on non-x86 hardware.
  5. Ultra-Lightweight Spinlocks via atomic_flag: Build fast micro-locks using atomic_flag test-and-set operations for critical sections lasting only a few cycles.
  6. Apply CPU Backoff Pause in Spin Loops: Insert _mm_pause() (x86) or __yield() (ARM) inside atomic polling loops to reduce pipeline stall penalties and power consumption.
  7. Eliminate Synchronization via Thread-Local Storage (_Thread_local): Store thread-specific state in Thread-Local Storage (_Thread_local) to perform lockless local accumulation before merging.
  8. Sharded / Striped Mutex Arrays: Partition global hash tables or resource locks into arrays of separate sub-locks indexed by resource hash to reduce contention.
  9. Read-Copy-Update (RCU) Strategy: Utilize RCU patterns for data structures characterized by frequent concurrent reads and infrequent modifications.
  10. Zero External I/O Inside Critical Sections: Complete all string formatting, memory allocations, and I/O preparation before acquiring a lock, holding the lock strictly for memory pointers updates.
  11. Single-Producer Single-Consumer (SPSC) Lock-Free Queues: Build thread-communication channels using SPSC ring buffers driven by atomic head and tail pointers.
  12. Batch Operations Across Locks: Acquire a lock once to process an entire batch of work items before releasing, amortizing lock acquisition overhead.
  13. Safe Lock-Free Reclamation (Hazard Pointers / Epochs): Implement Hazard Pointers or Epoch-Based Reclamation to safely free memory nodes without triggering use-after-free bugs.
  14. Configure POSIX Priority Inheritance (PTHREAD_PRIO_INHERIT): Set priority inheritance on realtime threads to prevent high-priority worker threads from stalling behind lower-priority lock holders.
  15. Direct Kernel Futexes: Use direct OS kernel futex syscalls on Linux to implement lightweight user-space synchronization primitives.
  16. Atomic Fetch-and-Add over CAS Loops: Use atomic atomic_fetch_add() instead of compare-and-swap (CAS) loops when performing numerical counter additions.
  17. Thread Affinity Pinning: Pin hot worker threads to physical CPU cores via pthread_setaffinity_np() to prevent cache-flushing OS thread migrations.

VIII. System, I/O & Runtime Performance

  1. Zero-Copy Kernel Data Transfers (sendfile / splice): Transfer stream data directly between file descriptors within kernel space, avoiding user-space memory copies.
  2. Asynchronous Ring-Buffer I/O (io_uring): Use Linux io_uring or high-performance event loops (epoll) instead of blocking system read/write calls.
  3. Custom Fast String and Integer Parsing: Avoid standard sscanf, strtol, or printf routines in performance-critical paths; write dedicated SIMD/SWAR parsers.
  4. Expand Standard I/O Buffer Capacity (setvbuf): Configure standard stream buffers to custom large sizes (e.g., 64KB) using setvbuf() to reduce system call frequency.
  5. Asynchronous Lock-Free Logging: Push log messages to a lock-free ring buffer and delegate actual disk writes to a dedicated background logging thread.
  6. Signal-Safe Atomic Flag Handlers: Limit signal handler execution exclusively to simple atomic flag toggles (volatile sig_atomic_t or atomic_flag).
  7. Zero-Copy Binary Protocol Parsing: Map wire or disk binary protocols directly over in-memory structures to bypass intermediate parsing iterations.

IX. Build System, Compiler Tooling & Profiling

  1. Profile-Guided Optimization (PGO): Build binaries with -fprofile-generate, run realistic production workloads, and recompile with -fprofile-use to enable basic-block layout optimizations.
  2. Link-Time Optimization (LTO): Enable Link-Time Optimization (-flto) to allow compilers to inline code and strip dead functions across separate .c compilation units.
  3. Target Host Architecture Generation (-march=native): Compile with -march=native to allow the compiler to utilize all vector extensions (AVX2, AVX-512, FMA, NEON) supported by the hardware.
  4. Restrict Internal Symbol Visibility (static): Mark private functions as static or set hidden visibility to allow compilers to eliminate dynamic symbol table overhead and optimize call sites.
  5. Strip Dead Sections (-ffunction-sections, --gc-sections): Place functions and data in distinct sections to allow the linker to strip unused code from the binary.
  6. Controlled Fast-Math Flags (-ffast-math): Apply fast-math flags selectively on math-intensive files to enable reciprocal multiplications and associative reordering when IEEE-754 strictness is negotiable.
  7. Frame Pointer Elimination (-fomit-frame-pointer): Compile with -fomit-frame-pointer to free up an extra general-purpose register (EBP/RBP) for register allocation in hot functions.
  8. Disable Unnecessary Exception Unwind Tables: Compile with -fno-unwind-tables and -fno-asynchronous-unwind-tables to shrink binary size and maximize instruction cache efficiency.
  9. Static Linking of Hot Libraries: Link critical third-party C libraries statically (-static) to eliminate PLT/GOT dynamic jump overhead.
  10. Measure Hardware PMUs Before Optimizing: Use hardware performance profiling tools (perf, Intel VTune, valgrind --tool=cachegrind) to measure L1 misses, branch mispredictions, and instruction retirement rates before making manual optimization decisions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment