Skip to content

Compare 2D arrays without boxing their cells - #25

Open
Illustar0 wants to merge 1 commit into
Noggog:devfrom
Illustar0:perf/array2d-sequence-equal
Open

Illustar0 wants to merge 1 commit into
Noggog:devfrom
Illustar0:perf/array2d-sequence-equal

Conversation

@Illustar0

Copy link
Copy Markdown

IReadOnlyArray2d<T> is an IEnumerable<IKeyValue<P2Int, T>>, so SequenceEqualNullable on two arrays goes through the IEnumerable overload: every cell is boxed as an IKeyValue and compared through ValueType.Equals. Mutagen's generated Equals does exactly this for fields like a landscape's heights, normals and colors.

This adds an IReadOnlyArray2d<T> overload that compares the size, then each cell with EqualityComparer<T>.Default. Since it is more specific, existing callers pick it up when they are recompiled, without any change on their side.

Comparing a landscape's three 33x33 arrays:

Method Mean Allocated
Before 240.7 µs 654 KB
After 7.3 µs 0 B

- Add IArray2dExt.SequenceEqualNullable, which compares the size and then each cell
- The IEnumerable overload enumerated each cell as a boxed IKeyValue and compared it through ValueType.Equals
- Comparing a landscape's 33x33 heights, normals and colors takes 7.3 µs instead of 241 µs, and allocates nothing instead of 654 KB
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