Replace usage of range-checked 'at' method when vector/string has already been size checked #3280
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
what
Replace places in the codebase where the
at
method of astd::vector
orstd::string
instance is used to access an element instead of the subscript operator ([]
), and the index of the item is guaranteed to be valid.Additionally, simplify some of these uses where an old-style C loop is used to iterate over all elements and replace it with a range-based loops (which also implicitly guarantees no out-of-bound access).
why
The
at
method is not equivalent to the subscript operator ([]
) operator, as it's range-checked and can thus throw astd::out_of_range
exception which makes the code slower not only because of the additional check but also due to the generation of exception handling code at these call sites.Because the updated instances are guaranteed to access valid indexes in the container, we can replace the usage of
at
to improve performance.And where possible, simplify the code by adopting ranged-based loops.
misc
This is part of a series of PRs to improve performance of the library (7/n). Previous: #3253