Originally developed 3 year ago as a final project for intro to C++ course. this project has been completely rewritten and optimized as a modern C++23 application designed to parse, hash, and analyze over 1,000,000 multi-dimensional (2D and 3D) points.
The goal of this updated project is to demonstrate low-level systems optimization, memory management, and microarchitectural profiling.
-
Bypassed Standard I/O Utilized Linux
mmapandstd::ispanstreamfor zero-copy file reading, avoiding standard stream I/O overhead. -
Zero-Allocation Parsing: Replaced standard strings with
std::string_viewto extract data without triggering unnecessary heap/string allocations. -
Modern Memory Views Leveraged
std::views::zipto combine multiple coordinate vectors into tuple views for fast, zero-copy reference reading. -
Compile-Time Hashing: Implemented a custom variadic template hashing functor utilizing
std::applyto efficiently hashstd::tuplestructures within astd::unordered_set.
The initial implementation suffered from severe memory and algorithmic bottlenecks. Parsing relied on dynamic string concatenation causing continuous heap reallocations, 2D frequency counting used an O(N²) nested loop resulting in billions of redundant instructions, and 3D frequency counting utilized pass-by-value std::vector copies inside a std::map, forcing heavy libc memory comparisons (memcmp)
Using Linux perf (stat, record --call-graph), bottlenecks were identified. The data structures were subsequently refactored to utilize a Structure of Arrays (SoA) layout and std::unordered_set.
Resulting Metrics:
-
Execution time was reduced from 20.8 seconds to 900 milliseconds (-O0) to 140 milliseconds (-O2) (>150x speedup).
-
Total executed CPU instructions were reduced by approximately 211 billion.
-
Flat C-Structs for Hashing: Linux
perfsampling indicated that roughly 27% of execution time was spent hashingstd::tuplewithin thestd::unordered_set. Replacing these tuples entirely with flattened custom structs would further reduce this overhead. -
Dynamic Bucket Allocation Pre-allocating std::unordered_set bucket sizes dynamically based on initial mmap file byte-size estimates to prevent rehashing
-
Algorithmic Distance Calculation Implementing Convex Hull and Rotating Calipers algorithms to find the maximum distance between 2D points algorithmically, rather than relying on dataset-specific boundries.
- A Linux environment (requires
sys/mman.hfor memory mapping (i.emmap) ). - A compiler supporting C++23 perferably clang 18+
Compile with optimizations enabled:
clang++ -Wall -Wextra -Werror -std=c++23 -O2 -g newMain.cpp -o newmain