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

improve performance for ascii(::String) #29609

Merged
merged 1 commit into from
Oct 16, 2018
Merged

improve performance for ascii(::String) #29609

merged 1 commit into from
Oct 16, 2018

Conversation

KristofferC
Copy link
Member

@KristofferC KristofferC commented Oct 12, 2018

julia> using Random

julia> s = randstring(10000);

# Before
julia> @btime ascii(s);
  7.886 μs (0 allocations: 0 bytes)

# After
julia> @btime ascii(s);
  4.093 μs (0 allocations: 0 bytes)

@KristofferC KristofferC added performance Must go faster strings "Strings!" labels Oct 12, 2018
@JeffBezanson
Copy link
Member

Is the improvement due to the inbounds or moving the throw or both? Might be something we should be compiling better here.

@KristofferC
Copy link
Member Author

Both, e.g.

 function ascii2(s::String)
   for i = 1:sizeof(s)
       b = @inbounds codeunit(s,i)
       b < 0x80 || throw(ArgumentError("invalid ASCII at index $i in $(repr(s))"))
   end
   return s
end

function ascii3(s::String)
   for i = 1:sizeof(s)
       b = @inbounds codeunit(s,i)
       b < 0x80 || __throw_invalid_ascii(s, i)
   end
   return s
end

@noinline __throw_invalid_ascii(s, i) = throw(ArgumentError("invalid ASCII at index $i in $(repr(s))"))
julia> @btime ascii2("a")
  7.395 ns (0 allocations: 0 bytes)
"a"

julia> @btime ascii3("a")
  4.451 ns (0 allocations: 0 bytes)
"a"

Guessing it is setting up gc frames and stuff, ref #23281.

@KristofferC KristofferC merged commit b72fc4c into master Oct 16, 2018
@KristofferC KristofferC deleted the kc/perf_ascii branch October 16, 2018 13:11
@KristofferC KristofferC added the potential benchmark Could make a good benchmark in BaseBenchmarks label Oct 16, 2018
KristofferC added a commit that referenced this pull request Oct 29, 2018
KristofferC added a commit that referenced this pull request Oct 31, 2018
KristofferC added a commit that referenced this pull request Nov 2, 2018
KristofferC added a commit that referenced this pull request Feb 11, 2019
KristofferC added a commit that referenced this pull request Feb 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Must go faster potential benchmark Could make a good benchmark in BaseBenchmarks strings "Strings!"
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants