From 0147c02f2a6dc2debef62313acec3af186a7d65e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l=20Gr=C3=B8n=C3=A5s=20Drange?= Date: Mon, 3 Nov 2025 12:09:23 +0100 Subject: [PATCH 1/2] Add additional gc benchmark with pickletools (#437) --- .../benchmarks/bm_pickle_opt/pyproject.toml | 9 ++++ .../benchmarks/bm_pickle_opt/run_benchmark.py | 45 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 pyperformance/data-files/benchmarks/bm_pickle_opt/pyproject.toml create mode 100644 pyperformance/data-files/benchmarks/bm_pickle_opt/run_benchmark.py diff --git a/pyperformance/data-files/benchmarks/bm_pickle_opt/pyproject.toml b/pyperformance/data-files/benchmarks/bm_pickle_opt/pyproject.toml new file mode 100644 index 00000000..07235075 --- /dev/null +++ b/pyperformance/data-files/benchmarks/bm_pickle_opt/pyproject.toml @@ -0,0 +1,9 @@ +[project] +name = "pyperformance_bm_pickle_opt" +requires-python = ">=3.8" +dependencies = ["pyperf"] +urls = {repository = "https://github.com/python/pyperformance"} +dynamic = ["version"] + +[tool.pyperformance] +name = "pickle_opt" diff --git a/pyperformance/data-files/benchmarks/bm_pickle_opt/run_benchmark.py b/pyperformance/data-files/benchmarks/bm_pickle_opt/run_benchmark.py new file mode 100644 index 00000000..eb64bc5a --- /dev/null +++ b/pyperformance/data-files/benchmarks/bm_pickle_opt/run_benchmark.py @@ -0,0 +1,45 @@ +"""The background for this benchmark is that the garbage collection in Python 3.14 +had a performance regression, see + +* https://github.com/python/cpython/issues/140175 +* https://github.com/python/cpython/issues/139951 + +""" + +import tempfile +from pathlib import Path + +import pyperf + + +def setup(fname, N): + import pickle + + x = {} + for i in range(1, N): + x[i] = f"ii{i:>07}" + + with open(fname, "wb") as fh: + pickle.dump(x, fh, protocol=4) + + +def run(fname): + import pickletools + + with open(fname, "rb") as fh: + p = fh.read() + + s = pickletools.optimize(p) + + with open(fname.with_suffix(".out"), "wb") as fh: + fh.write(s) + + +if __name__ == "__main__": + runner = pyperf.Runner() + N = 1_000_000 + tmp_path = Path(tempfile.mkdtemp()) + fname = tmp_path / "pickle" + setup(fname, N) + runner.metadata["description"] = "Pickletools optimize" + runner.bench_func("pickle_opt", run, fname) From 634229cdf0b32846f5f4eb8d5e9e88f06148bfa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l=20Gr=C3=B8n=C3=A5s=20Drange?= Date: Wed, 5 Nov 2025 16:46:41 +0100 Subject: [PATCH 2/2] Use TemporaryDirectory context manager --- .../benchmarks/bm_pickle_opt/run_benchmark.py | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/pyperformance/data-files/benchmarks/bm_pickle_opt/run_benchmark.py b/pyperformance/data-files/benchmarks/bm_pickle_opt/run_benchmark.py index eb64bc5a..8872fddc 100644 --- a/pyperformance/data-files/benchmarks/bm_pickle_opt/run_benchmark.py +++ b/pyperformance/data-files/benchmarks/bm_pickle_opt/run_benchmark.py @@ -1,5 +1,5 @@ -"""The background for this benchmark is that the garbage collection in Python 3.14 -had a performance regression, see +"""The background for this benchmark is that the garbage collection in +Python 3.14.0 had a performance regression, see * https://github.com/python/cpython/issues/140175 * https://github.com/python/cpython/issues/139951 @@ -8,13 +8,12 @@ import tempfile from pathlib import Path - import pyperf +import pickle +import pickletools def setup(fname, N): - import pickle - x = {} for i in range(1, N): x[i] = f"ii{i:>07}" @@ -24,8 +23,6 @@ def setup(fname, N): def run(fname): - import pickletools - with open(fname, "rb") as fh: p = fh.read() @@ -38,8 +35,9 @@ def run(fname): if __name__ == "__main__": runner = pyperf.Runner() N = 1_000_000 - tmp_path = Path(tempfile.mkdtemp()) - fname = tmp_path / "pickle" - setup(fname, N) - runner.metadata["description"] = "Pickletools optimize" - runner.bench_func("pickle_opt", run, fname) + with tempfile.TemporaryDirectory() as tmp: + tmp_path = Path(tmp) + fname = tmp_path / "pickle" + setup(fname, N) + runner.metadata["description"] = "Pickletools optimize" + runner.bench_func("pickle_opt", run, fname)