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

Efficient SMatrix SubArrays #17

Closed
tkoolen opened this issue Aug 12, 2016 · 7 comments
Closed

Efficient SMatrix SubArrays #17

tkoolen opened this issue Aug 12, 2016 · 7 comments

Comments

@tkoolen
Copy link
Contributor

tkoolen commented Aug 12, 2016

Maybe this is an issue for Base, but I thought I'd get your input first.

I have an application where I know the maximum size of a matrix at compile time, but the number of columns is only known at runtime. I was hoping that combining SMatrix and view would solve this problem. Making a view of an SMatrix works fine (awesome!), but there's a performance issue.

Consider the following code:

using StaticArrays
function view_colon()
    data = rand(SMatrix{3, 3, Float64})
    view(data, :, 1:2)
end

function view_range()
    data = rand(SMatrix{3, 3, Float64})
    view(data, 1:3, 1:2)
end

Base.linearindexing(view_colon()) # Base.LinearFast()
Base.linearindexing(view_range()) # Base.LinearSlow()

isbits(view_colon()) # false
isbits(view_range()) # true

So currently, I have to choose between a LinearFast view that is not isbits (resulting in lots of allocations down the line), or a LinearSlow view that is.

The only reason the colon version is not isbits seems to be that Colon is a type, instead of being immutable. So would it be possible to change that in Base without breaking things? Or should this be handled in StaticArrays instead?

@timholy
Copy link
Member

timholy commented Aug 14, 2016

Please do fix this in Base. Thanks for noticing this!

@andyferris
Copy link
Member

Oh dear... Colon is a type not an immutable. Seems like a mistake in Base to me, also.

@andyferris
Copy link
Member

In fact, it's an easy enough mistake to make that I think the compiler should probably treat all field-less types as isbits singletons. I can't see the point of having a mutable singleton... It seems that Colon.instance is defined so maybe this is a pretty trivial fix for isbits? Or am I overlooking something?

@andyferris
Copy link
Member

For now @tkoolen, have you considered using a Vector of SVectors, or else a full SVector and keep track of how many elements you use separately? Maybe this would be good enough for the mean-time.

@tkoolen
Copy link
Contributor Author

tkoolen commented Aug 15, 2016

Thanks. I'm not in a hurry, so I'd prefer to have this be fixed in Base rather than hack around it. I'll submit a PR to Base once I'm done running tests.

@tkoolen
Copy link
Contributor Author

tkoolen commented Aug 15, 2016

Fixed in base.

@tkoolen tkoolen closed this as completed Aug 15, 2016
@andyferris
Copy link
Member

Nice work! Thanks

oschulz pushed a commit to oschulz/StaticArrays.jl that referenced this issue Apr 4, 2023
* remove createinstance

* Make skipping constructor opt in
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