Optimization: Indexed Iteration
For some commonly used list implementations, such as List<T>, arrays, ImmutableArray<T> and Span<T> the enumerator based iteration (with foreach) is slower than the iteration with using the index.
So it would be good to change the iteration from enumerator based iteration to the indexed iteration, provided the list implementation is known.
Additional considerations are required for List<T>, because the index based iteration does not check for concurrent modifications. It should be the default option to inject this check for modification into the code, but dropping this check should be possible as well, because the check will rely on reflection and may make thing slower than before.
So it needs to be checked if it is reasonable to create the indexed iteration with the modification check, or if that is slower than the iteration using the enumerator.