-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
write multiple bytes of Char at once #27267 #27380
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
Conversation
|
Any tests? |
It passed |
|
Is speed for one byte Chars also better? |
function foo(io::IO, c::Char, n::Int)
for _ in 1:5
@time for _ in 1:n
write(io, c)
end
end
end
To make it more clear, I tried with 0.7.0-alpha:
Is there any better way to measure them? |
Use |
Great package! 0.7.0-alpha:
|
base/io.jl
Outdated
| n += 1 | ||
| v = reinterpret(UInt32, c) | ||
| u = hton(v) # BIG-endian | ||
| p = unsafe_convert(Ptr{Cvoid}, Ref{UInt32}(u)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this Ref needs a GC.@preserve.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it requires GC.@preserve for Ref.
function f0(c::Char)
u = hton(reinterpret(UInt32, c))
p = unsafe_convert(Ptr{Cvoid}, Ref{UInt32}(u))
show(unsafe_wrap(Array{UInt8,1}, Ptr{UInt8}(p), (4,)))
end
function f1(c::Char)
u = hton(reinterpret(UInt32, c))
GC.@preserve u begin
p = unsafe_convert(Ptr{Cvoid}, Ref{UInt32}(u))
show(unsafe_wrap(Array{UInt8,1}, Ptr{UInt8}(p), (4,)))
end
end
function f2(c::Char)
u = Ref{UInt32}(hton(reinterpret(UInt32, c)))
GC.@preserve u begin
p = unsafe_convert(Ptr{Cvoid}, u)
show(unsafe_wrap(Array{UInt8,1}, Ptr{UInt8}(p), (4,)))
end
endUInt8[0xed, 0x95, 0x9c, 0x00] must be printed:
julia> f0('한')
UInt8[0x40, 0x27, 0x3e, 0x3c]
julia> f0('한')
UInt8[0x01, 0x00, 0x00, 0x00]
julia>
julia> f1('한')
UInt8[0x20, 0x1c, 0x3e, 0x3c]
julia> f1('한')
UInt8[0x01, 0x00, 0x00, 0x00]
julia>
julia> f2('한')
UInt8[0xed, 0x95, 0x9c, 0x00]
julia> f2('한')
UInt8[0xed, 0x95, 0x9c, 0x00]
julia>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right – if @time isn't showing the extra allocation of the Ref object, this is broken (as you are demonstrating above)
I mean for your issue. e.g. '한'. I think you should create tests for this issue. |
Lines 213 to 239 in 5583ae4
Here are some tests for multibyte cases. |
| (u >>= 8) == 0 && return n | ||
| n += 1 | ||
| v = reinterpret(UInt32, c) | ||
| u = Ref{UInt32}(hton(v)) # BIG-endian |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be an unconditional bswap since UTF-8 is independent of the endianess of the system.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UTF-8 is independent of the endianess of the system.
Yes, it is true, but UInt32 isn't.
base/io.jl
Outdated
| unsafe_write(io, p, 2) | ||
| else | ||
| unsafe_write(io, p, 1) | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can replace this with something along the lines of
unsafe_write(io, p, min(4, (trailing_zeros(v) >>> 3) + 1))There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea!
StefanKarpinski
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Shame about AppVeyor timing out. Perhaps we should wait until that's sorted out and rebase since testing Windows seems fairly key here.
@StefanKarpinski @JeffBezanson One of them is from Lines 544 to 545 in b28496a
Error message from 64bit AppVeyor Error message from 32bit AppVeyor I guess the problem is garbage collected Those tests are spawning a process using |
|
@vtjnash |
|
#28314 Fixes the problem. |
Fix #27267
write()function from 0.7.0-alpha:write()function fromalkorang/display: