Skip to content

Commit 73f16fd

Browse files
committed
Add an option to count future cache needs
See python/cpython#26264 (comment)
1 parent 9ee145b commit 73f16fd

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

scripts/count_opcodes.py

+28-4
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,18 @@
3737
]
3838

3939
# TODO: Make this list an option
40-
OF_INTEREST_NAMES = [x for x in opcode.opname if not x.startswith("<")]
41-
OF_INTEREST_NAMES = ["BINARY_ADD", "BINARY_SUBTRACT",
42-
"INPLACE_ADD", "INPLACE_SUBTRACT"]
40+
CACHE_ENTRIES = {
41+
"LOAD_GLOBAL": 2,
42+
"LOAD_ATTR": 2,
43+
"CALL_FUNCTION": 2,
44+
"CALL_FUNCTION_KW": 2,
45+
"CALL_FUNCTION_EX": 2, # Unsure
46+
"CALL_METHOD": 2,
47+
"CALL_METHOD_KW": 2,
48+
}
49+
OF_INTEREST_NAMES = CACHE_ENTRIES.keys()
4350

44-
of_interest = set(opcode.opmap[x] for x in OF_INTEREST_NAMES)
51+
of_interest = set(opcode.opmap[x] for x in OF_INTEREST_NAMES if x in opcode.opmap)
4552

4653

4754
def all_code_objects(code):
@@ -165,6 +172,8 @@ def expand_globs(filenames):
165172
help="show N most common opcode pairs")
166173
argparser.add_argument("--bias", type=int,
167174
help="Add bias for opcodes inside for-loops")
175+
argparser.add_argument("--cache-needs", action="store_true",
176+
help="Show fraction of cache entries needed per opcode ")
168177
argparser.add_argument("filenames", nargs="*", metavar="FILE",
169178
help="files, directories or tarballs to count")
170179

@@ -223,6 +232,21 @@ def main():
223232
npairs = counter[NPAIRS]
224233
print(f"Total: {showstats(counter)}")
225234

235+
if args.cache_needs:
236+
print()
237+
print("Future cache needs")
238+
ncache = 0
239+
for key in counter:
240+
if key in of_interest:
241+
name = opcode.opname[key]
242+
need = CACHE_ENTRIES[name] * counter[key]
243+
ncache += need
244+
print(name, need)
245+
nops = counter[NOPCODES]
246+
print(f"{nops} opcodes, {2*nops} bytes,",
247+
f"{ncache} cache entries, {8*ncache} bytes,",
248+
f"{ncache/nops:.2f} ncache/nops")
249+
226250
if args.singles:
227251
singles = []
228252
for key in counter:

0 commit comments

Comments
 (0)