Implement Dynarray.sort#14848
Open
Jeannalyse wants to merge 8 commits into
Open
Conversation
Contributor
|
cc @ocaml/stdlib |
added 4 commits
June 5, 2026 15:03
Suggested-by: Gabriel Scherer <gabriel.scherer@inria.fr>
dd0cf0c to
88a1abc
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds new standard library functions :
Dynarray.stable_sort_sub,Dynarray.stable_sortandDynarray.sort.These functions were suggested here : #13343 (comment) .
They use
Array.stable_sort_sub, so the sorting algorithm is the same as in Array. They consider a change in length to be a programming error, and raise an exception when they detect one.Note that
Dynarray.sortis, in fact, an alias forDynarray.stable_sort: I figured that a default sorting function was a good thing to have, and that it could be later replaced by an other algorithm if needed.I have modified the tests in testsuite/tests/misc/sort_sub.ml so that they test both
Array.stable_sort_subandDynarray.stable_sort_sub.I came up with three versions in total, each committed separately :
Dynarray.get,Dynarray.set,Dynarray.blitetc.unsafe_geton the backing arraysArray.stable_sort_subon the backing arrayMy benchmarks show that the third one is the fastest, and it is also the shortest code-wise, so I only kept this one in the final commit. The benchmarks can be found on my fork on the branch named benchmarks/dynarray-stable-sort-sub. Here are some results :
./benchmarks.opt 1./benchmarks.opt 2./benchmarks.opt 3Please don't hesitate to ask questions and suggest improvements.