perf(matrix): fill a fixed buffer instead of a vector per cell - #827
Merged
samueltardieu merged 1 commit intoSep 11, 2026
Merged
Conversation
`neighbours` is already lazy, but `bfs_reachable` and `dfs_reachable` collect it
into a freshly allocated `Vec` for every cell they visit, so walking a matrix
allocates and frees once per cell. A cell has at most eight neighbours, so they
fit in a fixed buffer and the allocation buys nothing.
Output is unchanged. Besides the existing tests, both traversals were compared
against the previous code over 6092 traversals across 60 random matrices,
covering both diagonal settings and every starting cell: identical fingerprint
over every cell returned.
Measured against this commit's parent, four passes with the order of the two
binaries swapped on alternate passes:
Matrix::bfs_reachable, 400x400, 12% blocked: about 26% off
Matrix::dfs_reachable, 400x400, 12% blocked: about 31% off
Measured on Windows 11, Intel Core Ultra 7 265K, 64 GB RAM.
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.
Closes #826.
Collects the neighbours of a cell into a fixed
[(usize, usize); 8]rather than a vector, inbfs_reachableanddfs_reachable.neighboursitself is untouched — it already returns an iterator, so only the two traversals needed changing.Checking
All tests pass, clippy and rustfmt clean.
Beyond those, both traversals were compared against the previous code over 6092 traversals across 60 random matrices, covering both diagonal settings and every starting cell. Identical fingerprint over every cell returned, so this is not "faster because it visits less".
Measured against this branch's parent, four passes with the order of the two binaries swapped on alternate passes, consistent in both directions:
Measured on Windows 11, Intel Core Ultra 7 265K, 64 GB RAM.
Same shape as #824 but independent of it: that one touches
grid.rs, this onematrix.rs.