Skip to content

Speed up package minification#21

Merged
alvinwan merged 4 commits intomainfrom
dev/alvin/speed-optimization
Apr 8, 2026
Merged

Speed up package minification#21
alvinwan merged 4 commits intomainfrom
dev/alvin/speed-optimization

Conversation

@alvinwan
Copy link
Copy Markdown
Owner

@alvinwan alvinwan commented Apr 8, 2026

Summary

  • cache class-local and module-level public member reference counts instead of rescanning the AST for each rename decision
  • reuse those cached counts inside _public_member_reference_count, which was the dominant hotspot in package mode

Measured Impact

One-shot package minification on the checked-in fixtures with keep_module_names=False, keep_global_variables=False, and rename_arguments=True:

  • click: 9.290s on origin/main -> 3.529s on this branch
  • pytest: 27.858s on origin/main -> 15.592s on this branch

A profiler run on the pytest fixture showed _public_member_reference_count and its repeated ast.walk calls dominating runtime on main; after caching, that hotspot drops out of the top line items and total runtime falls materially.

Validation

  • .venv/bin/python -m pytest -q
  • direct package-mode timing against .bench-repos/click/src and .bench-repos/pytest/src

@alvinwan alvinwan marked this pull request as ready for review April 8, 2026 06:40
@alvinwan alvinwan merged commit 2da7418 into main Apr 8, 2026
10 checks passed
@alvinwan alvinwan deleted the dev/alvin/speed-optimization branch April 8, 2026 06:40
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9105854f3e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread pymini/pymini.py
Comment on lines +424 to +426
cached = self._class_public_member_reference_cache.get(cache_key)
if cached is not None:
return cached
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Invalidate class member count cache during class traversal

_public_member_reference_count is called multiple times while the same class AST is being mutated, but this cache never refreshes once populated. In keep_global_variables=True flows, earlier methods can rewrite receiver names (for example selfa when rename_arguments=True), so later rename decisions should use the updated tree; reusing the old counts overestimates member usage and can trigger renames that increase output size (e.g. LongClassName.foo/bar gets bar renamed with an alias, producing longer minified code than before this commit).

Useful? React with 👍 / 👎.

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.

1 participant