Skip to content

improve unordered byte span comparison performance#89

Merged
jamesmoore merged 2 commits into
mainfrom
improve-unordered-byte-span-performance
May 23, 2026
Merged

improve unordered byte span comparison performance#89
jamesmoore merged 2 commits into
mainfrom
improve-unordered-byte-span-performance

Conversation

@jamesmoore
Copy link
Copy Markdown
Owner

@jamesmoore jamesmoore commented May 22, 2026

This pull request introduces a new implementation, AutogramBytesNoStringsV5g, which enhances the autogram search algorithm by utilizing a new method for comparing byte arrays (UnorderedByteSpanEqualsWithSum). The update also integrates this new version into the benchmarking and testing infrastructure, and optimizes a hash code computation. The most important changes are grouped below:

New Algorithm Implementation

  • Added the new class AutogramBytesNoStringsV5g, which uses the UnorderedByteSpanEqualsWithSum method for improved byte array comparison during the autogram search process. This class is now part of the solver suite.

Byte Array Comparison Enhancements

  • Introduced the UnorderedByteSpanEqualsWithSum extension method for ReadOnlySpan<byte>, which first compares the sum of elements before checking for unordered equality, improving efficiency in certain scenarios.
  • Added a benchmark for UnorderedByteSpanEqualsWithSum in UnorderedByteArrayComparisonBenchmark.cs to measure its performance.

Benchmarking and Testing Integration

  • Integrated AutogramBytesNoStringsV5g into the benchmarking suite: added setup, cleanup, and a benchmark method to evaluate its performance alongside existing solvers. [1] [2] [3] [4]
  • Added a test for AutogramBytesNoStringsV5g in the test suite to ensure correctness.

Code Optimization

  • Optimized the GetHashCode method in ByteHistoryKey64 by simplifying the hash computation for better performance and maintainability.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new autogram solver variant (AutogramBytesNoStringsV5g) that uses a new unordered byte-span comparison helper (UnorderedByteSpanEqualsWithSum), and wires the new solver/comparison into the existing benchmark and test suites. It also adjusts ByteHistoryKey64.GetHashCode() as a performance optimization.

Changes:

  • Added AutogramBytesNoStringsV5g and a new ReadOnlySpan<byte> extension method UnorderedByteSpanEqualsWithSum.
  • Integrated the new solver and comparison method into benchmarks and added a correctness test for V5g.
  • Simplified ByteHistoryKey64.GetHashCode() implementation.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
AutogramTest/AutogramTest.cs Adds a new unit test covering the V5g solver.
AutogramBenchmark/UnorderedByteArrayComparisonBenchmark.cs Adds a benchmark for the new unordered-span comparison method.
AutogramBenchmark/AutogramBenchmark.cs Adds setup/cleanup and a benchmark entry for the V5g solver.
Autogram/Extensions.cs Introduces UnorderedByteSpanEqualsWithSum for faster unordered comparisons.
Autogram/ByteHistoryKey64.cs Updates hash code computation for ByteHistoryKey64.
Autogram/AutogramBytesNoStringsV5g.cs New solver implementation using the new comparison method.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Autogram/Extensions.cs
Comment on lines +218 to +223
Span<int> counts = stackalloc int[256];

foreach (var b1 in a)
{
counts[b1]++;
}
HashCode.Combine(length, chunk0, chunk1, chunk2),
HashCode.Combine(chunk3, chunk4, chunk5, chunk6),
chunk7);
return HashCode.Combine(chunk0, chunk1, chunk2, chunk3, chunk4, chunk5, chunk6, chunk7);
Comment thread AutogramBenchmark/UnorderedByteArrayComparisonBenchmark.cs Outdated
@jamesmoore jamesmoore merged commit 6bb4ca4 into main May 23, 2026
4 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.

2 participants