Optimized memory allocation, ~5x speedup