julia icon indicating copy to clipboard operation
julia copied to clipboard

Rename Memory wrapper from `view` to `unsafe_vector`

Open nhz2 opened this issue 9 months ago • 8 comments

Fixes #54156 Changes part of #53896

The main difference between view and unsafe_vector is that the result of view has a fixed size, and creates a copy when passed to String.

The String(view( pattern is fairly common: https://juliahub.com/ui/Search?q=String(view&type=code and as more packages start using Memory it may be hard to tell if view is being used safely or not.

I'm not sure about the name unsafe_vector, but the docstring says

It is only safe to resize v if m is subseqently not used.

So I added an unsafe_ prefix.

Ref https://github.com/JuliaLang/julia/issues/53552#issuecomment-2024288283 for a discussion on why view on a Memory should produce an Array.

nhz2 avatar Apr 26 '24 16:04 nhz2

If this gets approved, it might be worth adding a "See also:" reference to unsafe_vector in the view docstring.

christiangnrd avatar Apr 26 '24 17:04 christiangnrd

Nit: nothing about this is unsafe, so we could call it wrap as the counter part to unsafe_wrap

vtjnash avatar Apr 26 '24 17:04 vtjnash

wasn't it originally wrap then reverted?

https://github.com/JuliaLang/julia/pull/53896

I might be conflating issues together, didn't follow super closely

adienes avatar Apr 26 '24 17:04 adienes

Isn't Vector allowed to shift the data in its mem when resized? For example, in the future as an optimization, could doing popfirst! then push! cause the data to be shifted over by one in mem?

nhz2 avatar Apr 26 '24 18:04 nhz2

This would surely break CSV.jl, as the removal of wrap did before. I don't have a strong opinion on this, but I'm commenting to know when/if this PR is merged to add the corresponding fix

longemen3000 avatar Apr 26 '24 21:04 longemen3000

Triage named it view in https://github.com/JuliaLang/julia/issues/53552.

I don't really understand what the String(_) constructor's contract is supposed to be, especially on the issue of memory ownership and mutation.

jariji avatar Apr 26 '24 21:04 jariji

How about naming it wrap, but not export-ing it?

nsajko avatar Apr 28 '24 20:04 nsajko

"Base.wrap" doesn't seem like it means anything in particular, it's too uninformative. At least that was my view and I think triage agreed.

jariji avatar Apr 29 '24 05:04 jariji

Closing as discussed in https://github.com/JuliaLang/julia/pull/54372#issuecomment-2102885169

oscardssmith avatar May 09 '24 15:05 oscardssmith