8000 [codex] Fix package import rewrites and benchmark docs by alvinwan · Pull Request #15 · alvinwan/pymini · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10000
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,6 @@ dmypy.json

# Pyre type checker
.pyre/

# Local benchmark repo clones
.bench-repos/
25 changes: 12 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# pymini

`pymini` is an AST-based Python minifier for scripts and packages. It preserves
package layout by default, can emit a single-file bundle when asked, and can
shrink Python code by roughly `50%` to `75%` on the checked-in fixtures and
validated package benchmarks when aggressive renaming is enabled.
`pymini` is an AST-based Python minifier for scripts and packages. It can
shrink raw Python source code by up to **4x** and the associated `.whl` files
by up to **7.3x**.

- [Getting Started](#getting-started)
- [Installation](#installation)
Expand Down Expand Up @@ -39,18 +38,18 @@ sources, modules = minify(

# Compression

Current checked-in fixtures, using
Representative compression results, using
`--rename-modules --rename-global-variables --rename-arguments`:

| Input | Original | Minified | Reduction |
| --- | ---: | ---: | ---: |
| `tests/examples/pyminifier.py` | `1,355` bytes | `438` bytes | `67.7%` |
| `tests/examples/pyminify.py` | `1,990` bytes | `935` bytes | `53.0%` |
| `TexSoup/` raw Python source (`*.py`) | `98,181` bytes | `24,288` bytes | `75.3%` |
| `TexSoup/` compressed source (`.tar.gz`) | `23,101` bytes | `8,363` bytes | `63.8%` |
| Input | Original bytes | pymini | pyminifier | python-minifier |
| --- | ---: | ---: | ---: | ---: |
| TexSoup | 98,181 | 4.0x | 2.8x | 1.2x |
| timefhuman | 119,155 | 1.9x | 1.2x | 1.6x |
| rich | 1,217,001 | 2.6x | failed | 1.6x |

For baseline comparisons, speed results, and TexSoup validation details, see
[benchmarks/README.md](./benchmarks/README.md).
For the above results, we validate that all package tests still run and pass,
with the minified source code. For the full compression tables, speed results,
and package validation, see [benchmarks/README.md](./benchmarks/README.md).

# Installation

Expand Down
142 changes: 123 additions & 19 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,110 @@ the benchmark harness used to reproduce them.

- [Results](#results)
- [Reproduce](#reproduce)
- [TexSoup Validation](#texsoup-validation)
- [Validation](#validation)

# Results

| Input | Original | `pymini` size | `pymini` speed | `pyminifier` size | `pyminifier` speed | `python-minifier` size | `python-minifier` speed |
| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: |
| `pyminifier.py` | `1,355` bytes | `438` bytes, `67.7%` | `2.1 ms` | `611` bytes, `54.9%` | `0.4 ms` | `1,020` bytes, `24.7%` | `1.7 ms` |
| `pyminify.py` | `1,990` bytes | `935` bytes, `53.0%` | `5.9 ms` | `1,540` bytes, `22.6%` | `1.2 ms` | `983` bytes, `50.6%` | `4.3 ms` |
| `TexSoup/*.py` | `98,181` bytes | `24,288` bytes, `75.3%` | `127.5 ms` | `34,643` bytes, `64.7%` | `29.7 ms` | `83,303` bytes, `15.2%` | `123.6 ms` |
| `TexSoup.tar.gz` | `23,101` bytes | `8,363` bytes, `63.8%` | `127.5 ms` | `9,718` bytes, `57.9%` | `29.7 ms` | `21,497` bytes, `6.9%` | `123.6 ms` |
## Compression

Compression multipliers below are all relative to the original raw Python
source bytes for that repo.

### Minify Only

| Package | Original bytes | pymini | pyminifier | python-minifier |
| --- | ---: | ---: | ---: | ---: |
| TexSoup | 98,181 | 4.0x | 2.8x | 1.2x |
| timefhuman | 119,155 | 1.9x | 1.2x | 1.6x |
| pyminifier | 90,901 | 2.9x | 1.8x | 1.8x |
| rich | 1,217,001 | 2.6x | failed | 1.6x |

| Package | Original bytes | pymini bytes | pyminifier bytes | python-minifier bytes |
| --- | ---: | ---: | ---: | ---: |
| TexSoup | 98,181 | 24,724 | 34,643 | 83,303 |
| timefhuman | 119,155 | 63,821 | 98,576 | 76,466 |
| pyminifier | 90,901 | 31,372 | 50,022 | 51,574 |
| rich | 1,217,001 | 471,254 | failed | 752,839 |

Minify-only failures:

- rich + pyminifier: minification fails on `rich/__init__.py` with
`TypeError: 'NoneType' object is not subscriptable` in
`pyminifier.minification.reduce_operators`.

### tar.gz Results

| Package | Original bytes | tar.gz only | pymini + tar.gz | pyminifier + tar.gz | python-minifier + tar.gz |
| --- | ---: | ---: | ---: | ---: | ---: |
| TexSoup | 98,181 | 4.3x | 11.5x | 10.1x | 4.6x |
| timefhuman | 119,155 | 4.7x | 5.9x | 5.1x | 5.5x |
| pyminifier | 90,901 | 4.2x | 8.0x | 7.4x | 5.5x |
| rich | 1,217,001 | 5.5x | 10.5x | failed | 6.5x |

| Package | Original tar.gz bytes | pymini + tar.gz bytes | pyminifier + tar.gz bytes | python-minifier + tar.gz bytes |
| --- | ---: | ---: | ---: | ---: |
| TexSoup | 22,990 | 8,522 | 9,740 | 21,530 |
| timefhuman | 25,222 | 20,338 | 23,154 | 21,809 |
| pyminifier | 21,820 | 11,357 | 12,354 | 16,524 |
| rich | 220,870 | 115,910 | failed | 185,878 |

tar.gz failures:

- rich + pyminifier: the same minification failure prevents the compressed
package snapshot from being produced.

### Wheel Results

Wheel builds are stricter than raw package snapshots because they exercise the
repo's actual packaging metadata.

| Package | Original bytes | wheel only | pymini + wheel | pyminifier + wheel | python-minifier + wheel |
| --- | ---: | ---: | ---: | ---: | ---: |
| TexSoup | 98,181 | 3.4x | 7.3x | 6.6x | 3.6x |
| timefhuman | 119,155 | 2.9x | 3.4x | 3.1x | 3.2x |
| pyminifier | 90,901 | 1.1x | 1.5x | 1.4x | 1.2x |
| rich | 1,217,001 | 3.9x | 6.6x | failed | 4.6x |

| Package | Original wheel bytes | pymini wheel bytes | pyminifier wheel bytes | python-minifier wheel bytes |
| --- | ---: | ---: | ---: | ---: |
| TexSoup | 28,773 | 13,475 | 14,793 | 27,050 |
| timefhuman | 40,591 | 35,094 | 38,229 | 37,092 |
| pyminifier | 79,693 | 61,088 | 64,019 | 76,101 |
| rich | 310,458 | 183,326 | failed | 266,399 |

`pymini` is benchmarked with `--rename-modules --rename-global-variables --rename-arguments`.
`TexSoup/*.py` compares validated package outputs. `pymini` uses package mode;
the baselines minify each file independently in the preserved package tree. All
three outputs pass the upstream TexSoup test suite (`78` tests). The
`TexSoup.tar.gz` row reuses those same minification timings and only changes
the size measurement to the compressed archive.
The `pymini` rows use package mode. The baselines minify each file
independently in the preserved package tree. For wheel rows, each tool first
rewrites the package tree, then `python -m build --wheel` runs on that
rewritten checkout.

Wheel-specific failures:

- pyminifier wheel rows use a small benchmark-only compatibility patch for the
repo's legacy `setup.py` and `collections.Iterable` imports so the original
project still builds on Python 3.13.
- rich + pyminifier: minification fails on `rich/__init__.py` with
`TypeError: 'NoneType' object is not subscriptable` in
`pyminifier.minification.reduce_operators`, so the wheel build never starts.

## Speed

| Input | pymini | pyminifier | python-minifier |
| --- | ---: | ---: | ---: |
| pyminifier.py | 11.8 ms | 1.7 ms | 7.5 ms |
| pyminify.py | 25.3 ms | 4.4 ms | 24.2 ms |
| TexSoup | 124.9 ms | 52.2 ms | 117.2 ms |
| timefhuman | 352.0 ms | 71.0 ms | 266.0 ms |
| pyminifier | 137.1 ms | 35.6 ms | 114.8 ms |
| rich | 3286.6 ms | failed | 1838.7 ms |

Speed failures:

- rich + pyminifier: the same minification failure prevents a timing result.

The single-file rows come from [benchmark_speed.py](./benchmark_speed.py). The
package rows are one-shot package minification timings from the same
environment used for the compression comparison.

# Reproduce

Expand All @@ -33,15 +120,32 @@ git clone https://github.com/liftoff/pyminifier /tmp/pyminifier
PYTHONPATH=. .venv/bin/python benchmarks/benchmark_speed.py --pyminifier-root /tmp/pyminifier
```

# TexSoup Validation
The larger package comparisons in this file were run against these checkouts:

- `TexSoup`
- `timefhuman`
- `pyminifier`
- `rich`

# Validation

Each package result above was checked against the repo's own test suite using a
temporary minified package tree on `PYTHONPATH`.

| Package | pymini | pyminifier | python-minifier |
| --- | --- | --- | --- |
| TexSoup | 78 passed | 78 passed | 78 passed |
| timefhuman | 187 passed | 187 passed | 187 passed |
| rich | 952 passed, 25 skipped | build failed | 952 passed, 25 skipped |

`pyminifier` failed on `rich` before tests ran, with an `IndentationError`
during minification. The `pyminifier` repo itself is omitted here because one
upstream test shells out to a `pyminifier` executable on `PATH`, which is not a
like-for-like package-tree validation.

`pymini` has been validated against the upstream `TexSoup` test suite in
package mode with `--rename-modules --rename-global-variables --rename-arguments`.
Current validation: upstream pytest passes (`78` tests), raw source code is
`75.3%` smaller, and compressed source code (`.tar.gz`) is `63.8%` smaller
when measured on clean `.py`-only package snapshots.
## TexSoup

<!-- Raw bytes: 98,181 -> 24,288. Compressed bytes: 23,101 -> 8,363. -->
<!-- Raw bytes: 98,181 -> 24,724. Compressed bytes: 22,990 -> 8,522. -->

To reproduce that flow locally:

Expand Down
30 changes: 26 additions & 4 deletions pymini/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ def module_name_from_relative_path(path: Path) -> str:
def load_sources(paths: Iterable[Path], *, module_root: Optional[Path]) -> tuple[list[str], list[str], dict[str, Path]]:
sources, modules = [], []
module_to_output_path = {}
package_prefix = None
if module_root is not None and (module_root / "__init__.py").is_file():
package_prefix = module_root.name
for path in paths:
sources.append(path.read_text(encoding="utf-8"))
if module_root is None:
Expand All @@ -101,6 +104,12 @@ def load_sources(paths: Iterable[Path], *, module_root: Optional[Path]) -> tuple
else:
output_path = path.relative_to(module_root)
module = module_name_from_relative_path(output_path)
if package_prefix is not None:
module = (
package_prefix
if module == "__init__"
else f"{package_prefix}.{module}"
)
modules.append(module)
module_to_output_path[module] = output_path
return sources, modules, module_to_output_path
Expand All @@ -124,6 +133,7 @@ def write_outputs(
single_file: bool,
keep_module_names: bool,
module_to_output_path: dict[str, Path],
original_modules: Sequence[str],
) -> None:
if single_file:
destination = output if output.suffix == ".py" else output / f"{modules[0]}.py"
Expand All @@ -135,11 +145,21 @@ def write_outputs(
raise ValueError("output must be a directory unless --single-file is set")

output.mkdir(parents=True, exist_ok=True)
for source, module in zip(sources, modules):
for source, module, original_module in zip(sources, modules, original_modules):
original_output_path = module_to_output_path.get(original_module)
destination = (
output / module_to_output_path[module]
if keep_module_names and module in module_to_output_path
else output / f"{module}.py"
output / original_output_path
if (
original_output_path is not None and (
keep_module_names
or origina A3A6 l_output_path.name == "__init__.py"
)
)
else (
output / original_output_path.parent / f"{module.rsplit('.', 1)[-1]}.py"
if original_output_path is not None
else output / Path(*module.split(".")).with_suffix(".py")
)
)
destination.parent.mkdir(parents=True, exist_ok=True)
destination.write_text(source, encoding="utf-8")
Expand All @@ -158,6 +178,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
ensure_unique_modules(modules)
except ValueError as exc:
parser.error(str(exc))
original_modules = list(modules)
cleaned, modules = minify(
sources,
modules,
Expand All @@ -174,6 +195,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
single_file=output_single_file,
keep_module_names=keep_module_names,
module_to_output_path=module_to_output_path,
original_modules=original_modules,
)
except ValueError as exc:
parser.error(str(exc))
Expand Down
Loading
Loading
0