[php-src] PR #20273: Fix GH-20262: array_unique() SORT_REGULAR fails to deduplicate with mixed strings
| From: | jmarble | Date: | Thu, 23 Oct 2025 23:06:47 +0000 |
| Subject: | [php-src] PR #20273: Fix GH-20262: array_unique() SORT_REGULAR fails to deduplicate with mixed strings | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-33783@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/20273
Author: jmarble
### The Bug
array_unique() with SORT_REGULAR was failing to remove duplicate numeric
strings when mixed with alphanumeric strings:
```php
$units = ['5', '10', '3A', '5'];
array_unique($units, SORT_REGULAR);
// Before: ['5', '10', '3A', '5'] - duplicate '5'
not removed
// After: ['5', '10', '3A'] - works correctly
```
### Root Cause
Non-transitive comparisons in SORT_REGULAR (where '5' == 5 and
5 != '5abc' but '5' < '5abc') broke the
sort-based algorithm's assumption that sorting would group duplicates adjacently.
### Solution
Implemented hash-bucketing optimization for SORT_REGULAR:
- Hashes values to buckets by their coerced numeric value
- Compares within buckets using the **same** php_get_data_compare_func_unstable()
comparison
- **Fully preserves SORT_REGULAR type coercion semantics** (1 == '1' ==
true)
### Performance
- **4.2x - 6.9x speedup** on typical arrays (n=100 to n=10,000)
- Improves from O(n²) to O(n) average case
### Backward Compatibility
- **100% BC compatible** - preserves all existing coercion behavior
- Uses **identical comparison function as current implementation**
- All existing tests pass
- New comprehensive test coverage added
### Tests Added
- gh20262.phpt - Minimal regression test for the bug
- array_unique_variation_sort_regular.phpt - Comprehensive SORT_REGULAR behavior
coverage (16 scenarios)
Closes #20262