From e1b446437b62e018ba25baac6b76722711c6b17a Mon Sep 17 00:00:00 2001 From: r0ny123 <49360849+r0ny123@users.noreply.github.com> Date: Tue, 25 Aug 2026 22:30:00 +0530 Subject: [PATCH] count the procs and publics diagnose() has already read diagnose() walks every module stream to count record kinds, then called module_procs(), which re-reads and re-slices each of those same streams to extract the procedures it counts. Publics were worse: the symbol-record stream is already read for the kind counts and the truncation check, and public_symbols() read it a second time and then ordered the result through the publics hash -- an ordering the len() around it discards. Both counts now come from bytes the function is already holding. The totals are unchanged on all seven fixtures. Not a measurable speedup, and worth saying so rather than leaving the next reader to assume one: diagnose() is dominated by the record walks themselves -- count_malformed_records alone runs twice per module, once for the general count and once for S_INLINESITE -- against which the duplicated reads removed here do not register. --- purepdb/pdb.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/purepdb/pdb.py b/purepdb/pdb.py index 923a797..17e50bd 100644 --- a/purepdb/pdb.py +++ b/purepdb/pdb.py @@ -1115,6 +1115,10 @@ def diagnose(self) -> Diagnostics: malformed = 0 malformed_inline = 0 line_bytes = 0 + # Collected from the same `body` the counts below come from. + # `module_procs()` re-reads and re-slices every module stream to + # answer a length this loop is already holding the bytes for. + procs: list[codeview.ProcSymbol] = [] for mod in self.dbi.modules: line_bytes += len(self.module_c13_bytes(mod)) body = self.module_symbol_bytes(mod) @@ -1129,10 +1133,12 @@ def diagnose(self) -> Diagnostics: kinds[kind] = kinds.get(kind, 0) + count for t in report: truncations.append((f"module {mod.index} ({mod.module_name})", t)) + procs.extend(codeview.extract_procs(body)) idx = self.dbi.symrecord_stream_index proc_refs = undecoded_constants = unresolvable_refs = 0 proc_ref_targets: dict[int, int] = {} + public_records = 0 thread_locals = sum(kinds.get(k, 0) for k in codeview.THREAD_KINDS) if self.msf.is_valid_stream(idx): symrecords = self.msf.read_stream(idx) @@ -1146,6 +1152,10 @@ def diagnose(self) -> Diagnostics: symrecord_kinds = codeview.count_kinds(symrecords) proc_refs = sum(symrecord_kinds.get(k, 0) for k in codeview.PROC_REF_KINDS) + # Likewise `public_symbols()`, which would read this stream a + # second time and then order it through the publics hash -- an + # ordering the count discards. + public_records = len(codeview.extract_publics(symrecords)) # What those refs point at, which is the only way to say whether a # count differing from `proc_records` means anything is missing. # Grouped by module because the stream cache holds one stream: on @@ -1169,8 +1179,8 @@ def diagnose(self) -> Diagnostics: return Diagnostics( modules=len(self.dbi.modules), modules_with_symbols=with_symbols, - proc_records=len(self.module_procs()), - public_records=len(self.public_symbols()), + proc_records=len(procs), + public_records=public_records, has_section_headers=self._sections is not None, module_kinds=kinds, malformed_records=malformed,