Eliminate allocations on foreach

See detailed commit messages.