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

function does not get inlined? #2888

Closed
tanmaykm opened this issue Apr 19, 2013 · 2 comments
Closed

function does not get inlined? #2888

tanmaykm opened this issue Apr 19, 2013 · 2 comments

Comments

@tanmaykm
Copy link
Member

Hi,

While optimizing some of my code (a ChainedVector type implementation) I encountered something that I think is an issue.

import  Base.getindex, Base.setindex!, Base.size, Base.strides, Base.stride, Base.show, Base.similar, Base.print_matrix

type ChainedVector{T} <: AbstractVector{T}
    chain::Vector{Vector{T}}
    sizes::Vector{Int}
    sz::Int

    function ChainedVector(arr::Vector{T}...)
        sizes = [ length(v) for v in arr ]
        new([v for v in arr], sizes, sum(sizes))
    end
end

size{T}(cv::ChainedVector{T}) = cv.sz

function _get_vec_pos{T}(cv::ChainedVector{T}, ind::Integer)
    cidx = 1
    while(ind > cv.sizes[cidx]); ind -= cv.sizes[cidx]; cidx += 1; end
    (cidx, ind)
end

function getindex{T}(cv::ChainedVector{T}, ind::Integer)
    cidx = 1
    while(ind > cv.sizes[cidx]); ind -= cv.sizes[cidx]; cidx += 1; end
    (cv.chain[cidx])[ind]
end

#function getindex{T}(cv::ChainedVector{T}, ind::Integer)
#    cidx, ind = _get_vec_pos(cv, ind)
#    (cv.chain[cidx])[ind]
#end


function time_cv()
    cv = ChainedVector{Int}(zeros(Int, 1024*1024*32), zeros(Int, 1024*1024*32))
    cv_time = @elapsed begin
        i = 1
        x = 0
        l = length(cv)
        while(i < l)
            x += cv[i]
            i+=1
        end
        println(x)
    end

    println("ChainedVector: $cv_time")
end 

Unmodified, the above code gives:

julia> time_cv()
0
ChainedVector: 0.541100287

With the getindex functions swapped with the commented one, it gives:

julia> time_cv()
0
ChainedVector: 8.385757686

Is this an inlining problem?

Thanks
Tanmay

@JeffBezanson
Copy link
Member

Getting a compiler to inline when you want it to is an issue in every language. We need #1106 and #2496 .

@ViralBShah
Copy link
Member

What would be nice is to have warning flags. This is a case where it would be nice to have an explicit inline keyword, and a compiler warning if inlining did not happen.

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