Changed Rust code to use more idiomatic iterator pattern.#284
Open
Ichoran wants to merge 1 commit intobddicken:mainfrom
Open
Changed Rust code to use more idiomatic iterator pattern.#284Ichoran wants to merge 1 commit intobddicken:mainfrom
Ichoran wants to merge 1 commit intobddicken:mainfrom
Conversation
The previous code used indexing from ranges e.g. `for i in 1..=m`. While this works, it's usually slower and more error-prone than iterator-based traversal. This patch switches to the more idiomatic .iter().enumerate() pattern since this is how you would usually do this in Rust. This does seem also to improve performance (~20% on my machine). Furthermore, the working space for the two rows was initialized in an atypical way: instead of creating the working space with the content and size desired, it created empty Vecs and then put content into it. This did not alter performance on my machine, but it makes the code easier to read (because the pattern is more expected). I also switched the argument reading to `iter().enumerate()` since it also is more expected. (No performance implication.) None of this changes the algorithm from what is used in the C reference; rather, they express the same algorithm in a more idiomatic Rust style.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The previous code used indexing from ranges e.g.
for i in 1..=m. While this works, it's usually slower and more error-prone than iterator-based traversal. This patch switches to the more idiomatic .iter().enumerate() pattern since this is how you would usually do this in Rust.This does seem also to improve performance (~20% on my machine).
Furthermore, the working space for the two rows was initialized in an atypical way: instead of creating the working space with the content and size desired, it created empty Vecs and then put content into it. The Vecs are now initialized with the correct content directly.
This did not alter performance on my machine, but it makes the code easier to read (because the pattern is more expected).
I also switched the argument reading to
iter().enumerate()since it also is more expected. (No performance implication.)None of this changes the algorithm from what is used in the C reference; rather, it expresses the same algorithm in a more idiomatic Rust style.