Skip to content

Commit 64f46b8

Browse files
committed
optimize Symbol with constant string argument
1 parent 8ff014f commit 64f46b8

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

base/boot.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,11 +431,12 @@ Array{T}(A::AbstractArray{S,N}) where {T,N,S} = Array{T,N}(A)
431431
AbstractArray{T}(A::AbstractArray{S,N}) where {T,S,N} = AbstractArray{T,N}(A)
432432

433433
# primitive Symbol constructors
434-
function Symbol(s::String)
434+
eval(Core, :(function Symbol(s::String)
435+
$(Expr(:meta, :pure))
435436
return ccall(:jl_symbol_n, Ref{Symbol}, (Ptr{UInt8}, Int),
436437
ccall(:jl_string_ptr, Ptr{UInt8}, (Any,), s),
437438
sizeof(s))
438-
end
439+
end))
439440
function Symbol(a::Array{UInt8,1})
440441
return ccall(:jl_symbol_n, Ref{Symbol}, (Ptr{UInt8}, Int),
441442
ccall(:jl_array_ptr, Ptr{UInt8}, (Any,), a),

base/compiler/utilities.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ function quoted(@nospecialize(x))
7373
end
7474

7575
function is_inlineable_constant(@nospecialize(x))
76-
x isa Type && return true
76+
if x isa Type || x isa Symbol
77+
return true
78+
end
7779
return isbits(x) && Core.sizeof(x) <= MAX_INLINE_CONST_SIZE
7880
end
7981

base/strings/string.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Convert a string to a contiguous byte array representation encoded as UTF-8 byte
6666
This representation is often appropriate for passing strings to C.
6767
"""
6868
String(s::AbstractString) = print_to_string(s)
69-
String(s::Symbol) = unsafe_string(unsafe_convert(Ptr{UInt8}, s))
69+
@pure String(s::Symbol) = unsafe_string(unsafe_convert(Ptr{UInt8}, s))
7070

7171
unsafe_wrap(::Type{Vector{UInt8}}, s::String) = ccall(:jl_string_to_array, Ref{Vector{UInt8}}, (Any,), s)
7272

@@ -81,8 +81,8 @@ String(s::CodeUnits{UInt8,String}) = s.s
8181
pointer(s::String) = unsafe_convert(Ptr{UInt8}, s)
8282
pointer(s::String, i::Integer) = pointer(s)+(i-1)
8383

84-
ncodeunits(s::String) = Core.sizeof(s)
85-
sizeof(s::String) = Core.sizeof(s)
84+
@pure ncodeunits(s::String) = Core.sizeof(s)
85+
@pure sizeof(s::String) = Core.sizeof(s)
8686
codeunit(s::String) = UInt8
8787

8888
@inline function codeunit(s::String, i::Integer)

test/compiler/inference.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,3 +2421,7 @@ f31974(n::Int) = f31974(1:n)
24212421
# This query hangs if type inference improperly attempts to const prop
24222422
# call cycles.
24232423
@test code_typed(f31974, Tuple{Int}) !== nothing
2424+
2425+
# constant prop of `Symbol("")`
2426+
f_getf_computed_symbol(p) = getfield(p, Symbol("first"))
2427+
@test Base.return_types(f_getf_computed_symbol, Tuple{Pair{Int8,String}}) == [Int8]

0 commit comments

Comments
 (0)