-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Type-assert the return type of collect(...) in TOML
#59932
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
base: master
Are you sure you want to change the base?
Conversation
|
I don't think it'd guaranteed to return a Vector - if the iterator has a shape, it can return another Array type. |
|
Oops, yes you're quite right: julia> collect(Iterators.product(1:3, 1:4))
3×4 Matrix{Tuple{Int64, Int64}}:
(1, 1) (1, 2) (1, 3) (1, 4)
(2, 1) (2, 2) (2, 3) (2, 4)
(3, 1) (3, 2) (3, 3) (3, 4)Changed it to Array in bdba131. |
|
This would be very nice. Then I'd finally be able to turn collections into @nanosoldier |
|
Hmm, doesn't the docstring already guarantee that it returns an Array?
|
|
See Jeff's comment here: #50051 I strongly disagree with him - more abstraction and looser contracts don't always make function better - they make them harder to get correct and be performant. The issues with |
|
Looks like this is a breaking change so we can't do it, at least not in a minor release. Example from the tests: julia> using OffsetArrays
julia> x = zeros(5);
julia> a = OffsetArray(x, ntuple(Returns(-1), ndims(x)));
julia> collect(Broadcast.instantiate(Broadcast.broadcasted(+, a, 5)))
5-element OffsetArray(::Vector{Float64}, 0:4) with eltype Float64 with indices 0:4:
5.0
5.0
5.0
5.0
5.0Asserting it to be an AbstractArray would also solve the invalidations. Hope there aren't any edge-cases with that 😅 Alternatively, if folks think it's safer I could move the assertion to TOML.jl instead? |
|
Better to move it to the caller, IMO, since the caller has more context (e.g. in TOML, I'd expect it really is always a |
This prevents invalidations.
bdba131 to
58498f9
Compare
collect(itr)collect(...) in TOML
|
Fair enough, moved it in 58498f9.
Agreed, see also #59888 (comment). I personally don't intend to try fixing all invalidations, just the most egregious cases in packages I use to help TTFX until it's fixed properly. |
|
I believe the test failure is unrelated. |
|
Can we assume that |
|
Realistically that's probably always the case, but I don't see anything in the docs that guarantees it. |
On 1.12 this prevents the majority of invalidations from ChunkSplitters (dependency of OhMyThreads), going from:
To:
Cthulhu showed that most invalidations were coming from this method in TOML.jl:
vkeysis inferred as Any so on line 126enumerate(::Any)is called, which is invalidated by ChunkSplitters defining its ownBase.enumeratemethod: https://github.com/JuliaFolds2/ChunkSplitters.jl/blob/715991816504a40cbbe21cf057f29f1b82a7c349/src/internals.jl#L123It could be fixed by asserting
vkeys::Vector, but I figured it's better to type assertcollect(itr)since that's guaranteed to return an Vector anyway.