avoid redundant memory access in loop