Skip to content

Immutable Views #36

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

Closed
rofinn opened this issue Oct 15, 2015 · 5 comments
Closed

Immutable Views #36

rofinn opened this issue Oct 15, 2015 · 5 comments

Comments

@rofinn
Copy link

rofinn commented Oct 15, 2015

I think there are a lot of situations where it would be nice to have an immutable view. I find it very useful to return ArrayViews when "querying" a larger Array, however, I'd like to make those views read-only. This is mostly for my own sanity for now, but I could see this being more important once threading gets worked into base julia.

I've started an initial attempt at this here using a simple mutable keyword.

Example:

julia> using ArrayViews

julia> a = rand(10)
10-element Array{Float64,1}:
 0.890665
 0.842478
 0.324603
 0.670892
 0.288664
 0.310353
 0.593505
 0.440294
 0.7016
 0.922143

julia> v = view(a, 1:5)
5-element ArrayViews.ContiguousView{Float64,1,Array{Float64,1}}:
 0.890665
 0.842478
 0.324603
 0.670892
 0.288664

julia> v[1] = 0.0
0.0

julia> a
10-element Array{Float64,1}:
 0.0
 0.842478
 0.324603
 0.670892
 0.288664
 0.310353
 0.593505
 0.440294
 0.7016

or

julia> a = rand(10)
10-element Array{Float64,1}:
 0.607706
 0.397484
 0.451087
 0.717503
 0.469827
 0.204694
 0.0758664
 0.486908
 0.0546228
 0.910502

julia> v = view(a, 1:5; mutable=false)
5-element ArrayViews.ContiguousView{Float64,1,Array{Float64,1}}:
 0.607706
 0.397484
 0.451087
 0.717503
 0.469827

julia> v[1] = 0.0
ERROR: Setting elements of an immutable ArrayView is not allowed.
 in uset! at /Users/rory/repos/ArrayViews.jl/src/arrviews.jl:107
 in setindex! at /Users/rory/repos/ArrayViews.jl/src/indexing.jl:122

I'm not sure if this is the best way to do this in the long run, but for now it avoids manipulating the type hierarchy or breaking any existing functionality.

@timholy
Copy link
Member

timholy commented Oct 15, 2015

@Rory-Finnegan, now that julia 0.4 is out, is there any reason not to use SubArrays? This package was started before there were fast views built into julia.

One advantage of starting with SubArray is that it can form a view of an arbitrary AbstractArray; so all you'd need to do is define a ReadOnlyArray type and then make a SubArray of it.

@rofinn
Copy link
Author

rofinn commented Oct 15, 2015

@timholy, thanks, I didn't realize SubArrays were as fast as ArrayViews now. I guess in order to make any array type (eg: dense, sparse, shared, etc) immutable I'd need to implement an ImmutableArray that wraps an arbitrary AbstractArray like how the SharedArray wraps an Array?

@rofinn
Copy link
Author

rofinn commented Oct 15, 2015

Oh, the other issue is that I don't want the parent array to be immutable, just the view. Making a ReadOnlyArray would then require 2 wrappers 1 for the ReadOnlyArray/ImmutableArray and then another for the SubArray, which seems rather verbose. Either way I'll close the issue here, since either way I'll want to use SubArrays.

@rofinn rofinn closed this as completed Oct 15, 2015
@nalimilan
Copy link

A read-only mode could also be useful to ease the transition to returning array views by default from getindex: any existing code modifying the result would then trigger an error, making it easy to spot the places that need a call to copy.

@timholy
Copy link
Member

timholy commented Oct 15, 2015

Making a ReadOnlyArray would then require 2 wrappers 1 for the ReadOnlyArray/ImmutableArray and then another for the SubArray, which seems rather verbose.

Are you worried about usage verbosity or something different? In your own code you could define a one-liner helper function

subro(A, indexes...) = sub(readonly(A), indexes...)

if you wanted. Since the two concepts are orthogonal, to me it seems to be an advantage to keep them well-separated---you could make a ReadOnly full sized-view, if you needed to.

If you're worried about the implications for performance, my bet is that all of this will get elided by the compiler, except the splatting penalty. If that's problematic, you can use a @generated function if necessary (see JuliaLang/julia#13359).

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

No branches or pull requests

3 participants