Repository navigation
Support comparison functions in array.sorted - #5627
Conversation
|
Could also use the |
|
Yes we can also do that, but this requires adding a new dependency. |
Is that stated somewhere? I couldn't find anything. |
Not explicitly, but you can search for |
|
I think I would prefer glidesort over an O(n^2) sort, even if it's an additional dependency. |
|
I think |
|
From a quick look, looks like that's what the code actually does. Then, just the docs need an update. |
|
I updated the documentation. There was already a test for the behavior. |
laurmaedje
left a comment
There was a problem hiding this comment.
Any thoughts on using by instead of compare as the name?
Co-authored-by: Laurenz <[email protected]>
|
Quoting the original post from #5371:
Of course, I can change it to |
|
I don't know, I like how |
|
I'll change it in an hour or so |
c9b45c0 to
40302e4
Compare
|
Should we allow |
a5af06c to
9e910df
Compare
|
hmm, when sorting floats it might be useful, but the details around +/- 0 are a bit tricky. if we do, we should first investigate what other languages do. |
|
In JavaScript, |
|
I feel like JavaScript is the last place one should look for inspiration in this regard. Personally, just returning integers with the clear {-1, 0, 1} seems sufficient to me. |
|
The Lua case is interesting. To make a stable sort possible with just two return values I guess they need to ensure that the the first element passed to the comparison function is always the leftmost one. But if that is a guarantee that one can give, a boolean seems quite straightforward. |
I don't think we can safely assume that if we stick with |
|
I think booleans would be much more convenient than -1, 0, 1, so I'd prefer to investigate a bit more whether that's somehow possible before proceeding. |
|
We can always convert from booleans to orderings using |
|
I found a way to rely on The idea is that when a sorting algorithm calls a comparison function, there are only two possible outcomes: the elements are swapped, or they are not. This means out of the three outcomes ( The drawback is that we need to make an extra allocation. This could be prevented when Additionally, I believe this method effectively makes any sorting algorithm stable. So maybe we can find even better sorting algorithms if we also include unstable sorting algorithms. |
laurmaedje
left a comment
There was a problem hiding this comment.
If key or by are specified, then Typst's function call overhead will most likely dominate the allocation cost anyway. But if neither is specified, I think we could skip all the complexity and have a separate code path for that. What do you think?
Co-authored-by: Laurenz <[email protected]>
|
I initially did not do that because I feared it would have made the code even more complex. In the end, it's fine, but I'm not sure the performance impact is very noticeable. I may try to do a benchmark later if I have the time, but don't count on it. |
|
Thank you! |
Co-authored-by: +merlan #flirora <[email protected]> Co-authored-by: Laurenz <[email protected]>
Co-authored-by: +merlan #flirora <[email protected]> Co-authored-by: Laurenz <[email protected]>
Closes #5365.
This PR is based on a rebased version of #5371 by @bluebear94. I used
glidesortinstead of the standard library sorting algorithm to prevent panics, which was the blocker for merging #5371.Original post below.
This PR is based on a rebased version of #5371 by @bluebear94. As suggested in #5371 (comment), I implemented a custom sorting function that does not panic, which was the blocker for merging #5371.
Sorting algorithm
The sorting algorithm I implemented is an in-place stable insertion sort. In particular, it does not allocate anything. The drawback is that it is considerably slower than Rust's default sorting algorithm. I initially tried to implement an in-place stable merge sort,1 but was unable to reach a point where I was confident about my implementation.
Performance
I did a very basic benchmark that involved executing the following function:
The total execution times of the for loop on my machine, as determined by
--timings, are shown in the table below.lenThe difference seems quite significant for big arrays. The three solutions I can think of are:
I tend to think option 2. is better.
Footnotes
https://thomas.baudel.name/Visualisation/VisuTri/inplacestablesort.html ↩