feat(libc): implement heapsort #11629
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11629"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by B-Howell on 2026-10-26T23:49:36Z.
What
Draft — overlapping implementation. Open PR #11400 by another contributor already implements the same
heapsort_nid_postfixexport and has been approved. The earlier "no other open PR implements the same function" claim on this PR was wrong; the overlap check correctly flaggedheapsortas a shared export. This PR is being reclassified: see the Consolidation plan below.Original scope (retained for review history): implement the BSD
heapsort(3)export for the libc PRX, following FreeBSD 11lib/libc/stdlib/heapsort.c: in-place binary max-heap built from the array, then repeated root/last swaps and a sift-down of the new root over the shrinking heap, using a one-elementmallocscratch. Guard ordering follows the reference:count <= 1returns0before any other check; above that,size == 0setserrnotoEINVALand returns-1; a scratch allocation failure returns-1with the allocator'serrno. Nullbase/comparethrowstd::invalid_argumentandcount * sizepastSIZE_MAXthrowsstd::overflow_errorbefore any element access or comparator call. Documented indocs/dev/debt/libc.md. NID3z6y7yolHpAregistry-confirmed viatools/nid_names.py.Consolidation plan
codex/libc-heapsort-tests-on-11400, local) is prepared on top of #11400's headb999ea3cadding the useful differential coverage from this PR: 11-bucket histogram validation across counts {0,1,2,3,7,16,33,1000}, byte-array (size=1) sort, non-power-of-2 three-byte-stride sort, a comparator-ABI probe that verifies the comparator sees in-array pointers (now usingstd::uintptr_trange/stride checks with non-wrapping bounds, addressing the review note), and extra null+count edge cases. It will be opened as a new PR after #11400 merges, not as a competitor.RecordingCompareKeys(cross-object pointer relational comparison and subtraction) is addressed in commita0c82ac07con this branch usingstd::uintptr_tconversions before any range/stride arithmetic;guest_qsortpasses locally. That commit will migrate to the follow-up branch rather than be published here.Tested
Windows 11 Pro, MinGW UCRT64 (gcc 16.2.0): built
guest_qsort_testsand ranctest -R "^guest_qsort$"→1/1 Passed. Rebuilt and reran after theuintptr_tfix (commita0c82ac07c) → still1/1 Passed. Also built and ran the same focused test on the tests-only follow-up branch rebased onto #11400 (b999ea3c) →1/1 Passed. Not run on a console.Checklist
main; no other open PR implements the same function — false: PR #11400 implements the sameheapsortexport.mdfiles or images added to the repositoryImported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T00:00:35Z.
Review: COMMENTED
Focused review of
268fa0bdb2fdff34cd8624bec67a635a23915304: the heapsort implementation follows the stated BSD control-flow and error cases, and the focused Linux Release target builds.ctest -R '^guest_qsort$'passes (1/1); conventions andgit diff --checkpass. Hosted product jobs are skipped by path policy; some relinker/register-context checks remain pending, so this is not a full product build result.One test portability issue:
RecordingCompareKeyscompares each comparator pointer against the array bounds with built-in>=/<atcore/libs/tests/GuestQsort.cpp:198, then may subtract unrelated pointers at line 199. For a scratch-buffer argument, those pointers do not belong to the same array, so the relational result is unspecified; if it happens to satisfy the range test, subtraction is undefined. Please comparestd::uintptr_taddress values (or otherwise avoid cross-object relational comparison/subtraction) so the comparator-ABI regression is deterministic across platforms. This is a test-only finding; I found no implementation blocker in the focused review.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T00:25:02Z.
Review: CHANGES_REQUESTED
One test portability issue remains in
RecordingCompareKeys. When the heapsort passes its scratch allocation,inArraycompares that pointer relationally againstheapsortBase/heapsortLimitand subtractsheapsortBasefrom it. Those operations are only defined for pointers into the same array object; the scratch allocation is a separate object. A passing Linux run therefore does not make this check portable across the project's supported compilers.Please classify the pointer addresses as
std::uintptr_tvalues before doing range/stride checks (with the bounds constructed so the integer arithmetic cannot wrap), or use another defined way to identify in-array arguments. Then rerunguest_qsortand the supported compiler checks. I have not rerun the test locally in this turn.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T02:01:29Z.
Review: CHANGES_REQUESTED
There is an unresolved duplicate implementation: open PR #11400 by another contributor exports and implements the same
heapsort_nid_postfixNID, and the overlap check identifies the sameheapsortexport. #11400 remains open and is not marked intake-paused; its current head already has an approval and passing focusedguest_qsortevidence. This PR also changes the same API declaration, implementation and test files, so merging both would duplicate the export and conflict in those files. The added differential/error-path coverage here may still be useful, but please coordinate with #11400 and the maintainers on one implementation/merge order, then record any dependency or consolidation in the checklist. I am not asking either contributor to close work solely because the queue is large. Separately, my prior portability request remains: the currentRecordingCompareKeysstill performs relational comparison and subtraction between its array pointers and a separately allocated scratch pointer; the exact current head still contains that code.Pull request closed