Optimizing thread + memory handling