Skip to content

Instantly share code, notes, and snippets.

@nascheme
nascheme / run_sphinx_bench.py
Created December 4, 2025 20:34
Benchmark of sphinx processing typing.rst file
#!/usr/bin/env ./python
import contextlib
import os
import sys
import shutil
import subprocess
import time
import venv
from pathlib import Path
import tempfile
@nascheme
nascheme / frozenset_opt.patch
Created November 19, 2025 21:22
frozenset changes for 3.14 backport
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-07-09-21-27-14.gh-issue-132657.kSA8R3.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-09-21-27-14.gh-issue-132657.kSA8R3.rst
new file mode 100644
index 00000000000..99f7a990875
--- /dev/null
+++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-09-21-27-14.gh-issue-132657.kSA8R3.rst
@@ -0,0 +1 @@
+Improve performance of :class:`frozenset` by removing locks in the free-threading build.
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-10-22-12-48-05.gh-issue-140476.F3-d1P.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-10-22-12-48-05.gh-issue-140476.F3-d1P.rst
new file mode 100644
index 00000000000..a24033208c5
@nascheme
nascheme / lazy_import.py
Created October 27, 2025 18:39
Lazy import implementation for Python
import importlib
import types
class _LazyModule(types.ModuleType):
def __init__(self, fullname):
self._lazy_module_name = fullname
def __getattribute__(self, attr):
# Stop triggering this method.
[600, 6107, 1667, 754, 6609, 999, 5030, 5668, 1135, 5712, 2386, 6015,
846, 1360, 5628, 2078, 6904, 2226, 855, 1254, 5861, 2130, 6110, 6148,
2853, 1050, 5890, 1937, 5417, 2153, 2255, 6883, 2173, 2189, 6914,
2822, 5629, 2079, 5492, 1291, 2227, 5572, 5672, 2344, 2801, 164, 6149,
719, 5904, 542, 5875, 483, 2776, 1105, 308, 2015, 6787, 6596, 2176,
2996, 5627, 1292, 5869, 6150, 5194, 6009, 165, 5741, 643, 2060, 2779,
1106, 5838, 6598, 5885, 1346, 5626, 6153, 5803, 910, 6319, 6160, 1739,
1293, 6255, 2324, 6011, 964, 2480, 166, 2780, 1575, 798, 6938, 423,
771, 1726, 6530, 1639, 1741, 5542, 1455, 5066, 6436, 5673, 167, 5297,
5116, 114, 2164, 2380, 6391, 5368, 674, 1136, 5664, 6016, 847, 6602,
[469, 470, 473, 16019, 6858, 11563, 11427, 12015, 5405, 16020, 471,
472, 475, 540, 6857, 19787, 7389, 11562, 6856, 11555, 6859, 11564,
5860, 5406, 3544, 3546, 3545, 6462, 6887, 12029, 12028, 12030, 12027,
19838, 5450, 9735, 9736, 9737, 8906, 9366, 8911, 9401, 6215, 6657,
6850, 6673, 5804, 5623, 5700, 6561, 3247, 3742, 3739, 3741, 3740,
5177, 5176, 5205, 5178, 9504, 9505, 9506, 7109, 3195, 9452, 7335,
7851, 7336, 7862, 3359, 3360, 3361, 5065, 7558, 5659, 4944, 6672,
7626, 3690, 3689, 7624, 5592, 7912, 5507, 7916, 5508, 7722, 7723,
5299, 7706, 4299, 4297, 4298, 4296, 3272, 3271, 7, 15964, 15965,
15967, 15963, 6, 8, 16151, 6681, 6693, 6680, 5434, 5436, 6986, 5435,
@nascheme
nascheme / python_explicit_rooting_mock.c
Created August 13, 2025 17:50
Explicit rooting of references for Python
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <limits.h>
#include <assert.h>
typedef size_t Py_ssize_t;
#define PY_SSIZE_T_MAX ULONG_MAX
/* dummy PyObject structure */
@nascheme
nascheme / gc_big.py
Created July 15, 2025 21:32
Python GC benchmark based on OCaml pre-fetch PR
import gc
import time
import statistics
# Adapted from https://gist.github.com/stedolan/4369a0fa27820e27d6e56bee5e412896
# See https://github.com/ocaml/ocaml/pull/10195
#
# In Python (free threading), this uses closer to 1.9 GiB of memory isntead of 800 MiB
# perf stat -e L1-dcache-loads,L1-dcache-load-misses,LLC-loads,LLC-load-misses ./python gc_big_800m.py
# From https://github.com/python/cpython/issues/132917
import itertools
import time
def performance_test(n_options=5, n_items=5, iterations=50):
list = []
def expensive_operation():
# Create lists of tuples
data = []
@nascheme
nascheme / gc_big_tree.py
Created June 25, 2025 17:46
BTree benchmark for testing cyclic GC performance
import sys
import gc
import random
import time
import collections.abc
import statistics
# perf stat -e L1-dcache-loads,L1-dcache-load-misses,LLC-loads,LLC-load-misses ./python gc_big_tree.py
# perf stat -e l3_request_g1.caching_l3_cache_accesses,l3_comb_clstr_state.request_miss ./python gc_big_tree.py
@nascheme
nascheme / bench_tp_lookup2.py
Created May 29, 2025 19:31
Benchmark for non-interned type lookup
import time
import threading
THREADS = 6
LOOPS = 200_000
class A:
x = 1
xx = 1