Skip to content

Commit eefcea4

Browse files
committed
Update benchmarks.jl
handle previous signature for `attributes`
1 parent ff9176e commit eefcea4

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

benchmark/benchmarks.jl

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ using Tables
66
const SUITE = BenchmarkGroup()
77
const HAS_EXPLICIT_ATTRIBUTE_API = isdefined(MultiScaleTreeGraph, :attribute) && isdefined(MultiScaleTreeGraph, :attribute!)
88
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})
911
const DEFAULT_ATTR_KEY_IS_SYMBOL = true # always true
1012

1113
const SIZE_TIERS = (
@@ -37,6 +39,15 @@ end
3739
return sym_is_symbol ? syms : Tuple(String(s) for s in syms)
3840
end
3941

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+
4051
function synthetic_mtg(; n_nodes::Int=10_000, seed::Int=42)
4152
rng = MersenneTwister(seed)
4253
mass_key = _default_attr_key(:mass)
@@ -204,7 +215,7 @@ end
204215

205216
function traverse_update_one_explicit_api!(root, key_mass)
206217
traverse!(root) do node
207-
m = HAS_EXPLICIT_ATTRIBUTE_API ? attribute(node, key_mass, 0.0) : node[key_mass]
218+
m = _attribute_default(node, key_mass, 0.0)
208219
m === nothing && (m = 0.0)
209220
HAS_EXPLICIT_ATTRIBUTE_API ? attribute!(node, key_mass, m + 0.1) : (node[key_mass] = m + 0.1)
210221
end
@@ -225,8 +236,8 @@ end
225236

226237
function traverse_update_multi_leaf_explicit_api!(root, key_width, key_area, symbol_leaf)
227238
traverse!(root, symbol=symbol_leaf) do node
228-
width = HAS_EXPLICIT_ATTRIBUTE_API ? attribute(node, key_width, 0.0) : node[key_width]
229-
area = HAS_EXPLICIT_ATTRIBUTE_API ? attribute(node, key_area, 0.0) : node[key_area]
239+
width = _attribute_default(node, key_width, 0.0)
240+
area = _attribute_default(node, key_area, 0.0)
230241
width === nothing && (width = 0.0)
231242
area === nothing && (area = 0.0)
232243
if HAS_EXPLICIT_ATTRIBUTE_API
@@ -254,9 +265,9 @@ end
254265

255266
function traverse_update_multi_mixed_explicit_api!(root, key_mass, key_counter, symbol_leaf_internode)
256267
traverse!(root, symbol=symbol_leaf_internode) do node
257-
m = HAS_EXPLICIT_ATTRIBUTE_API ? attribute(node, key_mass, 0.0) : node[key_mass]
268+
m = _attribute_default(node, key_mass, 0.0)
258269
m === nothing && (m = 0.0)
259-
counter = HAS_EXPLICIT_ATTRIBUTE_API ? attribute(node, key_counter, 0) : node[key_counter]
270+
counter = _attribute_default(node, key_counter, 0)
260271
isnothing(counter) && (counter = 0)
261272
if HAS_EXPLICIT_ATTRIBUTE_API
262273
attribute!(node, key_mass, m * 0.999 + 0.0001)

0 commit comments

Comments
 (0)