Skip to content

perf(matrix): fill a fixed buffer instead of a vector per cell - #827

Merged
samueltardieu merged 1 commit into
evenfurther:mainfrom
tachsin:perf/matrix-reachable-buffer
Sep 11, 2026
Merged

samueltardieu merged 1 commit into
evenfurther:mainfrom
tachsin:perf/matrix-reachable-buffer

Conversation

@tachsin

@tachsin tachsin commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Closes #826.

Collects the neighbours of a cell into a fixed [(usize, usize); 8] rather than a vector, in bfs_reachable and dfs_reachable.

neighbours itself 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:

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.

Same shape as #824 but independent of it: that one touches grid.rs, this one matrix.rs.

`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.
@samueltardieu
samueltardieu added this pull request to the merge queue Sep 11, 2026
Merged via the queue into evenfurther:main with commit e1460e6 Sep 11, 2026
12 checks passed
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.

Matrix reachability allocates a vector for every cell it visits

2 participants