34
34
35
35
TOTAL = "specialization.deferred" , "specialization.hit" , "specialization.miss" , "execution_count"
36
36
37
+ def format_ratio (num , den ):
38
+ """
39
+ Format a ratio as a percentage. When the denominator is 0, returns the empty
40
+ string.
41
+ """
42
+ if den == 0 :
43
+ return ""
44
+ else :
45
+ return f"{ num / den :.01%} "
46
+
37
47
def join_rows (a_rows , b_rows ):
38
48
"""
39
49
Joins two tables together, side-by-side, where the first column in each is a
@@ -87,7 +97,7 @@ def calculate_specialization_stats(family_stats, total):
87
97
continue
88
98
else :
89
99
label = key
90
- rows .append ((f"{ label :>12} " , f"{ family_stats [key ]:>12} " , f" { 100 * family_stats [key ]/ total :0.1f } %" ))
100
+ rows .append ((f"{ label :>12} " , f"{ family_stats [key ]:>12} " , format_ratio ( family_stats [key ], total ) ))
91
101
return rows
92
102
93
103
def calculate_specialization_success_failure (family_stats ):
@@ -100,7 +110,7 @@ def calculate_specialization_success_failure(family_stats):
100
110
label = key [len ("specialization." ):]
101
111
label = label [0 ].upper () + label [1 :]
102
112
val = family_stats .get (key , 0 )
103
- rows .append ((label , val , f" { 100 * val / total_attempts :0.1f } %" ))
113
+ rows .append ((label , val , format_ratio ( val , total_attempts ) ))
104
114
return rows
105
115
106
116
def calculate_specialization_failure_kinds (name , family_stats , defines ):
@@ -118,7 +128,7 @@ def calculate_specialization_failure_kinds(name, family_stats, defines):
118
128
for value , index in failures :
119
129
if not value :
120
130
continue
121
- rows .append ((kind_to_text (index , defines , name ), value , f" { 100 * value / total_failures :0.1f } %" ))
131
+ rows .append ((kind_to_text (index , defines , name ), value , format_ratio ( value , total_failures ) ))
122
132
return rows
123
133
124
134
def print_specialization_stats (name , family_stats , defines ):
@@ -318,11 +328,11 @@ def calculate_execution_counts(opcode_stats, total):
318
328
for (count , name , miss ) in counts :
319
329
cumulative += count
320
330
if miss :
321
- miss = f" { 100 * miss / count :0.1f } %"
331
+ miss = format_ratio ( miss , count )
322
332
else :
323
333
miss = ""
324
- rows .append ((name , count , f" { 100 * count / total :0.1f } %" ,
325
- f" { 100 * cumulative / total :0.1f } %" , miss ))
334
+ rows .append ((name , count , format_ratio ( count , total ) ,
335
+ format_ratio ( cumulative , total ) , miss ))
326
336
return rows
327
337
328
338
def emit_execution_counts (opcode_stats , total ):
@@ -386,9 +396,9 @@ def emit_comparative_specialization_stats(base_opcode_stats, head_opcode_stats):
386
396
def calculate_specialization_effectiveness (opcode_stats , total ):
387
397
basic , not_specialized , specialized = categorized_counts (opcode_stats )
388
398
return [
389
- ("Basic" , basic , f" { basic * 100 / total :0.1f } %" ),
390
- ("Not specialized" , not_specialized , f" { not_specialized * 100 / total :0.1f } %" ),
391
- ("Specialized" , specialized , f" { specialized * 100 / total :0.1f } %" ),
399
+ ("Basic" , basic , format_ratio ( basic , total ) ),
400
+ ("Not specialized" , not_specialized , format_ratio ( not_specialized , total ) ),
401
+ ("Specialized" , specialized , format_ratio ( specialized , total ) ),
392
402
]
393
403
394
404
def emit_specialization_overview (opcode_stats , total ):
@@ -405,7 +415,7 @@ def emit_specialization_overview(opcode_stats, total):
405
415
counts .sort (reverse = True )
406
416
if total :
407
417
with Section (f"{ title } by instruction" , 3 ):
408
- rows = [ (name , count , f" { 100 * count / total :0.1f } %" ) for (count , name ) in counts [:10 ] ]
418
+ rows = [ (name , count , format_ratio ( count , total ) ) for (count , name ) in counts [:10 ] ]
409
419
emit_table (("Name" , "Count:" , "Ratio:" ), rows )
410
420
411
421
def emit_comparative_specialization_overview (base_opcode_stats , base_total , head_opcode_stats , head_total ):
@@ -432,15 +442,15 @@ def calculate_call_stats(stats):
432
442
rows = []
433
443
for key , value in stats .items ():
434
444
if "Calls to" in key :
435
- rows .append ((key , value , f" { 100 * value / total :0.1f } %" ))
445
+ rows .append ((key , value , format_ratio ( value , total ) ))
436
446
elif key .startswith ("Calls " ):
437
447
name , index = key [:- 1 ].split ("[" )
438
448
index = int (index )
439
449
label = name + " (" + pretty (defines [index ][0 ]) + ")"
440
- rows .append ((label , value , f" { 100 * value / total :0.1f } %" ))
450
+ rows .append ((label , value , format_ratio ( value , total ) ))
441
451
for key , value in stats .items ():
442
452
if key .startswith ("Frame" ):
443
- rows .append ((key , value , f" { 100 * value / total :0.1f } %" ))
453
+ rows .append ((key , value , format_ratio ( value , total ) ))
444
454
return rows
445
455
446
456
def emit_call_stats (stats ):
@@ -468,13 +478,13 @@ def calculate_object_stats(stats):
468
478
for key , value in stats .items ():
469
479
if key .startswith ("Object" ):
470
480
if "materialize" in key :
471
- ratio = f" { 100 * value / total_materializations :0.1f } %"
481
+ ratio = format_ratio ( value , total_materializations )
472
482
elif "allocations" in key :
473
- ratio = f" { 100 * value / total_allocations :0.1f } %"
483
+ ratio = format_ratio ( value , total_allocations )
474
484
elif "increfs" in key :
475
- ratio = f" { 100 * value / total_increfs :0.1f } %"
485
+ ratio = format_ratio ( value , total_increfs )
476
486
elif "decrefs" in key :
477
- ratio = f" { 100 * value / total_decrefs :0.1f } %"
487
+ ratio = format_ratio ( value , total_decrefs )
478
488
else :
479
489
ratio = ""
480
490
label = key [6 :].strip ()
@@ -517,8 +527,8 @@ def emit_pair_counts(opcode_stats, total):
517
527
for (count , pair ) in itertools .islice (pair_counts , 100 ):
518
528
i , j = pair
519
529
cumulative += count
520
- rows .append ((opname [i ] + " " + opname [j ], count , f" { 100 * count / total :0.1f } %" ,
521
- f" { 100 * cumulative / total :0.1f } %" ))
530
+ rows .append ((opname [i ] + " " + opname [j ], count , format_ratio ( count , total ) ,
531
+ format_ratio ( cumulative , total ) ))
522
532
emit_table (("Pair" , "Count:" , "Self:" , "Cumulative:" ),
523
533
rows
524
534
)
0 commit comments