Skip to content

Changed Rust code to use more idiomatic iterator pattern.#284

Open
Ichoran wants to merge 1 commit intobddicken:mainfrom
Ichoran:main
Open

Changed Rust code to use more idiomatic iterator pattern.#284
Ichoran wants to merge 1 commit intobddicken:mainfrom
Ichoran:main

Conversation

@Ichoran
Copy link

@Ichoran Ichoran commented Dec 22, 2024

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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant