github.com/ii64/gouring@v0.4.1/inline_cost.py (about) 1 #!/bin/python3 2 import re 3 import subprocess 4 from io import BytesIO 5 from typing import Tuple, List 6 7 from sys import ( 8 stdout as sys_stdout, 9 stderr as sys_stderr 10 ) 11 12 # https://dave.cheney.net/2020/05/02/mid-stack-inlining-in-go 13 14 gcflags = [ 15 "-m=2", 16 ] 17 18 re_cost = re.compile(rb"with cost (\d+) as:") 19 re_cost2 = re.compile(rb"cost (\d+) exceeds") 20 21 def main(): 22 h = subprocess.Popen( 23 args=["go","build","-gcflags="+" ".join(gcflags),"."], 24 stdout=subprocess.PIPE, 25 stderr=subprocess.PIPE) 26 ret: Tuple[str, str] = h.communicate() 27 stdout, stderr = ret 28 29 lines: List[bytes] = stderr.split(b"\n") 30 inlined_lines: List[bytes] = [line for line in lines if b"inline" in line] 31 32 can_inline: List[Tuple[bytes, int]] = [] 33 cannot_inline: List[Tuple[bytes, int]] = [] 34 for line in inlined_lines: 35 if b"can inline" in line: 36 inline_cost = int(re_cost.findall(line)[0]) 37 can_inline += [ (line, inline_cost) ] 38 elif b"cannot inline" in line: 39 cur_cost = 0 40 try: 41 cur_cost = int(re_cost2.findall(line)[0]) 42 except: pass 43 cannot_inline += [ (line, cur_cost) ] 44 else: 45 sys_stderr.write(b"[UNK] ") 46 sys_stderr.write(line) 47 sys_stderr.write(b"\n") 48 49 # sort by cost 50 # can_inline = sorted(can_inline, key=lambda v: v[1]) 51 # cannot_inline = sorted(cannot_inline, key=lambda v: v[1]) 52 53 for item in can_inline: 54 print( (str(item[1]).encode() +b"\t"+ item[0]) .decode() ) 55 56 print("============") 57 58 for item in cannot_inline: 59 print( (str(item[1]).encode() +b"\t"+ item[0]) .decode() ) 60 61 62 if __name__ == "__main__": 63 main()