Skip to content

Make wrapper generation more efficient - #126

Draft
kwabenantim wants to merge 1 commit into
developfrom
perf-generation-speedups
Draft

Make wrapper generation more efficient#126
kwabenantim wants to merge 1 commit into
developfrom
perf-generation-speedups

Conversation

@kwabenantim

Copy link
Copy Markdown
Member

Fixes #125

Profiling a from-scratch pychaste generation (758s) showed most of the
time was cppwg's own Python, not CastXML. Two hot spots dominated:

1. The `Path(a) in Path(b).parents` idiom, evaluated per declaration and
   per file across several phases (the source-declaration filter, unknown-
   class logging, auto-include resolution, file collection), allocated
   millions of Path objects and did O(depth) string-normalized compares.
   Replace it with utils.path_is_within(), a lexical normalized-string
   test with the same (strict, no-symlink) semantics as Path.parents.

2. ModuleInfo.sort_classes ran two C-by-C loops over the module's classes
   (~270 for pychaste) with utils.type_string_matches() inside, re-
   canonicalizing and re-compiling the same regex on every comparison
   (~11M re.sub calls). Precompute each class's canonical argument-type
   strings and a compiled whole-token regex for its name once, and cache
   the pairwise `requires` result. Factor the pattern build out of
   type_string_matches() into utils.compile_type_pattern() so both share it.

Also hoist the loop-invariant realpath() out of the per-declaration loop
in CppSourceParser.parse_instantiations().

These are equivalence-preserving refactors: the 274 pychaste wrappers are
byte-for-byte identical, the shapes example regenerates identically, and
the 480 unit tests pass. Measured on pychaste: 758.7s -> 275.2s (-64%),
with parse_headers -223s, resolve_auto_includes -77s, sort_classes -75s,
log_unknown_classes -57s.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@codecov

codecov Bot commented Aug 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.59%. Comparing base (f570136) to head (7131f0b).

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #126      +/-   ##
===========================================
- Coverage    99.75%   99.59%   -0.16%     
===========================================
  Files           31       31              
  Lines         2475     2488      +13     
  Branches       534      535       +1     
===========================================
+ Hits          2469     2478       +9     
- Misses           5        9       +4     
  Partials         1        1              
Flag Coverage Δ
cells 71.15% <85.71%> (+0.07%) ⬆️
shapes 71.14% <73.80%> (+0.03%) ⬆️
unit 92.96% <92.85%> (-0.21%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
cppwg/generators.py 99.44% <100.00%> (-0.01%) ⬇️
cppwg/info/module_info.py 100.00% <100.00%> (ø)
cppwg/info/package_info.py 99.42% <100.00%> (-0.01%) ⬇️
cppwg/parsers/source_parser.py 100.00% <100.00%> (ø)
cppwg/utils/utils.py 100.00% <100.00%> (ø)

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Speed up wrapper generation

1 participant