Skip to content

Commit 0e59c9e

Browse files
authored
Use StringMemory instead of StringVector where possible (#53962)
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 #53950, actually now even faster than `1.10.2`. It doesn't look like the length is ever changed and we don't return these `StringMemory`s so this change should be fine.
1 parent d505c8c commit 0e59c9e

File tree

5 files changed

+10
-10
lines changed

5 files changed

+10
-10
lines changed

base/gmp.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ function string(n::BigInt; base::Integer = 10, pad::Integer = 1)
754754
iszero(n) && pad < 1 && return ""
755755
nd1 = ndigits(n, base=base)
756756
nd = max(nd1, pad)
757-
sv = Base.StringVector(nd + isneg(n))
757+
sv = Base.StringMemory(nd + isneg(n))
758758
GC.@preserve sv MPZ.get_str!(pointer(sv) + nd - nd1, base, n)
759759
@inbounds for i = (1:nd-nd1) .+ isneg(n)
760760
sv[i] = '0' % UInt8

base/intfuncs.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ ndigits(x::Integer; base::Integer=10, pad::Integer=1) = max(pad, ndigits0z(x, ba
742742
function bin(x::Unsigned, pad::Int, neg::Bool)
743743
m = top_set_bit(x)
744744
n = neg + max(pad, m)
745-
a = StringVector(n)
745+
a = StringMemory(n)
746746
# for i in 0x0:UInt(n-1) # automatic vectorization produces redundant codes
747747
# @inbounds a[n - i] = 0x30 + (((x >> i) % UInt8)::UInt8 & 0x1)
748748
# end
@@ -769,7 +769,7 @@ end
769769
function oct(x::Unsigned, pad::Int, neg::Bool)
770770
m = div(top_set_bit(x) + 2, 3)
771771
n = neg + max(pad, m)
772-
a = StringVector(n)
772+
a = StringMemory(n)
773773
i = n
774774
while i > neg
775775
@inbounds a[i] = 0x30 + ((x % UInt8)::UInt8 & 0x7)
@@ -844,7 +844,7 @@ end
844844

845845
function dec(x::Unsigned, pad::Int, neg::Bool)
846846
n = neg + ndigits(x, pad=pad)
847-
a = StringVector(n)
847+
a = StringMemory(n)
848848
append_c_digits_fast(n, x, a, 1)
849849
neg && (@inbounds a[1] = 0x2d) # UInt8('-')
850850
String(a)
@@ -853,7 +853,7 @@ end
853853
function hex(x::Unsigned, pad::Int, neg::Bool)
854854
m = 2 * sizeof(x) - (leading_zeros(x) >> 2)
855855
n = neg + max(pad, m)
856-
a = StringVector(n)
856+
a = StringMemory(n)
857857
i = n
858858
while i >= 2
859859
b = (x % UInt8)::UInt8
@@ -880,7 +880,7 @@ function _base(base::Integer, x::Integer, pad::Int, neg::Bool)
880880
b = (base % Int)::Int
881881
digits = abs(b) <= 36 ? base36digits : base62digits
882882
n = neg + ndigits(x, base=b, pad=pad)
883-
a = StringVector(n)
883+
a = StringMemory(n)
884884
i = n
885885
@inbounds while i > neg
886886
if b > 0
@@ -956,7 +956,7 @@ julia> bitstring(2.2)
956956
function bitstring(x::T) where {T}
957957
isprimitivetype(T) || throw(ArgumentError("$T not a primitive type"))
958958
sz = sizeof(T) * 8
959-
str = StringVector(sz)
959+
str = StringMemory(sz)
960960
i = sz
961961
@inbounds while i >= 4
962962
b = UInt32(sizeof(T) == 1 ? bitcast(UInt8, x) : trunc_int(UInt8, x))

base/strings/util.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ function bytes2hex end
10611061

10621062
function bytes2hex(itr)
10631063
eltype(itr) === UInt8 || throw(ArgumentError("eltype of iterator not UInt8"))
1064-
b = Base.StringVector(2*length(itr))
1064+
b = Base.StringMemory(2*length(itr))
10651065
@inbounds for (i, x) in enumerate(itr)
10661066
b[2i - 1] = hex_chars[1 + x >> 4]
10671067
b[2i ] = hex_chars[1 + x & 0xf]

base/uuid.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ let groupings = [36:-1:25; 23:-1:20; 18:-1:15; 13:-1:10; 8:-1:1]
9090
global string
9191
function string(u::UUID)
9292
u = u.value
93-
a = Base.StringVector(36)
93+
a = Base.StringMemory(36)
9494
for i in groupings
9595
@inbounds a[i] = hex_chars[1 + u & 0xf]
9696
u >>= 4

stdlib/FileWatching/src/pidfile.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ function open_exclusive(path::String;
280280
end
281281

282282
function _rand_filename(len::Int=4) # modified from Base.Libc
283-
slug = Base.StringVector(len)
283+
slug = Base.StringMemory(len)
284284
chars = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
285285
for i = 1:len
286286
slug[i] = chars[(Libc.rand() % length(chars)) + 1]

0 commit comments

Comments
 (0)