-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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 number to string conversion functions #28661
Conversation
Noticed this when looking into the |
The following patch also manages to speed it up: diff --git a/base/char.jl b/base/char.jl
index 700c61db27c..d071a3988bd 100644
--- a/base/char.jl
+++ b/base/char.jl
@@ -108,10 +108,14 @@ and [`show_invalid`](@ref).
"""
isoverlong(c::AbstractChar) = false
-function UInt32(c::Char)
+@inline function UInt32(c::Char)
# TODO: use optimized inline LLVM
u = reinterpret(UInt32, c)
u < 0x80000000 && return u >> 24
+ return _uint32(u)
+end
+
+@noinline function _uint32(u::UInt32)
l1 = leading_ones(u)
t0 = trailing_zeros(u) & 56
(l1 == 1) | (8l1 + t0 > 32) |
@@ -142,8 +146,12 @@ that support overlong encodings should implement `Base.decode_overlong`.
"""
decode_overlong
-function Char(u::UInt32)
+@inline function Char(u::UInt32)
u < 0x80 && return reinterpret(Char, u << 24)
+ return _char(u)
+end
+
+@noinline function _char(u::UInt32)
u < 0x00200000 || code_point_err(u)::Union{}
c = ((u << 0) & 0x0000003f) | ((u << 2) & 0x00003f00) |
((u << 4) & 0x003f0000) | ((u << 6) & 0x3f000000) It is a bit more general but I guess might slow things down if the old |
That is great. Any chance there's a similar fix for a performance regression in the print_to_file microbenchmark? It went from 6.86 in julia-0.6.4 to 10.8 in julia-0.7.0 and 1.0.0. The core of the benchmark is
|
Maybe use |
There are, in general, |
That kills performance. |
For now, I think we should go with this "local" performance improvement and then we can look at a perhaps more general but more intrusive change like #28661 (comment). |
Argh, I've been using this type of pattern. Isn't the conversion done at compile time? Why would it be worse than just '0'? |
It should be and in some cases it does.
But for some reason, it doesn't seem to happen here. Not sure why. |
Typically, we handle that case by writing it thusly: @eval function f() = begin
some_computation($(some_constant_computation(some_constants)))
end |
* improve performance for number to string conversion functions (cherry picked from commit 472fe5f)
* improve performance for number to string conversion functions (cherry picked from commit 472fe5f)
* improve performance for number to string conversion functions (cherry picked from commit 472fe5f)
* improve performance for number to string conversion functions (cherry picked from commit 472fe5f)
* improve performance for number to string conversion functions (cherry picked from commit 472fe5f)
* improve performance for number to string conversion functions (cherry picked from commit 472fe5f)
This is kinda silly but it is what it is...
Benchmark:
Before
After