-
-
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
Simpler syntax for creating uninitialized arrays #34775
Comments
See this issue and this Discourse discussion. These posts are both very long and mostly discuss something other than your concrete proposal. However, there is at least one relevant point, namely that Not sure I agree, though. The same could be said for So think your proposed |
...which might be a reason not to do it... |
It may be important to make the "uninitialized" part explicit, but I don't think it's necessary to make the syntax harder to use. |
This is indeed somewhat intentional, to discourage uninitialized arrays. But we also wanted to move towards more general and regular syntax instead of all the special cases like |
If we want to increase uniformity, one thought for 2.0: deprecate |
Wasn't this already discussed, cf JuliaLang/LinearAlgebra.jl#484? |
I guess I'm consistent! |
I’ve actually often wanted a way to go in the other direction: factor out the initializer concept so that I can do things uniformly like this: Array{T}(undef, m, n)
Array{T}(zeros, m, n)
Array{T}(ones, m, n) Why? It makes it easier to swap out any of the properties of what’s being done: it cleanly separates the container type, the element type, what to initialize it with and the dimensions. |
Note also that while |
Not really, because in reality |
I think the point is that making an uninitialized immutable array isn't very useful. |
Oh, yeah, I misread that. |
Comet topic! :)
For interested newcomers to this discussion, #24595 (comment) discusses this direction at length as 'the second proposal':
|
One thing we could do is:
That way we round out the collection of convenience constructors in way that can always be expressed in terms of the fuller |
What I see from this syntax is that whatever initializer put here should be as fast as undef. Since julia> @btime zeros(Float64, 1000, 1000);
443.589 μs (2 allocations: 7.63 MiB)
julia> @btime Array{Float64}(undef, 1000, 1000);
37.140 μs (2 allocations: 7.63 MiB) |
I am trying to think about the implications of these proposals for generic code. It is not clear to me if
As for (1) a lot of packages define Regarding (2), it would be nice for custom types to be able to rely on a default like function zeros(S::Type{SomeCustomType{T}}, shape...) where T
fill(S, zero(eltype(S)), shape...)
end and define only this function undef(SomeCustomType{T}, shape...)
SomeCustomType{T}(undef, shape...)
end |
I don't understand why that should be the case. Yes, we want initializers to be as fast as we can make them, but some require more work than others. Why would we require that they all be as fast as doing nothing? |
It is quite tricky to measure this since the OS can sometimes give out uninitialized memory "for free" and only commit to the actual allocation when the memory is used. |
I find the syntax for creating uninitialized arrays a bit verbose, while there are nice and short options for almost all other common cases of creating arrays:
But for uninitialized arrays you always have to use curly bracket syntax if I'm correct:
How about one of these two alternatives, which both seem to be available:
The second one is actually easy to get via:
The text was updated successfully, but these errors were encountered: