Skip to content

Commit f65fe61

Browse files
authored
add a precompile workload to TOML (JuliaLang#58949)
1 parent 34f9032 commit f65fe61

File tree

3 files changed

+61
-19
lines changed

3 files changed

+61
-19
lines changed

stdlib/TOML/src/TOML.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,6 @@ Internals.reinit!(p::Parser, str::String; filepath::Union{Nothing, String}=nothi
148148
Internals.parse(p::Parser) = Internals.parse(p._p)
149149
Internals.tryparse(p::Parser) = Internals.tryparse(p._p)
150150

151+
include("precompile.jl")
152+
151153
end

stdlib/TOML/src/precompile.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
if Base.generating_output()
2+
let
3+
# Test TOML content
4+
test_toml = """
5+
title = "Example"
6+
with_quotes = \"quoted\"
7+
number = 42
8+
float = 3.14
9+
boolean = true
10+
date = 2023-01-01
11+
datetime = 2023-01-01T12:00:00Z
12+
time = 12:00:00
13+
array = [1, 2, 3]
14+
inline = {a=1, b=1.0, c=[1,2,3], d="foo"}
15+
16+
[nested]
17+
key = "value"
18+
"""
19+
20+
test_dict = TOML.parse(test_toml)
21+
22+
TOML.parse(test_toml)
23+
24+
io_stream = IOBuffer(test_toml)
25+
TOML.parse(io_stream)
26+
27+
mktemp() do path, io
28+
write(io, test_toml)
29+
close(io)
30+
TOML.parsefile(path)
31+
end
32+
33+
mktemp() do path, io
34+
TOML.print(io, test_dict)
35+
close(io)
36+
end
37+
end
38+
end

stdlib/TOML/src/print.jl

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ function print_toml_escaped(io::IO, s::AbstractString)
3333
end
3434
end
3535

36-
const MbyFunc = Union{Function, Nothing}
3736
const TOMLValue = Union{AbstractVector, AbstractDict, Bool, Integer, AbstractFloat, AbstractString,
3837
Dates.DateTime, Dates.Time, Dates.Date, Base.TOML.DateTime, Base.TOML.Time, Base.TOML.Date}
3938

@@ -59,8 +58,8 @@ function printkey(io::IO, keys::Vector{String})
5958
end
6059
end
6160

62-
function to_toml_value(f::MbyFunc, value)
63-
if f === nothing
61+
function to_toml_value(@nospecialize(f::Function), value)
62+
if f === identity
6463
error("type `$(typeof(value))` is not a valid TOML type, pass a conversion function to `TOML.print`")
6564
end
6665
toml_value = f(value)
@@ -75,12 +74,12 @@ end
7574
##########
7675

7776
# Fallback
78-
function printvalue(f::MbyFunc, io::IO, value, sorted::Bool)
77+
function printvalue(f::Function, io::IO, value, sorted::Bool)
7978
toml_value = to_toml_value(f, value)
8079
@invokelatest printvalue(f, io, toml_value, sorted)
8180
end
8281

83-
function printvalue(f::MbyFunc, io::IO, value::AbstractVector, sorted::Bool)
82+
function printvalue(f::Function, io::IO, value::AbstractVector, sorted::Bool)
8483
Base.print(io, "[")
8584
for (i, x) in enumerate(value)
8685
i != 1 && Base.print(io, ", ")
@@ -89,7 +88,7 @@ function printvalue(f::MbyFunc, io::IO, value::AbstractVector, sorted::Bool)
8988
Base.print(io, "]")
9089
end
9190

92-
function printvalue(f::MbyFunc, io::IO, value::TOMLValue, sorted::Bool)
91+
function printvalue(f::Function, io::IO, value::TOMLValue, sorted::Bool)
9392
value isa Base.TOML.DateTime && (value = Dates.DateTime(value))
9493
value isa Base.TOML.Time && (value = Dates.Time(value))
9594
value isa Base.TOML.Date && (value = Dates.Date(value))
@@ -117,7 +116,7 @@ function print_integer(io::IO, value::Integer)
117116
return
118117
end
119118

120-
function print_inline_table(f::MbyFunc, io::IO, value::AbstractDict, sorted::Bool)
119+
function print_inline_table(f::Function, io::IO, value::AbstractDict, sorted::Bool)
121120
vkeys = collect(keys(value))
122121
if sorted
123122
sort!(vkeys)
@@ -138,15 +137,14 @@ end
138137
# Tables #
139138
##########
140139

141-
is_table(value) = isa(value, AbstractDict)
142-
is_array_of_tables(value) = isa(value, AbstractArray) &&
143-
length(value) > 0 && (
144-
isa(value, AbstractArray{<:AbstractDict}) ||
145-
all(v -> isa(v, AbstractDict), value)
146-
)
147-
is_tabular(value) = is_table(value) || @invokelatest(is_array_of_tables(value))
140+
is_table(@nospecialize(value)) = isa(value, AbstractDict)
141+
is_array_of_tables(@nospecialize(value)) =
142+
isa(value, AbstractArray) &&
143+
length(value) > 0 && (isa(value, AbstractArray{<:AbstractDict}) ||
144+
all(v -> isa(v, AbstractDict), value))
145+
is_tabular(@nospecialize(value)) = is_table(value) || @invokelatest(is_array_of_tables(value))
148146

149-
function print_table(f::MbyFunc, io::IO, a::AbstractDict,
147+
function print_table(f::Function, io::IO, a::AbstractDict,
150148
ks::Vector{String} = String[];
151149
indent::Int = 0,
152150
first_block::Bool = true,
@@ -228,7 +226,11 @@ end
228226
# API #
229227
#######
230228

231-
print(f::MbyFunc, io::IO, a::AbstractDict; sorted::Bool=false, by=identity, inline_tables::IdSet{<:AbstractDict}=IdSet{Dict{String}}()) = print_table(f, io, a; sorted, by, inline_tables)
232-
print(f::MbyFunc, a::AbstractDict; sorted::Bool=false, by=identity, inline_tables::IdSet{<:AbstractDict}=IdSet{Dict{String}}()) = print(f, stdout, a; sorted, by, inline_tables)
233-
print(io::IO, a::AbstractDict; sorted::Bool=false, by=identity, inline_tables::IdSet{<:AbstractDict}=IdSet{Dict{String}}()) = print_table(nothing, io, a; sorted, by, inline_tables)
234-
print( a::AbstractDict; sorted::Bool=false, by=identity, inline_tables::IdSet{<:AbstractDict}=IdSet{Dict{String}}()) = print(nothing, stdout, a; sorted, by, inline_tables)
229+
print(f::Function, io::IO, a::AbstractDict; sorted::Bool=false, by=identity, inline_tables::IdSet{<:AbstractDict}=IdSet{Dict{String}}()) =
230+
print_table(f, io, a; sorted, by, inline_tables)
231+
print(f::Function, a::AbstractDict; sorted::Bool=false, by=identity, inline_tables::IdSet{<:AbstractDict}=IdSet{Dict{String}}()) =
232+
print(f, stdout, a; sorted, by, inline_tables)
233+
print(io::IO, a::AbstractDict; sorted::Bool=false, by=identity, inline_tables::IdSet{<:AbstractDict}=IdSet{Dict{String}}()) =
234+
print_table(identity, io, a; sorted, by, inline_tables)
235+
print(a::AbstractDict; sorted::Bool=false, by=identity, inline_tables::IdSet{<:AbstractDict}=IdSet{Dict{String}}()) =
236+
print(identity, stdout, a; sorted, by, inline_tables)

0 commit comments

Comments
 (0)