Closed mlubin closed 10 years ago
Ha @mlubin you mad man. I like it. I mean, its unsafe in some sense, but its user facing so we know its always correct.
Two questions:
pointer
may not be implemented for an arbitrary AbstractArray
type.VectorView
does not count as a reference).You might be able to mitigate some issues by storing a reference to the array (rather than a pointer). But I confess I've used similar tricks in some places in my own code.
Will this only be used on Arrays? pointer may not be implemented for an arbitrary AbstractArray type.
I'll probably settle on DenseVector
.
Presumably you're aware that you need to maintain a reference to the underlying array, to keep it from being GCed (passing a VectorView does not count as a reference).
The vector view isn't stored anywhere, so the original vector will always have a longer lifespan.
I tried storing a reference to the original array, but that seemed to trigger extra allocations.
I tried storing a reference to the original array, but that seemed to trigger extra allocations.
I think when I tried something like this, I found the same thing. So this seems like a reasonable solution.
Cool, thanks for the feedback @timholy
Gives a 3.3x speedup on a model with 1,000,000 constraints, which means that we create 1,000,000 array views on every function call. Any allocations in this loop are bad news given the quadratic performance of the GC. @timholy, do you know of any safer way to make a zero-allocation array view object? @IainNZ @joehuchette