Skip to content

Commit

Permalink
Use StringMemory to convert integer to string ~20% faster
Browse files Browse the repository at this point in the history
On `1.11.0-alpha2`
Old:
```julia
@benchmark Base.dec($0x1, $0, $false)
BenchmarkTools.Trial: 10000 samples with 994 evaluations.
 Range (min … max):  33.702 ns …   4.242 μs  ┊ GC (min … max):  0.00% … 97.61%
 Time  (median):     37.626 ns               ┊ GC (median):     0.00%
 Time  (mean ± σ):   45.787 ns ± 147.794 ns  ┊ GC (mean ± σ):  14.53% ±  4.47%

    ▄▅▆▇█▇▇▅▃▃▂▂▂▁    ▁▂▁▁▁             ▁▁   ▁                 ▂
  ▄▇███████████████▇▇██████▇█▆▆▄▄▃▄▅▄▆▇████████▆▅▅▇▆▅▆▄▄▅▄▄▄▁▅ █
  33.7 ns       Histogram: log(frequency) by time      67.5 ns <

 Memory estimate: 88 bytes, allocs estimate: 3.
```
New:
```julia
BenchmarkTools.Trial: 10000 samples with 995 evaluations.
 Range (min … max):  27.538 ns …   3.397 μs  ┊ GC (min … max):  0.00% … 97.86%
 Time  (median):     30.151 ns               ┊ GC (median):     0.00%
 Time  (mean ± σ):   34.547 ns ± 105.101 ns  ┊ GC (mean ± σ):  10.37% ±  3.39%

       ▁ █▆▃  ▁
  ▂▂▃▃▅█████▆████▆▄▄▃▃▃▃▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▂▂▂▂▂▁▂▂▂▂▂▂▂▂▂▂▂▂▂ ▃
  27.5 ns         Histogram: frequency by time         43.8 ns <

 Memory estimate: 56 bytes, allocs estimate: 2.
```


Fixes JuliaLang#53950, actually now even faster than `1.10.2`.
  • Loading branch information
Zentrik authored Apr 5, 2024
1 parent 5f4dec1 commit 6d4b192
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ ndigits(x::Integer; base::Integer=10, pad::Integer=1) = max(pad, ndigits0z(x, ba
function bin(x::Unsigned, pad::Int, neg::Bool)
m = top_set_bit(x)
n = neg + max(pad, m)
a = StringVector(n)
a = StringMemory(n)
# for i in 0x0:UInt(n-1) # automatic vectorization produces redundant codes
# @inbounds a[n - i] = 0x30 + (((x >> i) % UInt8)::UInt8 & 0x1)
# end
Expand All @@ -769,7 +769,7 @@ end
function oct(x::Unsigned, pad::Int, neg::Bool)
m = div(top_set_bit(x) + 2, 3)
n = neg + max(pad, m)
a = StringVector(n)
a = StringMemory(n)
i = n
while i > neg
@inbounds a[i] = 0x30 + ((x % UInt8)::UInt8 & 0x7)
Expand Down Expand Up @@ -844,7 +844,7 @@ end

function dec(x::Unsigned, pad::Int, neg::Bool)
n = neg + ndigits(x, pad=pad)
a = StringVector(n)
a = StringMemory(n)
append_c_digits_fast(n, x, a, 1)
neg && (@inbounds a[1] = 0x2d) # UInt8('-')
String(a)
Expand All @@ -853,7 +853,7 @@ end
function hex(x::Unsigned, pad::Int, neg::Bool)
m = 2 * sizeof(x) - (leading_zeros(x) >> 2)
n = neg + max(pad, m)
a = StringVector(n)
a = StringMemory(n)
i = n
while i >= 2
b = (x % UInt8)::UInt8
Expand All @@ -880,7 +880,7 @@ function _base(base::Integer, x::Integer, pad::Int, neg::Bool)
b = (base % Int)::Int
digits = abs(b) <= 36 ? base36digits : base62digits
n = neg + ndigits(x, base=b, pad=pad)
a = StringVector(n)
a = StringMemory(n)
i = n
@inbounds while i > neg
if b > 0
Expand Down Expand Up @@ -956,7 +956,7 @@ julia> bitstring(2.2)
function bitstring(x::T) where {T}
isprimitivetype(T) || throw(ArgumentError("$T not a primitive type"))
sz = sizeof(T) * 8
str = StringVector(sz)
str = StringMemory(sz)
i = sz
@inbounds while i >= 4
b = UInt32(sizeof(T) == 1 ? bitcast(UInt8, x) : trunc_int(UInt8, x))
Expand Down

0 comments on commit 6d4b192

Please sign in to comment.