What is Base.maybeview?

I found usage of Base.maybeview in LSHFunctions. What is it? (no docs). How is it different from view? thanks.

It is documented in the source:

maybeview is like getindex, but returns a view for slicing operations
(while remaining equivalent to getindex for scalar indices and non-array types)

yes I saw that piece, but not understand …

could an example be given? (that could not be replaced by view). thanks.

It handles scalar indexes and non-array types.

Incidentally, it is an internal function in rather old code. You should not be using it outside Base. If you are making a PR to this code and want to remove/extend it, ask in the code review.

the problem is it’s used inside LSHFunctions where I found that usage could be replaced by view

and I’m trying to make an in-place version of LSHFunctions so wanna to understand every line of code there…

I imagine you would get more specific answers if you asked the maintainers of that package.

That said, I can’t find maybeview at all in that repository.

1 Like

It’s the difference between:

julia> x = rand(1,3)
1×3 Matrix{Float64}:
 0.767098  0.88126  0.19787

julia> view(x, 1, 1)
0-dimensional view(::Matrix{Float64}, 1, 1) with eltype Float64:
0.7670979357324583

julia> @views x[1,1]
0.7670979357324583

The latter is internally doing Base.maybeview and returning a scalar value directly instead of a 0-dimensional array.

6 Likes
julia> using LSHFunctions

julia> v = randn(3);

julia> hashfun = L2Hash(5);

julia> hashfun(v)
5-element Array{Int32,1}:
 -1
  9
  2
 -3
 -6

julia> @enter hashfun(v)
In LpHash(x) at /Users/Thomas/.julia/packages/LSHFunctions/A9iRT/src/hashes/lphash.jl:221
  4  │   %4  = (LSHFunctions.current_max_input_size)(hashfn)
  5  │   %5  = (>)(%3, %4)
  6  └──       goto #3 if not %5
  7  2 ─       (resize!)(hashfn, n)
  8  3 ┄       S#274 = (getproperty)(hashfn, :coeff)
  9  │   %9  = S#274
 10  │   %10 = (lastindex)(S#274, 1)
 11  │   %11 = (Colon())(1, %10)
 12  │   %12 = (Colon())(1, n)
 >13  │   %13 = (Base.maybeview)(%9, %11, %12)
 14  │         h = (*)(%13, x)
 15  │   %15 = h

in the above, %9 is a float32 concrete matrix coeff; %13 is the maybeview of this matrix, which, I don’t understand why a maybeview (or even a view) is needed for the subsequent matrix multiplication h = (*)(%13, x)

#####################
:smiley:problem solved by @mbauman above: use of maybeview here is to enable the switch to scalar multiplication (from matrix multiplication) when x has length 1.

thanks @mbauman !

julia> aa = randn(3, 2)
3×2 Array{Float64,2}:
 1.02029   -0.0648397
 0.401972   1.44311
 1.0134     0.881608

julia> view(aa, 1, 1)
0-dimensional view(::Array{Float64,2}, 1, 1) with eltype Float64:
1.0202872251901545

julia> Base.maybeview(aa, 1, 1)
1.0202872251901545

julia> view(aa, 1, 1:2)
2-element view(::Array{Float64,2}, 1, 1:2) with eltype Float64:
  1.0202872251901545
 -0.06483974101638355

julia> Base.maybeview(aa, 1, 1:2)
2-element view(::Array{Float64,2}, 1, 1:2) with eltype Float64:
  1.0202872251901545
 -0.06483974101638355

Note that Base.maybeview is probably not used directly by LHSFunctions, you are showing already pre-compiled code. Some other public Base function was probably inlined putting maybeview there. It probably should not be used directly.

3 Likes