@@ -432,11 +432,12 @@ def plot_file(filename, index=0, timestamps=True, children=True, options=None):
432432 # plot timestamps, if any
433433 if len (ts ) > 0 and timestamps :
434434 func_num = 0
435+ f_labels = function_labels (ts .keys ())
435436 for f , exec_ts in ts .items ():
436437 for execution in exec_ts :
437438 add_brackets (execution [:2 ], execution [2 :], xshift = global_start ,
438439 color = all_colors [func_num % len (all_colors )],
439- label = f . split ( "." )[ - 1 ]
440+ label = f_labels [ f ]
440441 + " %.3fs" % (execution [1 ] - execution [0 ]), options = options )
441442 func_num += 1
442443
@@ -449,6 +450,33 @@ def plot_file(filename, index=0, timestamps=True, children=True, options=None):
449450 return mprofile
450451
451452
453+ def function_labels (dotted_function_names ):
454+ state = {}
455+
456+ def set_state_for (function_names , level ):
457+ for fn in function_names :
458+ label = "." .join (fn .split ("." )[- level :])
459+ label_state = state .setdefault (label , {"functions" : [],
460+ "level" : level })
461+ label_state ["functions" ].append (fn )
462+
463+ set_state_for (dotted_function_names , 1 )
464+
465+ while True :
466+ ambiguous_labels = [label for label in state if len (state [label ]["functions" ]) > 1 ]
467+ for ambiguous_label in ambiguous_labels :
468+ function_names = state [ambiguous_label ]["functions" ]
469+ new_level = state [ambiguous_label ]["level" ] + 1
470+ del state [ambiguous_label ]
471+ set_state_for (function_names , new_level )
472+ if len (ambiguous_labels ) == 0 :
473+ break
474+
475+ fn_to_label = { label_state ["functions" ][0 ] : label for label , label_state in state .items () }
476+
477+ return fn_to_label
478+
479+
452480def plot_action ():
453481 def xlim_type (value ):
454482 try :
0 commit comments