Reduce memory footprint with SparseArray, Merged loops