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

specialize length(SubString) for DirectIndexString #7145

Merged
merged 3 commits into from
Jun 6, 2014
Merged

specialize length(SubString) for DirectIndexString #7145

merged 3 commits into from
Jun 6, 2014

Conversation

catawbasam
Copy link
Contributor

passes 'make clean testall'

Double-checked code_typed( length,(SubString{ASCIIString},) ) to verify that this one actually affects execution :/

Current code_typed() output:

1-element Array{Any,1}:
 :($(Expr(:lambda, {:s}, {{:i,:n,:#s406,:#s404,:c,:#s403,:j,:#s405,:_var0,:_var1,:_var2,:_var3},{{:s,SubString{ASCIIString},0},{:i,Int64,2},{:n,Int64,2},{:#s406,(Char,Int64),18},{:#s404,(Char,Int64),18},{:c,Char,18},{:#s403,(Int64,Int64),18},{:j,Int64,18},{:#s405,Int64,2},{:_var0,Char,18},{:_var1,Int64,18},{:_var2,Int64,18},{:_var3,Int64,18}},{}}, :(begin  # string.jl, line 81:
        i = 1 # line 82:
        unless slt_int(top(getfield)(s::SubString{ASCIIString},:endof)::Int64,i::Int64)::Bool goto 0 # line 83:
        return 0
        0:  # line 85:
        n = 1 # line 86:
        unless true goto 2
        3:  # line 87:
        #s406 = next(s::SubString{ASCIIString},i::Int64)::(Char,Int64)
        #s405 = 1
        _var0 = tupleref(#s406::(Char,Int64),1)::Union(Int64,Char)
        _var1 = box(Int64,add_int(1,1))::Int64
        c = _var0::Char
        #s405 = _var1::Int64
        _var2 = tupleref(#s406::(Char,Int64),2)::Union(Int64,Char)
        _var3 = box(Int64,add_int(2,1))::Int64
        j = _var2::Int64
        #s405 = _var3::Int64 # line 88:
        unless slt_int(top(getfield)(s::SubString{ASCIIString},:endof)::Int64,j::Int64)::Bool goto 5 # line 89:
        return n::Int64
        5:  # line 91:
        n = box(Int64,add_int(n::Int64,1))::Int64 # line 92:
        i = j::Int64
        4: 
        unless false goto 3
        2: 
        1: 
        return
    end::Int64))))

Proposed code_typed() output:

1-element Array{Any,1}:
 :($(Expr(:lambda, {:s}, {{},{{:s,SubString{ASCIIString},0}},{}}, :(begin  # In[65], line 1:
        return top(getfield)(s::SubString{ASCIIString},:endof)::Int64
    end::Int64))))

@JeffBezanson
Copy link
Sponsor Member

Nice!

@StefanKarpinski
Copy link
Sponsor Member

Yes, this is clearly a big improvement. I'm trying to think if situations where this isn't true, but I can't think of any. I think the fact that the index of the i character is i implies that endof(s) == length(s).

@catawbasam
Copy link
Contributor Author

Below are a few tests. I'd be happy to add them to test/strings.jl -- but have no experience piling changes onto an open pull request. Is that a safe thing to do?

julia> using Base.Test

julia> str="tempus fugit"
"tempus fugit"

julia> ss=SubString(str,1,length(str))
"tempus fugit"
julia> @test length(ss)==length(str)

julia> ss=SubString(str,1,0)
""
julia> @test length(ss)==0

julia> ss=SubString(str,14,20)
""
julia> @test length(ss)==0

julia> ss=SubString(str,10,16)
"git"
julia> @test length(ss)==3

julia> str2=""
""

julia> ss=SubString(str2,1,4)
""
julia> @test length(ss)==0

julia> ss=SubString(str2,1,1)
""
julia> @test length(ss)==0

@JeffBezanson
Copy link
Sponsor Member

Yes, you can keep pushing to the same branch and the new commits will appear as part of this pull request.

@catawbasam
Copy link
Contributor Author

OK. tests pushed.

JeffBezanson added a commit that referenced this pull request Jun 6, 2014
specialize length(SubString) for DirectIndexString
@JeffBezanson JeffBezanson merged commit f8f7a0f into JuliaLang:master Jun 6, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Must go faster
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants