8000 [codex] improve package-mode compression by alvinwan · Pull Request #13 · 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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

`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 `74%` on the checked-in fixtures and
shrink Python code by roughly `50%` to `75%` on the checked-in fixtures and
validated package benchmarks when aggressive renaming is enabled.

- [Getting Started](#getting-started)
Expand Down Expand Up @@ -44,10 +44,10 @@ Current checked-in fixtures, using

| Input | Original | Minified | Reduction |
| --- | ---: | ---: | ---: |
| `tests/examples/pyminifier.py` | `1,355` bytes | `444` bytes | `67.2%` |
| `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 | `25,621` bytes | `73.9%` |
| `TexSoup/` compressed source (`.tar.gz`) | `23,119` bytes | `9,209` bytes | `60.2%` |
| `TexSoup/` raw Python source (`*.py`) | `98,181` bytes | `24,722` bytes | `74.8%` |
| `TexSoup/` compressed source (`.tar.gz`) | `23,656` bytes | `9,208` bytes | `61.1%` |

For baseline comparisons, speed results, and TexSoup validation details, see
[benchmarks/README.md](./benchmarks/README.md).
Expand Down
14 changes: 7 additions & 7 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ the benchmark harness used to reproduce them.

| Input | Original | `pymini` size | `pymini` speed | `pyminifier` size | `pyminifier` speed | `python-minifier` size | `python-minifier` speed |
| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: |
| `pyminifier.py` | `1,355` bytes | `444` bytes, `67.2%` | `1.2 ms` | `611` bytes, `54.9%` | `0.4 ms` | `1,020` bytes, `24.7%` | `1.9 ms` |
| `pyminify.py` | `1,990` bytes | `935` bytes, `53.0%` | `4.7 ms` | `1,540` bytes, `22.6%` | `1.3 ms` | `983` bytes, `50.6%` | `5.3 ms` |
| `TexSoup/*.py` | `98,181` bytes | `25,621` bytes, `73.9%` | `95.2 ms` | `34,643` bytes, `64.7%` | `27.8 ms` | `83,303` bytes, `15.2%` | `120.5 ms` |
| `TexSoup.tar.gz` | `23,119` bytes | `9,209` bytes, `60.2%` | `95.2 ms` | `9,725` bytes, `57.9%` | `27.8 ms` | `21,504` bytes, `7.0%` | `120.5 ms` |
| `pyminifier.py` | `1,355` bytes | `438` bytes, `67.7%` | `1.9 ms` | `611` bytes, `54.9%` | `0.4 ms` | `1,020` bytes, `24.7%` | `1.6 ms` |
| `pyminify.py` | `1,990` bytes | `935` bytes, `53.0%` | `5.4 ms` | `1,540` bytes, `22.6%` | `1.3 ms` | `983` bytes, `50.6%` | `4.1 ms` |
| `TexSoup/*.py` | `98,181` bytes | `24,722` bytes, `74.8%` | `175.6 ms` | `34,643` bytes, `64.7%` | `28.0 ms` | `83,303` bytes, `15.2%` | `118.3 ms` |
| `TexSoup.tar.gz` | `23,656` bytes | `9,208` bytes, `61.1%` | `175.6 ms` | `9,725` bytes, `58.9%` | `28.0 ms` | `21,509` bytes, `9.1%` | `118.3 ms` |

`pymini` is benchmarked with `--rename-modules --rename-global-variables --rename-arguments`.
`TexSoup/*.py` compares validated package outputs. `pymini` uses package mode;
Expand All @@ -38,10 +38,10 @@ PYTHONPATH=. .venv/bin/python benchmarks/benchmark_speed.py --pyminifier-root /t
`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
`73.9%` smaller, and compressed source code (`.tar.gz`) is `60.2%` smaller when
measured on clean `.py`-only package snapshots.
`74.8%` smaller, and compressed source code (`.tar.gz`) is `61.1%` smaller
when measured on clean `.py`-only package snapshots.

<!-- Raw bytes: 98,181 -> 25,621. Compressed bytes: 23,119 -> 9,209. -->
<!-- Raw bytes: 98,181 -> 24,722. Compressed bytes: 23,656 -> 9,208. -->

To reproduce that flow locally:

Expand Down
30 changes: 20 additions & 10 deletions benchmarks/benchmark_speed.py
8000
Original file line number Diff line numberDiff line change
Expand Up @@ -19,16 +19,11 @@
EXAMPLE_DIR = ROOT / "tests" / "examples"
DEFAULT_TEXSOUP_ROOT = Path("/tmp/pymini-texsoup-repo/TexSoup")
DEFAULT_PYMINIFIER_ROOT = Path("/tmp/pymini-pyminifier-src/pyminifier-2.1")
PYMINI_BENCHMARK_KWARGS = {
PYMINI_AGGRESSIVE_OPTIONS = {
"keep_module_names": False,
"keep_global_variables": False,
"rename_arguments": True,
}
PYMINI_CLI_FLAGS = [
"--rename-modules",
"--rename-global-variables",
"--rename-arguments",
]


def benchmark_transform(
Expand Down Expand Up @@ -57,7 +52,12 @@ def benchmark_transform(

def pymini_single_file_transform(path: Path):
def transform(source: str) -> str:
outputs, _ = minify(source, path.stem, **PYMINI_BENCHMARK_KWARGS)
outputs, _ = minify(
source,
path.stem,
keep_global_variables=False,
rename_arguments=True,
)
return outputs[0]

return transform
Expand Down Expand Up @@ -108,7 +108,7 @@ def benchmark_package_api(
minify(
sources,
modules,
**PYMINI_BENCHMARK_KWARGS,
**PYMINI_AGGRESSIVE_OPTIONS,
)
samples = []
outputs = None
Expand All @@ -117,7 +117,7 @@ def benchmark_package_api(
outputs, _ = minify(
sources,
modules,
**PYMINI_BENCHMARK_KWARGS,
**PYMINI_AGGRESSIVE_OPTIONS,
)
samples.append(perf_counter() - start)
raw_bytes = sum(len(source.encode()) for source in sources)
Expand All @@ -139,7 +139,17 @@ def benchmark_package_cli(package_root: Path, *, iterations: int) -> dict[str, f
output_dir = Path(tempfile.mkdtemp(prefix="pymini-bench-"))
try:
start = perf_counter()
rc = cli_main(["package", str(package_root), *PYMINI_CLI_FLAGS, "-o", str(output_dir)])
rc = cli_main(
[
"package",
str(package_root),
"--rename-modules",
"--rename-global-variables",
"--rename-arguments",
"-o",
str(output_dir),
]
)
samples.append(perf_counter() - start)
if rc != 0:
raise RuntimeError(f"pymini CLI returned {rc}")
Expand Down
36 changes: 18 additions & 18 deletions examples/pyminify.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
def a(event,context):
e='RequestType';f='PhysicalResourceId';g='None';h='Status';i='SUCCESS';j='Tags';k='OldResourceProperties';l.info(event);a,m,n,o,p,q,r,s,t=(event,create_cert,add_tags,validate,wait_for_issuance,context,send,reinvoke,acm)
e='RequestType';f='PhysicalResourceId';g='None';h='Status';i='SUCCESS';j='Tags';k='OldResourceProperties';l.info(event);m,n,o,p,q,r,s,t,u=(event,create_cert,add_tags,validate,wait_for_issuance,context,send,reinvoke,acm)
try:
b=hashlib.new('md5',(a['RequestId']+a['StackId']).encode()).hexdigest();c=a['ResourceProperties']
if a[e]=='Create':
a[f]=g;a[f]=m(c,b);n(a[f],c);o(a[f],c)
if p(a[f],q):a[h]=i;return r(a)
else:return s(a,q)
elif a[e]=='Delete':
if a[f]!=g:t.delete_certificate(CertificateArn=a[f])
a[h]=i;return r(a)
elif a[e]=='Update':
if replace_cert(a):
a[f]=m(c,b);n(a[f],c);o(a[f],c)
if not p(a[f],q):return s(a,q)
b=hashlib.new('md5',(m['RequestId']+m['StackId']).encode()).hexdigest();c=m['ResourceProperties']
if m[e]=='Create':
m[f]=g;m[f]=n(c,b);o(m[f],c);p(m[f],c)
if q(m[f],r):m[h]=i;return s(m)
else:return t(m,r)
elif m[e]=='Delete':
if m[f]!=g:u.delete_certificate(CertificateArn=m[f])
m[h]=i;return s(m)
elif m[e]=='Update':
if replace_cert(m):
m[f]=n(c,b);o(m[f],c);p(m[f],c)
if not q(m[f],r):return t(m,r)
else:
if j in a[k]:t.remove_tags_from_certificate(CertificateArn=a[f],Tags=a[k][j])
n(a[f],c)
a[h]=i;return r(a)
if j in m[k]:u.remove_tags_from_certificate(CertificateArn=m[f],Tags=m[k][j])
o(m[f],c)
m[h]=i;return s(m)
else:raise RuntimeError('Unknown RequestType')
except Exception as d:l.exception('');a[h]='FAILED';a['Reason']=str(d);return r(a)
del (a,m,n,o,p,q,r,s,t)
except Exception as d:l.exception('');m[h]='FAILED';m['Reason']=str(d);return s(m)
del (m,n,o,p,q,r,s,t,u)
handler=a
2 changes: 1 addition & 1 deletion pymini/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def build_parser() -> ArgumentParser:
parser.add_argument(
'--rename-arguments',
action='store_true',
help='Rename function and method parameter names, including matching internal keyword callers.',
help='Rename function and method arguments, including internal keyword call sites when safe.',
)
parser.add_argument('--single-file', action='store_true', help=SUPPRESS)
parser.add_argument('-o', '--output', help='Path to the output directory', default='./')
Expand Down
Loading
Loading
0