reduce 1 memory write in loop