From 4abcdba7a8e367523cc0d6cd2968e6f28d0e1a34 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 28 Jan 2022 17:48:11 +0000 Subject: [PATCH 1/2] Added --jumps option --- scripts/count_opcodes.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/scripts/count_opcodes.py b/scripts/count_opcodes.py index 44ec60a..173d58d 100644 --- a/scripts/count_opcodes.py +++ b/scripts/count_opcodes.py @@ -36,6 +36,10 @@ NSTORE_NONE_FAST = "__nstore_none_fast__" # Number of STORE_FAST preceded by LOAD_CONST(None) CO_CONSTS_SIZE = "__co_consts_size__" CO_CONSTS_NUM = "__co_consts_num__" +NUM_JUMP_ABS = "__num_jump_abs__" +NUM_JUMP_REL = "__num_jump_rel__" +NUM_JUMP_ABS_EXT = "__num_jump_abs_extended__" +NUM_JUMP_REL_EXT = "__num_jump_rel_extended__" SHOW_ITEMS = [ (NERRORS, "errors"), @@ -52,6 +56,10 @@ (PREV_EXTENDED, "prev extended args"), (CO_CONSTS_SIZE, "total size of co_consts"), (CO_CONSTS_NUM, "number of co_consts"), + (NUM_JUMP_ABS, "number of absolute jumps"), + (NUM_JUMP_REL, "number of relative jumps"), + (NUM_JUMP_ABS_EXT, "number of absolute jumps with extended args"), + (NUM_JUMP_REL_EXT, "number of relative jumps with extended args"), ] # TODO: Make this list an option @@ -253,6 +261,24 @@ def reporting_guts(self, counter, co, bias): key = f"!{name}" counter[key] += 1 +class JumpsReporter(Reporter): + + def reporting_guts(self, counter, co, bias): + co_code = co.co_code + for i in range(0, len(co_code), 2): + counter[NOPCODES] += 1 + op = co_code[i] + extended = i>0 and self.prev_is_extended_arg + if op in opcode.hasjabs: + counter[NUM_JUMP_ABS] += 1 + if extended: + counter[NUM_JUMP_ABS_EXT] += 1 + if op in opcode.hasjrel: + counter[NUM_JUMP_REL] += 1 + if extended: + counter[NUM_JUMP_REL_EXT] += 1 + self.prev_is_extended_arg = opcode.opname[op] == "EXTENDED_ARG" + def expand_globs(filenames): for filename in filenames: @@ -276,6 +302,8 @@ def expand_globs(filenames): help="Show N most common constants") argparser.add_argument("--names", type=int, metavar="N", help="Show N most common names") +argparser.add_argument("--jumps", action="store_true", + help="counts jumps with and without extended args") argparser.add_argument("--bias", type=int, help="Add bias for opcodes inside for-loops") argparser.add_argument("--cache-needs", action="store_true", @@ -312,6 +340,8 @@ def main(): reporter = ConstantsReporter() elif args.names: reporter = NamesReporter() + elif args.jumps: + reporter = JumpsReporter() else: reporter = Reporter() hits = 0 From 1f197c278c41e998c4422c9e487e94064a0f12bd Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Fri, 28 Jan 2022 13:04:23 -0800 Subject: [PATCH 2/2] PEP 8 tweak --- scripts/count_opcodes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/count_opcodes.py b/scripts/count_opcodes.py index 173d58d..018152d 100644 --- a/scripts/count_opcodes.py +++ b/scripts/count_opcodes.py @@ -268,7 +268,7 @@ def reporting_guts(self, counter, co, bias): for i in range(0, len(co_code), 2): counter[NOPCODES] += 1 op = co_code[i] - extended = i>0 and self.prev_is_extended_arg + extended = i > 0 and self.prev_is_extended_arg if op in opcode.hasjabs: counter[NUM_JUMP_ABS] += 1 if extended: