-
Notifications
You must be signed in to change notification settings - Fork 152
Allow @SVector comprehension to eval range with locals? #26
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
Comments
Normally However, the second version can be written directly with a generator which isn't too bad: function mytransform{N,T}(x::SVector{N,T})
SVector{N,T}(x[i]*i for i=1:N)
end From a quick look at code_native, it seems that a call is still being made so this probably won't be super fast. It may be possible to improve the codegen with an extra generated function, to unroll once |
@dbeach24 This is something I'd like to do but as @c42f mentions its a little difficult with current compiler support. I have an idea for one hack which would end up using a call and a loop yet AFAIK it still should be faster to write
as @c42f we need an Another approach is to have a statically valued thing like To my understanding, this one:
should be performant up until N = 16 and slow afterwards, The compiler people are interested in making this fast for all N (a longer term goal, however). |
I've started to look at this in #30 |
OK I've had a thought. First we implement function initialization for all SVector{N}(f::Function) populates Furthermore, transform this: @SVector [f(i) for i = inds] into this N = size(inds) # can we check/enforce if this is statically determinable? E.g. size(typeof(inds)) is OK.
SVector{N}(j -> f(inds[i])) We can assume the It would help syntactically if we made |
BTW @dbeach24 the function mytransform{N,T}(x::SVector{N,T})
SVector{N,T}(ntuple(i -> x[i] * i, N) ...)
end So this is fast (up to N = 15 or so) function mytransform{N,T}(x::SVector{N,T})
SVector{N,T}(ntuple(i -> x[i] * i, Val{N}))
end |
The construct in question function mytransform{N}(x::SVector{N})
@SVector [x[i] * i for i=1:N]
end can be written without the macro like this: function mytransform{N}(x::SVector{N})
SVector([x[i] * i for i=1:N]...)
end Though i feel like the |
That would allocate a temporary array as well as causing the output type to not be inferrable. |
Slightly OT: In two comments above it is mentioned that the following workaround should work function mytransform{N,T}(x::SVector{N,T})
SVector{N,T}(x[i]*i for i=1:N)
end however, when I call it as Did passing a generator to the |
We've never robustly supported generators in the past. It's something I'd like to improve on, though. Hmm... |
I think this issue is basically the same as #97 |
Note that |
* add pair constructor * add StructVector * added empty and indexstyle * export StructVector * add columns colnames and ncols * test columns * test empty
Because the
@SVector
macro does not evaluate the range using the local scope, I find myself transforming code like this:into this:
I'm not sure if the second form is as efficient, but in either case, the first version is far easier to read.
Is there any chance that
@SVector
comprehensions can be made to support local variables in the range? Or, is there some other way I should be writing this?The text was updated successfully, but these errors were encountered: