Skip to content

Commit

Permalink
couple of micro-optimizations for #8826
Browse files Browse the repository at this point in the history
- don't call utf8sizeof() twice when writing a Char
- faster sizeof(::SubString{ASCIIString})

About 6% faster.
  • Loading branch information
nolta committed Nov 18, 2014
1 parent fd88252 commit b38c478
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
5 changes: 3 additions & 2 deletions base/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -771,12 +771,13 @@ function write(s::AsyncStream, b::UInt8)
return 1
end
function write(s::AsyncStream, c::Char)
@uv_write utf8sizeof(c) ccall(:jl_pututf8_copy, Int32, (Ptr{Void},UInt32, Ptr{Void}, Ptr{Void}), handle(s), c, uvw, uv_jl_writecb_task::Ptr{Void})
nb = utf8sizeof(c)
@uv_write nb ccall(:jl_pututf8_copy, Int32, (Ptr{Void}, UInt32, Ptr{Void}, Ptr{Void}), handle(s), c, uvw, uv_jl_writecb_task::Ptr{Void})
ct = current_task()
uv_req_set_data(uvw,ct)
ct.state = :waiting
stream_wait(ct)
return utf8sizeof(c)
return nb
end
function write{T}(s::AsyncStream, a::Array{T})
if isbits(T)
Expand Down
3 changes: 2 additions & 1 deletion base/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,8 @@ write{T<:ByteString}(to::IOBuffer, s::SubString{T}) =
s.endof==0 ? 0 : write_sub(to, s.string.data, s.offset+1, next(s,s.endof)[2]-1)
print(io::IOBuffer, s::SubString) = write(io, s)

sizeof{T<:ByteString}(s::SubString{T}) = s.endof==0 ? 0 : next(s,s.endof)[2]-1
sizeof(s::SubString{ASCIIString}) = s.endof
sizeof(s::SubString{UTF8String}) = s.endof == 0 ? 0 : next(s,s.endof)[2]-1

# TODO: length(s::SubString) = ??
# default implementation will work but it's slow
Expand Down

0 comments on commit b38c478

Please sign in to comment.