Skip to content

Commit ed0a4dc

Browse files
committed
Update benchmarks.jl
1 parent 50211fc commit ed0a4dc

1 file changed

Lines changed: 40 additions & 99 deletions

File tree

benchmark/benchmarks.jl

Lines changed: 40 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -4,67 +4,22 @@ using Random
44
using Tables
55

66
const SUITE = BenchmarkGroup()
7-
const HAS_EXPLICIT_ATTRIBUTE_API = isdefined(MultiScaleTreeGraph, :attribute) && isdefined(MultiScaleTreeGraph, :attribute!)
8-
const HAS_TABLE_VIEWS_API = isdefined(MultiScaleTreeGraph, :symbol_table) && isdefined(MultiScaleTreeGraph, :mtg_table)
9-
const HAS_ATTRIBUTE_POSITIONAL_DEFAULT = HAS_EXPLICIT_ATTRIBUTE_API &&
10-
hasmethod(attribute, Tuple{MultiScaleTreeGraph.Node,Symbol,Any})
11-
const DEFAULT_ATTR_KEY_IS_SYMBOL = true # always true
127

138
const SIZE_TIERS = (
149
small=10_000,
1510
medium=100_000,
1611
large=300_000,
1712
)
1813

19-
@inline _default_attr_key(key::Symbol) = DEFAULT_ATTR_KEY_IS_SYMBOL ? key : String(key)
20-
21-
@inline function _is_symbol_attr_store(root)
22-
try
23-
return root[:mass] !== nothing
24-
catch
25-
end
26-
try
27-
root["mass"]
28-
return false
29-
catch
30-
end
31-
return DEFAULT_ATTR_KEY_IS_SYMBOL
32-
end
33-
34-
@inline _attr_key(symbol_attrs::Bool, key::Symbol) = symbol_attrs ? key : String(key)
35-
36-
@inline _symbol_filter(sym_is_symbol::Bool, sym::Symbol) = sym_is_symbol ? sym : String(sym)
37-
38-
@inline function _symbol_filter(sym_is_symbol::Bool, syms::Tuple{Vararg{Symbol}})
39-
return sym_is_symbol ? syms : Tuple(String(s) for s in syms)
40-
end
41-
42-
@inline function _attribute_default(node, key, default)
43-
HAS_EXPLICIT_ATTRIBUTE_API || return node[key]
44-
if HAS_ATTRIBUTE_POSITIONAL_DEFAULT
45-
return attribute(node, key, default)
46-
else
47-
return attribute(node, key; default=default)
48-
end
49-
end
50-
5114
function synthetic_mtg(; n_nodes::Int=10_000, seed::Int=42)
5215
rng = MersenneTwister(seed)
53-
mass_key = _default_attr_key(:mass)
54-
height_key = _default_attr_key(:height)
55-
temp_key = _default_attr_key(:temperature)
56-
length_key = _default_attr_key(:Length)
57-
diameter_key = _default_attr_key(:Diameter)
58-
width_key = _default_attr_key(:Width)
59-
area_key = _default_attr_key(:Area)
60-
6116
root = Node(
6217
1,
6318
MutableNodeMTG(:/, :Plant, 1, 1),
64-
Dict{Any,Any}(
65-
mass_key => rand(rng),
66-
height_key => rand(rng),
67-
temp_key => 20.0,
19+
Dict{Symbol,Any}(
20+
:mass => rand(rng),
21+
:height => rand(rng),
22+
:temperature => 20.0,
6823
),
6924
)
7025

@@ -83,27 +38,27 @@ function synthetic_mtg(; n_nodes::Int=10_000, seed::Int=42)
8338
sym = :Internode
8439
scale_ = 2
8540
link_ = :<
86-
attrs = Dict{Any,Any}(
87-
mass_key => rand(rng),
88-
length_key => rand(rng),
89-
diameter_key => rand(rng),
41+
attrs = Dict{Symbol,Any}(
42+
:mass => rand(rng),
43+
:Length => rand(rng),
44+
:Diameter => rand(rng),
9045
)
9146
elseif roll < 0.95
9247
sym = :Leaf
9348
scale_ = 3
9449
link_ = :+
95-
attrs = Dict{Any,Any}(
96-
mass_key => rand(rng),
97-
width_key => rand(rng),
98-
area_key => rand(rng),
50+
attrs = Dict{Symbol,Any}(
51+
:mass => rand(rng),
52+
:Width => rand(rng),
53+
:Area => rand(rng),
9954
)
10055
else
10156
sym = :Axis
10257
scale_ = 2
10358
link_ = :<
104-
attrs = Dict{Any,Any}(
105-
mass_key => rand(rng),
106-
length_key => rand(rng),
59+
attrs = Dict{Symbol,Any}(
60+
:mass => rand(rng),
61+
:Length => rand(rng),
10762
)
10863
end
10964

@@ -215,9 +170,9 @@ end
215170

216171
function traverse_update_one_explicit_api!(root, key_mass)
217172
traverse!(root) do node
218-
m = _attribute_default(node, key_mass, 0.0)
173+
m = attribute(node, key_mass, 0.0)
219174
m === nothing && (m = 0.0)
220-
HAS_EXPLICIT_ATTRIBUTE_API ? attribute!(node, key_mass, m + 0.1) : (node[key_mass] = m + 0.1)
175+
attribute!(node, key_mass, m + 0.1)
221176
end
222177
return nothing
223178
end
@@ -236,17 +191,12 @@ end
236191

237192
function traverse_update_multi_leaf_explicit_api!(root, key_width, key_area, symbol_leaf)
238193
traverse!(root, symbol=symbol_leaf) do node
239-
width = _attribute_default(node, key_width, 0.0)
240-
area = _attribute_default(node, key_area, 0.0)
194+
width = attribute(node, key_width, 0.0)
195+
area = attribute(node, key_area, 0.0)
241196
width === nothing && (width = 0.0)
242197
area === nothing && (area = 0.0)
243-
if HAS_EXPLICIT_ATTRIBUTE_API
244-
attribute!(node, key_width, width * 1.001)
245-
attribute!(node, key_area, area + width)
246-
else
247-
node[key_width] = width * 1.001
248-
node[key_area] = area + width
249-
end
198+
attribute!(node, key_width, width * 1.001)
199+
attribute!(node, key_area, area + width)
250200
end
251201
return nothing
252202
end
@@ -265,24 +215,19 @@ end
265215

266216
function traverse_update_multi_mixed_explicit_api!(root, key_mass, key_counter, symbol_leaf_internode)
267217
traverse!(root, symbol=symbol_leaf_internode) do node
268-
m = _attribute_default(node, key_mass, 0.0)
218+
m = attribute(node, key_mass, 0.0)
269219
m === nothing && (m = 0.0)
270-
counter = _attribute_default(node, key_counter, 0)
220+
counter = attribute(node, key_counter, 0)
271221
isnothing(counter) && (counter = 0)
272-
if HAS_EXPLICIT_ATTRIBUTE_API
273-
attribute!(node, key_mass, m * 0.999 + 0.0001)
274-
attribute!(node, key_counter, counter + 1)
275-
else
276-
node[key_mass] = m * 0.999 + 0.0001
277-
node[key_counter] = counter + 1
278-
end
222+
attribute!(node, key_mass, m * 0.999 + 0.0001)
223+
attribute!(node, key_counter, counter + 1)
279224
end
280225
return nothing
281226
end
282227

283228
function descendants_vec_workload(root, keys, symbol_internode)
284229
vals = descendants(root, keys, symbol=symbol_internode, ignore_nothing=true)
285-
_assert_descendants_matrix(vals, 2)
230+
_assert_descendants_matrix(vals, length(keys))
286231
s = 0.0
287232
for v in vals
288233
s += v[1] + v[2]
@@ -296,7 +241,7 @@ end
296241

297242
function descendants_mixed_many(root, keys, symbol_leaf_internode; ignore_nothing::Bool)
298243
vals = descendants(root, keys, symbol=symbol_leaf_internode, ignore_nothing=ignore_nothing)
299-
_assert_descendants_matrix(vals, 2)
244+
_assert_descendants_matrix(vals, length(keys))
300245
return vals
301246
end
302247

@@ -306,18 +251,16 @@ function build_tier!(suite, tier_name::String, n_nodes::Int)
306251
root = data.root
307252
sample_nodes = data.sample_nodes
308253
sample_leaves = data.sample_leaves
309-
sym_is_symbol = symbol(root) isa Symbol
310-
symbol_attrs = _is_symbol_attr_store(root)
311254

312-
key_mass = _attr_key(symbol_attrs, :mass)
313-
key_length = _attr_key(symbol_attrs, :Length)
314-
key_width = _attr_key(symbol_attrs, :Width)
315-
key_area = _attr_key(symbol_attrs, :Area)
316-
key_counter = _attr_key(symbol_attrs, :update_counter)
255+
key_mass = :mass
256+
key_length = :Length
257+
key_width = :Width
258+
key_area = :Area
259+
key_counter = :update_counter
317260

318-
symbol_leaf = _symbol_filter(sym_is_symbol, :Leaf)
319-
symbol_internode = _symbol_filter(sym_is_symbol, :Internode)
320-
symbol_leaf_internode = _symbol_filter(sym_is_symbol, (:Leaf, :Internode))
261+
symbol_leaf = :Leaf
262+
symbol_internode = :Internode
263+
symbol_leaf_internode = (:Leaf, :Internode)
321264

322265
tier = BenchmarkGroup()
323266
suite[tier_name] = tier
@@ -351,20 +294,18 @@ function build_tier!(suite, tier_name::String, n_nodes::Int)
351294
tier["many_queries"]["descendants_repeated_inplace"] = @benchmarkable descendants_repeated_workload_inplace($root, 30, $key_length, $symbol_internode)
352295

353296
if tier_name == "small"
354-
tier["api_surface_small_only"]["insert_child"] = @benchmarkable insert_child!(mtg_[1], MutableNodeMTG(:<, :Internode, 1, 2), x -> Dict{Any,Any}(mass_key_ => 0.1), max_id_) setup = (data_ = synthetic_mtg(n_nodes=8_000, seed=111); mtg_ = data_.root; max_id_ = [max_id(mtg_)]; mass_key_ = _attr_key(_is_symbol_attr_store(mtg_), :mass))
297+
tier["api_surface_small_only"]["insert_child"] = @benchmarkable insert_child!(mtg_[1], MutableNodeMTG(:<, :Internode, 1, 2), _ -> Dict{Symbol,Any}(:mass => 0.1), max_id_) setup = (data_ = synthetic_mtg(n_nodes=8_000, seed=111); mtg_ = data_.root; max_id_ = [max_id(mtg_)])
355298

356299
tier["api_surface_small_only"]["delete_node"] = @benchmarkable delete_node!(target_) setup = (data_ = synthetic_mtg(n_nodes=8_000, seed=222); mtg_ = data_.root; target_ = get_node(mtg_, node_id(mtg_[1])))
357300

358301
tier["api_surface_small_only"]["prune_subtree"] = @benchmarkable prune!(target_) setup = (data_ = synthetic_mtg(n_nodes=8_000, seed=333); mtg_ = data_.root; target_ = mtg_[1])
359302

360-
tier["api_surface_small_only"]["transform"] = @benchmarkable transform!(mtg_, in_ => (x -> x + 1.0) => out_, ignore_nothing=true) setup = (data_ = synthetic_mtg(n_nodes=8_000, seed=444); mtg_ = data_.root; symattrs_ = _is_symbol_attr_store(mtg_); in_ = _attr_key(symattrs_, :mass); out_ = _attr_key(symattrs_, :mass2))
303+
tier["api_surface_small_only"]["transform"] = @benchmarkable transform!(mtg_, in_ => (x -> x + 1.0) => out_, ignore_nothing=true) setup = (data_ = synthetic_mtg(n_nodes=8_000, seed=444); mtg_ = data_.root; in_ = :mass; out_ = :mass2)
361304

362-
tier["api_surface_small_only"]["select"] = @benchmarkable select!(mtg_, key1_, key2_, ignore_nothing=true) setup = (data_ = synthetic_mtg(n_nodes=8_000, seed=555); mtg_ = data_.root; symattrs_ = _is_symbol_attr_store(mtg_); key1_ = _attr_key(symattrs_, :mass); key2_ = _attr_key(symattrs_, :Length))
305+
tier["api_surface_small_only"]["select"] = @benchmarkable select!(mtg_, key1_, key2_, ignore_nothing=true) setup = (data_ = synthetic_mtg(n_nodes=8_000, seed=555); mtg_ = data_.root; key1_ = :mass; key2_ = :Length)
363306

364-
if HAS_TABLE_VIEWS_API
365-
tier["api_surface_small_only"]["tables_symbol"] = @benchmarkable symbol_table($root, :Leaf)
366-
tier["api_surface_small_only"]["tables_unified"] = @benchmarkable mtg_table($root)
367-
end
307+
tier["api_surface_small_only"]["tables_symbol"] = @benchmarkable to_table($root, symbol=:Leaf)
308+
tier["api_surface_small_only"]["tables_unified"] = @benchmarkable to_table($root)
368309

369310
tier["api_surface_small_only"]["write_mtg"] = @benchmarkable write_mtg(f_, mtg_) setup = (data_ = synthetic_mtg(n_nodes=3_000, seed=666); mtg_ = data_.root; f_ = tempname() * ".mtg") teardown = (isfile(f_) && rm(f_, force=true))
370311
end

0 commit comments

Comments
 (0)