@@ -6,6 +6,8 @@ using Tables
66const SUITE = BenchmarkGroup ()
77const HAS_EXPLICIT_ATTRIBUTE_API = isdefined (MultiScaleTreeGraph, :attribute ) && isdefined (MultiScaleTreeGraph, :attribute! )
88const 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})
911const DEFAULT_ATTR_KEY_IS_SYMBOL = true # always true
1012
1113const SIZE_TIERS = (
3739 return sym_is_symbol ? syms : Tuple (String (s) for s in syms)
3840end
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+
4051function synthetic_mtg (; n_nodes:: Int = 10_000 , seed:: Int = 42 )
4152 rng = MersenneTwister (seed)
4253 mass_key = _default_attr_key (:mass )
204215
205216function 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
225236
226237function 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
254265
255266function 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