MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / main

Function main

scripts/memwaste-padding.py:94–170  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

92
93
94def main() -> int:
95 ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
96 ap.add_argument("--cc", default="clang")
97 ap.add_argument("--cxx", default="clang++")
98 ap.add_argument("--top", type=int, default=40)
99 ap.add_argument("--project-only", action="store_true")
100 ap.add_argument("--jobs", type=int, default=os.cpu_count() or 4)
101 ap.add_argument("--json", action="store_true")
102 args = ap.parse_args()
103
104 jobs = build_commands(args.cc, args.cxx)
105 if args.project_only:
106 jobs = [j for j in jobs if not is_vendored(j[2])]
107 if not jobs:
108 sys.exit("memwaste-padding: no compile commands found (make -n printed none for the compiler)")
109
110 holes: dict[str, dict] = {}
111 with cf.ThreadPoolExecutor(max_workers=args.jobs) as pool:
112 for source, stderr in pool.map(run_one, jobs):
113 for line in stderr.splitlines():
114 m = WARN_RE.match(line)
115 if not m:
116 continue
117 file = os.path.relpath(os.path.join(ROOT, m.group("file")), ROOT) \
118 if not os.path.isabs(m.group("file")) else os.path.relpath(m.group("file"), ROOT)
119 t = holes.setdefault(m.group("type"), {"type": m.group("type"), "interior": {}, "tail": 0,
120 "where": f"{file}:{m.group('line')}",
121 "vendored": is_vendored(file)})
122 n = int(m.group("bytes"))
123 if m.group("field") is not None:
124 t["interior"][m.group("field")] = n # a header seen by many TUs reports the same hole
125 else:
126 t["tail"] = n
127 t["where"] = f"{file}:{m.group('line')}"
128
129 # Allocation hint: how many allocation expressions size by this type.
130 sizeof_sites: dict[str, int] = defaultdict(int)
131 names = {t.split(" ", 1)[-1] for t in holes}
132 alloc_re = re.compile(r"(?:alloc|calloc|realloc|arena_alloc|CBM_DA_PUSH|cbm_da_push)\w*\s*\([^;]*sizeof\s*\(\s*"
133 r"(?:struct\s+)?(\w+)\s*\)")
134 for base in ("src", "internal/cbm"):
135 for dirpath, _, filenames in os.walk(os.path.join(ROOT, base)):
136 if "/vendored" in dirpath:
137 continue
138 for fn in filenames:
139 if not fn.endswith((".c", ".h")):
140 continue
141 try:
142 text = open(os.path.join(dirpath, fn), encoding="utf-8", errors="replace").read()
143 except OSError:
144 continue
145 for m in alloc_re.finditer(text):
146 if m.group(1) in names:
147 sizeof_sites[m.group(1)] += 1
148
149 rows = []
150 for t in holes.values():
151 interior = sum(t["interior"].values())

Callers 1

Calls 6

build_commandsFunction · 0.85
sumFunction · 0.85
is_vendoredFunction · 0.70
getMethod · 0.65
openFunction · 0.50
readMethod · 0.45

Tested by

no test coverage detected