-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Missing documentation: sortperm! is not truly non-allocating unless scratchspace is provided #53834
Comments
It's a style convention, not something enforced, and it's about modifying at least one of arguments, not being entirely non-allocating. |
I don't disagree that |
Scratchspace handling in sorting is okay but not perfect. For example, sometimes it still allocates when passed a scratchspace. I don't want to commit to keeping the current system as is forever. For example, it's possible using Memory might be better. Consequently, I chose not to publicize it (and therefore make it a part of the stable API) when I added it. I have yet to find a single real-world example where explicitly passing a scratch space to Actionable things here
|
Thanks for detailed explanation. Okay, if scratchspace is not part of official/stable API, it does not make sense to document it inside of I think what you suggest by improving the explanation here, https://docs.julialang.org/en/v1/manual/style-guide/#bang-convention, to highlight that in-place operations does not mean non-allocating. I just make a fork of official doc, put some text in and then we take it from there? I had a weird edge case, in which if I commented out a line, then sortperm! would not allocate at all, if I commented it in, it would allocate once. I am programming non-allocating algorithms for simulations, so I need to ensure there is truly none - this is why I spotted it in the first place. Kind regards |
Why must your algorithms not allocate? If it is for performance reasons, remember that unexpected allocations are typically a sign of type instability or other major performance issues, but allocations themselves are not all that slow and can enable faster algorithms.
On Julia 1.9+, you can use julia> VERSION
v"1.8.5"
julia> x = rand(1:10, 100);
julia> @b sort!($x, alg=QuickSort)
248.288 ns (1 allocs: 144 bytes)
Yes! Once you are ready to contribute the changes you've made to the official docs, make a pull request, and feel free to @ me in it. |
FWIW, I have experimented with scratch spaces for sort! in my code (repetitive sorting of rows and columns of huge sparse matrices), which gave 1-3% overall speed up in linear algebra for particular "real-world" examples. So I think it is a nice improvement :), though no idea how useful it is in general |
Hi!
Full thread here:
https://discourse.julialang.org/t/why-does-sortperm-allocate-here/112028/3
Doing a small example it is easy to see that sortperm! will allocate:
This is because a scratch-space has not been preallocated. Dan kindly provided the following code which fixes the issue:
I would not have been able to discover this without help since the
? sortperm!
returns:With no mention of scratch-space.
I believe that any
!
function is assumed to be non-allocating, and that this goes against this.I went to check help for
sort!
too and did not find any mention of scratch space either.Tasks
The text was updated successfully, but these errors were encountered: