Principle:ClickHouse ClickHouse Optimized Memcpy
| Knowledge Sources | |
|---|---|
| Domains | Memory, SIMD |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Hand-optimized memory copying using SIMD instructions and careful algorithm selection.
Description
Standard memcpy performance varies by libc implementation and CPU. Custom memcpy can optimize for specific patterns: small size branches ordered by frequency, overlapping moves for uneven sizes, SSE/AVX for large copies, aligned stores with unaligned loads, loop unrolling. ClickHouse's memcpy avoids glibc symbol versioning issues and PLT overhead while matching or exceeding glibc performance on common data sizes.
Usage
Use when memcpy is a performance bottleneck, to avoid glibc dependencies, or in embedded systems needing optimized copies.
Theoretical Basis
SIMD Instructions: SSE2 (_mm_loadu_si128, _mm_store_si128) copies 16 bytes per instruction.
Alignment: Aligned stores are faster; aligning destination helps even if source is unaligned.
Loop Unrolling: Unrolling 8x uses all SSE registers and reduces loop overhead.
Size Thresholds: Different algorithms optimal for different size ranges (small: 2-16 bytes with overlapping moves, medium: simple loop, large: unrolled aligned).