Skip to content

Commit c336152

Browse files
committed
update readme, change where depth prints for Node, update benchmarks/suite.jl
1 parent 11a6a55 commit c336152

File tree

4 files changed

+36
-95
lines changed

4 files changed

+36
-95
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,15 @@ Similar to [Cobweb.jl](https://github.com/JuliaComputing/Cobweb.jl#-creating-nod
121121
using XML: h
122122

123123
julia> node = h.parent(
124-
h.child("content", id="my id")
124+
h.child("first child content", id="id1"),
125+
h.child("second child content", id="id2")
125126
)
126-
# Node Element <parent> (1 child)
127+
# Node Element <parent> (2 children)
127128

128-
julia> XML.write(node)
129+
julia> print(XML.write(node))
129130
# <parent>
130-
# <child id=\"my id\">content</child>
131+
# <child id="id1">first child content</child>
132+
# <child id="id2">second child content</child>
131133
# </parent>
132134
```
133135

benchmarks/suite.jl

Lines changed: 19 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -20,90 +20,32 @@ file = joinpath(@__DIR__, "nasa.xml")
2020

2121
df = DataFrame(kind=String[], name=String[], bench=BenchmarkTools.Trial[])
2222

23+
macro add_benchmark(kind, name, expr...)
24+
esc(:(let
25+
@info string($kind, " - ", $name)
26+
bench = @benchmark $(expr...)
27+
push!(df, (; kind=$kind, name=$name, bench))
28+
end))
29+
end
2330

2431
#-----------------------------------------------------------------------------# Write
25-
kind = "Write"
26-
output = tempname()
27-
28-
name = "XML.write 2"
29-
@info name
30-
node1 = read(file, Node)
31-
bench = @benchmark XML.write($output, $node1)
32-
push!(df, (;kind, name, bench))
33-
34-
35-
name = "EzXML.writexml"
36-
@info name
37-
node2 = EzXML.readxml(file)
38-
bench = @benchmark EzXML.write($output, $node2)
39-
push!(df, (;kind, name, bench))
40-
41-
32+
@add_benchmark "Write" "XML.write" XML.write($(tempname()), o) setup = (o = read(file, Node))
33+
@add_benchmark "Write" "EzXML.writexml" EzXML.write($(tempname()), o) setup = (o = EzXML.readxml(file))
4234

4335
#-----------------------------------------------------------------------------# Read
44-
kind = "Read"
45-
46-
# name = "XML.Raw"
47-
# @info name
48-
# bench = @benchmark read($file, XML.Raw)
49-
# push!(df, (;kind, name, bench))
50-
51-
52-
name = "XML.LazyNode"
53-
@info name
54-
bench = @benchmark read($file, LazyNode)
55-
push!(df, (;kind, name, bench))
56-
57-
name = "XML.Node"
58-
@info name
59-
bench = @benchmark read($file, Node)
60-
push!(df, (;kind, name, bench))
61-
62-
63-
name = "EzXML.readxml"
64-
@info name
65-
bench = @benchmark EzXML.readxml($file)
66-
push!(df, (;kind, name, bench))
67-
68-
69-
name = "XMLDict.xml_dict"
70-
@info name
71-
bench = @benchmark XMLDict.xml_dict(read($file, String))
72-
push!(df, (;kind, name, bench))
73-
36+
@add_benchmark "Read" "XML.LazyNode" read($file, LazyNode)
37+
@add_benchmark "Read" "XML.Node" read($file, Node)
38+
@add_benchmark "Read" "EzXML.readxml" EzXML.readxml($file)
39+
@add_benchmark "Read" "XMLDict.xml_dict" XMLDict.xml_dict(read($file, String))
7440

7541
#-----------------------------------------------------------------------------# Lazy Iteration
76-
kind = "Lazy Iteration"
77-
78-
name = "for x in read(file, LazyNode); end"
79-
@info name
80-
bench = @benchmark (for x in read($file, LazyNode); end)
81-
push!(df, (;kind, name, bench))
82-
83-
84-
name = "for x in open(EzXML.StreamReader, file); end"
85-
@info name
86-
bench = @benchmark (reader = open(EzXML.StreamReader, $file); for x in reader; end; close(reader))
87-
push!(df, (;kind, name, bench))
88-
42+
@add_benchmark "Lazy Iteration" "LazyNode" for x in read($file, LazyNode); end
43+
@add_benchmark "Lazy Iteration" "EzXML.StreamReader" (reader = open(EzXML.StreamReader, $file); for x in reader; end; close(reader))
8944

9045
#-----------------------------------------------------------------------------# Lazy Iteration: Collect Tags
91-
kind = "Collect Tags"
92-
93-
name = "via XML.LazyNode"
94-
@info name
95-
bench = @benchmark [tag(x) for x in o] setup=(o = read(file, LazyNode))
96-
push!(df, (;kind, name, bench))
46+
@add_benchmark "Collect Tags" "LazyNode" [tag(x) for x in o] setup = (o = read(file, LazyNode))
47+
@add_benchmark "Collect Tags" "EzXML.StreamReader" [r.name for x in r if x == EzXML.READER_ELEMENT] setup=(r=open(EzXML.StreamReader, file)) teardown=(close(r))
9748

98-
99-
name = "via EzXML.StreamReader"
100-
@info name
101-
bench = @benchmark [r.name for x in r if x == EzXML.READER_ELEMENT] setup=(r=open(EzXML.StreamReader, file)) teardown=(close(r))
102-
push!(df, (;kind, name, bench))
103-
104-
105-
name = "via EzXML.readxml"
106-
@info name
10749
function get_tags(o::EzXML.Node)
10850
out = String[]
10951
for node in EzXML.eachelement(o)
@@ -114,18 +56,14 @@ function get_tags(o::EzXML.Node)
11456
end
11557
out
11658
end
117-
bench = @benchmark get_tags(o.root) setup=(o = EzXML.readxml(file))
118-
push!(df, (;kind, name, bench))
119-
120-
121-
59+
@add_benchmark "Collect Tags" "EzXML.readxml" get_tags(o.root) setup=(o = EzXML.readxml(file))
12260

12361

12462
#-----------------------------------------------------------------------------# Plots
12563
function plot(df, kind)
12664
g = groupby(df, :kind)
12765
sub = g[(;kind)]
128-
x = map(row -> "$(row.kind): $(row.name)", eachrow(sub))
66+
x = map(row -> "$(row.name)", eachrow(sub))
12967
y = map(x -> median(x).time / 1000^2, sub.bench)
13068
display(barplot(x, y, title = "$kind Time (ms)", border=:none, width=50))
13169
end

src/XML.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -253,17 +253,19 @@ nodetype(o) = o.nodetype
253253
tag(o) = o.tag
254254
attributes(o) = o.attributes
255255
value(o) = o.value
256-
children(o::T) where {T} = isnothing(o.children) ? T[] : o.children
256+
children(o::T) where {T} = isnothing(o.children) ? () : o.children
257257

258-
depth(o) = missing
258+
depth(o) = 1
259259
parent(o) = missing
260260
next(o) = missing
261261
prev(o) = missing
262262

263263
is_simple(o) = nodetype(o) == Element && (isnothing(attributes(o)) || isempty(attributes(o))) &&
264-
length(children(o)) == 1 && nodetype(only(o)) in [Text, CData]
264+
length(children(o)) == 1 && nodetype(only(o)) in (Text, CData)
265265

266-
simplevalue(o) = is_simple(o) ? value(only(o)) : error("`XML.simplevalue(o)` is only defined for simple nodes.")
266+
simple_value(o) = is_simple(o) ? value(only(o)) : error("`XML.simple_value` is only defined for simple nodes.")
267+
268+
Base.@deprecate_binding simplevalue simple_value
267269

268270
#-----------------------------------------------------------------------------# nodes_equal
269271
function nodes_equal(a, b)
@@ -293,8 +295,8 @@ Base.length(o::AbstractXMLNode) = length(children(o))
293295

294296
#-----------------------------------------------------------------------------# printing
295297
function _show_node(io::IO, o)
296-
!ismissing(depth(o)) && print(io, depth(o), ':')
297298
printstyled(io, typeof(o), ' '; color=:light_black)
299+
!ismissing(depth(o)) && printstyled(io, "(depth=", depth(o), ") ", color=:light_black)
298300
printstyled(io, nodetype(o), ; color=:light_green)
299301
if o.nodetype === Text
300302
printstyled(io, ' ', repr(value(o)))
@@ -352,13 +354,12 @@ write(x; kw...) = (io = IOBuffer(); write(io, x; kw...); String(take!(io)))
352354

353355
write(filename::AbstractString, x; kw...) = open(io -> write(io, x; kw...), filename, "w")
354356

355-
function write(io::IO, x; indentsize::Int=2, depth::Union{Missing,Int}=depth(x))
357+
function write(io::IO, x; indentsize::Int=2, depth::Int=depth(x))
356358
indent = ' ' ^ indentsize
357359
nodetype = XML.nodetype(x)
358360
tag = XML.tag(x)
359361
value = XML.value(x)
360362
children = XML.children(x)
361-
depth = ismissing(depth) ? 1 : depth
362363

363364
padding = indent ^ max(0, depth - 1)
364365
print(io, padding)

test/runtests.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ end
3131
@test s == unescape(unescape(escape(s)))
3232

3333
n = Element("tag", Text(s))
34-
@test XML.simplevalue(n) == s
34+
@test XML.simple_value(n) == s
3535

3636
XML.escape!(n)
37-
@test XML.simplevalue(n) == escape(s)
37+
@test XML.simple_value(n) == escape(s)
3838

3939
XML.unescape!(n)
40-
@test XML.simplevalue(n) == s
40+
@test XML.simple_value(n) == s
4141
end
4242

4343
#-----------------------------------------------------------------------------# DTD

0 commit comments

Comments
 (0)