Skip to content
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

Make reshape and view on Memory produce Arrays and delete wrap #53896

Merged
merged 14 commits into from Apr 6, 2024

Conversation

LilithHafner
Copy link
Member

@LilithHafner LilithHafner commented Mar 29, 2024

Make reshape and (one-based) view on Memory produce Arrays and delete wrap

Implements #53552 (comment)

Fixes #53552

@LilithHafner LilithHafner marked this pull request as ready for review March 29, 2024 16:41
base/genericmemory.jl Outdated Show resolved Hide resolved
@N5N3
Copy link
Member

N5N3 commented Mar 30, 2024

The reshape change seems ok as reshape of Array returns Array.
But I'm afraid view is another story, as it seems incompatible with offset inds.

@LilithHafner
Copy link
Member Author

I dropped the @inbounds and restricted view to Base.OneTo and UnitRange inds. If there were an AbstractOneBasedUintRange type or an isonebased trait on abstract array types I would have used that instead.

Should we support slices?

view(m::GenericMemory{M, T}, inds::Colon) where {M, T} = view(m, eachindex(m))

base/iobuffer.jl Outdated Show resolved Hide resolved
base/iobuffer.jl Outdated Show resolved Hide resolved
@LilithHafner LilithHafner added this to the 1.11 milestone Apr 1, 2024
base/genericmemory.jl Outdated Show resolved Hide resolved
@vtjnash
Copy link
Sponsor Member

vtjnash commented Apr 1, 2024

But I'm afraid view is another story, as it seems incompatible with offset inds.

I think reshape(view(a, n:m), (x, y)) should be possible for the compiler to be exactly as optimized?

LilithHafner and others added 2 commits April 2, 2024 13:12
Co-authored-by: Jameson Nash <vtjnash@gmail.com>
@LilithHafner
Copy link
Member Author

I think @N5N3 was referring to offset indices like OffsetArrays.IdOffsetRange(values=3:6, indices=53:56), and they were correct that the proposed implementation was buggy for those offset inds. Fixed in 91542bc.

@LilithHafner LilithHafner added the backport 1.11 Change should be backported to release-1.11 label Apr 2, 2024
@LilithHafner
Copy link
Member Author

IIUC it is possible for resizing a vector produced here to corrupt the underlying memory. If I'm correct, that should be documented.

@LilithHafner
Copy link
Member Author

Seemingly unrelated CI failure, but master is green. Updating branch.

@LilithHafner
Copy link
Member Author

The failure, all(T -> T <: AbstractString, Base.return_types(string)), was real. IDK if that's a valid or useful thing to test, but I fixed it by telling the compiler that StringVector will indeed return a Vector{UInt8}.

@oscardssmith
Copy link
Member

I think it might be an issue if the compiler isn't able to infer the type of taking a view of a Memory.

@LilithHafner
Copy link
Member Author

The compiler knows that just fine

julia> Base.return_types(view, (Memory{Int}, UnitRange))
1-element Vector{Any}:
 Vector{Int64} (alias for Array{Int64, 1})

It's the return type of UnitRange when given a non-Int integer that is Any

julia> Base.return_types(:, (Int, Integer))
2-element Vector{Any}:
 UnitRange{Int64}
 Any

@LilithHafner
Copy link
Member Author

I'm ready to merge if @vtjnash and @oscardssmith think it's ready.

@LilithHafner
Copy link
Member Author

Nothing complex or controversial has changed since @vtjnash's approval. I'm going to go ahead and merge.

@LilithHafner LilithHafner merged commit 273d91e into master Apr 6, 2024
4 of 7 checks passed
@LilithHafner LilithHafner deleted the lh/delete-wrap branch April 6, 2024 13:12
@@ -42,7 +42,7 @@ end

# allocate Vector{UInt8}s for IOBuffer storage that can efficiently become Strings
StringMemory(n::Integer) = unsafe_wrap(Memory{UInt8}, _string_n(n))
StringVector(n::Integer) = wrap(Array, StringMemory(n))
StringVector(n::Integer) = view(StringMemory(n), 1:n)::Vector{UInt8}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change broke JSON3:

(base) oscar@Oscars-MBP JSONSchema % julia +nightly
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.12.0-DEV.313 (2024-04-07)
 _/ |\__'_|_|_|\__'_|  |  Commit c5a3b653353 (0 days old master)
|__/                   |

julia> Base.StringVector(UInt64(2))
ERROR: TypeError: in new, expected Tuple{Int64}, got a value of type Tuple{UInt64}
Stacktrace:
 [1] view
   @ ./genericmemory.jl:312 [inlined]
 [2] StringVector(n::UInt64)
   @ Base ./iobuffer.jl:45
 [3] top-level scope
   @ REPL[1]:1

julia> exit()
(base) oscar@Oscars-MBP JSONSchema % julia         
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.10.2 (2024-03-01)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> Base.StringVector(UInt64(2))
2-element Vector{UInt8}:
 0x50
 0x47

Example: https://github.com/quinnj/JSON3.jl/blob/6429adafd02a7ad07fa80e9a0ad21b098635244c/src/write.jl#L32

MathOptInterface failing on nightly: https://github.com/jump-dev/MathOptInterface.jl/actions/runs/8591539106/job/23540421403?pr=2464

image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for reporting. I can confirm that this is a bug and this PR introduced it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR fixing this: #53991

@longemen3000
Copy link
Contributor

longemen3000 commented Apr 9, 2024

there is also a bug on CSV.jl that seems to be activated with this PR, but i suppose that is for the same reasons as the last bug (CSV really wants a Vector{UInt8})

@odow
Copy link
Contributor

odow commented Apr 9, 2024

there is also a bug on CSV.jl that seems to be activated with this PR

Can you elaborate? Reproducible example and stack trace?

@longemen3000
Copy link
Contributor

longemen3000 commented Apr 9, 2024

Can you elaborate? Reproducible example and stack trace?

The main problem is that CSV.jl depended on Base.wrap, that was removed in this PR. but the dependency was just to fix another bug, so i already made a PR fixing the breakage (JuliaData/CSV.jl#1133)

@KristofferC KristofferC mentioned this pull request Apr 9, 2024
41 tasks
KristofferC pushed a commit that referenced this pull request Apr 9, 2024
- Make reshape and view with one based indexing on Memory produce Arrays
- delete wrap

Implements
#53552 (comment)

---------

Co-authored-by: Jameson Nash <vtjnash@gmail.com>
(cherry picked from commit 273d91e)
KristofferC added a commit that referenced this pull request Apr 9, 2024
Backported PRs:
- [x] #53757 <!-- Add an IndexStyle example to the diagind docstring -->
- [x] #53809 <!-- Add missing GC_POP() in emit_cfunction -->
- [x] #53789 <!-- also check that UUID of project is non-null when
treating it as a package -->
- [x] #53805 <!-- precompilepkgs: simplify custom config printing if
only one -->
- [x] #53822 <!-- Bump libuv -->
- [x] #53837 <!-- update MPFR to 4.2.1 -->
- [x] #53862 <!-- precompilepkgs: fix error reporting -->
- [x] #53774 <!-- Remove some duplicates from emitted compilation traces
-->
- [ ] #53696 <!-- add invokelatest to on_done callback in bracketed
paste -->
- [x] #53383 <!-- Add `_unsetindex!` methods for `SubArray`s and
`CartesianIndex`es -->
- [x] #53475 <!-- Fix boundscheck in unsetindex for SubArrays -->
- [x] #53888 
- [x] #53870 <!-- Revert change to checksum for llvm-julia -->
- [x] #53906 <!-- Add `Base.isrelocatable(pkg)` -->
- [x] #53833 <!-- Profile: make heap snapshots viewable in vscode viewer
-->
- [x] #53961 <!-- `LazyString` in `LinearAlgebra.checksquare` error
message -->
- [x] #53962 <!-- Use StringMemory instead of StringVector where
possible -->
- [x] #53825 <!-- profile: doc: update the `Allocs.@profile` doc string
-->
- [x] #53975 <!-- `LazyString` in `DimensionMismatch` error messages in
broadcasting -->
- [x] #53905 <!-- Avoid repeated precompilation when loading from
non-relocatable cachefiles -->
- [x] #53896 <!-- Make reshape and view on Memory produce Arrays and
delete wrap -->
- [x] #53991 <!-- Test and fix non-int-length bug in `view(::Memory,
::Union{UnitRange, Base.OneTo})` -->
ViralBShah pushed a commit to JuliaData/CSV.jl that referenced this pull request Apr 9, 2024
* fix breakage caused by JuliaLang/julia/pull/53896

* make __wrap compatible with 1.11 RC
@KristofferC KristofferC removed the backport 1.11 Change should be backported to release-1.11 label Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

wrap is a very generic name to export
8 participants