Add fast path for immutable primitive array equality#22366
Add fast path for immutable primitive array equality#22366gorsing wants to merge 1 commit intodlang:masterfrom
Conversation
|
Thanks for your pull request and interest in making D better, @gorsing! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + dmd#22366" |
|
I'd assume LDC is able to inline and optimize the loop without explicitly using |
|
You are right that LDC is excellent at optimizing loops. However, the main goal of this PR is to introduce an Identity Check optimization. Currently, comparing a large array to itself takes O(N) time. By adding if (lhs.ptr == rhs.ptr), we make it O(1) for identity cases. Here is a benchmark showing the impact on identity matches (10M elements):
For the loop itself, I'm happy to use the standard lhs[i] if LDC's output is identical, but the pointer comparison at the start provides a massive win for a common pattern. My draft benchmark on https://run.dlang.io/ |
Doing an identity check is a 1 line change, the extra special casing looks redundant. |
|
The identity check doesn't work for custom opEquals and floating point types (because of nan comparisons), it should be limited to integral or class types. I don't see a reason to check for |
|
Could you now undo the changes to |
|
I have finalized the implementation by addressing all feedback:
|
|
Update draft benchmark |
a091421 to
a1d0249
Compare
a1d0249 to
ad4fe6f
Compare
Adds a specialized fast path to array equality for immutable arrays of
primitive element types.
For identical immutable element types (integral, char/wchar/dchar, bool),
equality is optimized by:
All other cases continue to use the existing generic comparison logic.
This change is a pure performance optimization with no semantic impact.