Python is fast enough. Python programmers tend to understand the Python Cost Model, Python’s strengths and weaknesses, libraries that give compiled performance, and when to use a compiled language from the start.
So why do I care? Why do I get obsessed enough to coerce Claude into running these benchmarks and writing these Plotly charts? I do not know.1
But! I do know what I care about (for now) - and today (and some of the past weekend, and perhaps some of the next one), it’s definitely the cost of defining (ideally immutable) record types (AKA structs) in Python.
So let’s get this out of the way: this write up is about benchmarking “Python type speed” (informally: compile-time), it is NOT about benchmarking
Right, so that’s what Python programmers often care about, because they are probably working on long running programs, like apps, servers or pipelines, where the cost of defining a type is paid upfront, one time, whereas the cost of allocation, instantiation, validation, and serialization is paid repeatedly. So yeah, if that’s what you care about, this post is not for you.
But I did include instance cost benchmarks if you’re curious. 😻
If you already know you care about type definition speed, then jump straight to the analysis, otherwise keep reading for my motivation and context on this subject.
#”how fast to--help”
I tend to work on CLIs for developers and tooling for build systems or test suites where the time from program start to end is what we’re measuring. Perhaps you’ve noticed that running a command from a CLI may be near instant in a compiled program, but in Python, it can easily be hundreds of milliseconds: perceptible for UX, noticeable in CI/CD, and amplified by repeated calls as part of build system tooling.
Unlike in a compiled language, Python type definitions are not free (free in the sense that they were paid for during compilation ahem, Rust). They are code to be executed on every startup. And that includes imports of libraries and their type trees and dependents trees. We’ll see in the benchmarks that (evil-runtime-) metaprogramming, like decorators, metaclasses, or worse, have more of an upfront runtime type generation cost than manual type definitions.
Can we get the best of everything: a Pythonic type definition style, complete static typing and match, with the speed of a hand-written C struct, and the startup time of a compiled extension? I think so. seriously, I’m not sure, need to do more work, but I have good preliminary data
But why not use a compiled language and framework like Rust + clap? I certainly do, but what can I say? I love the Python ecosystem, build tooling libraries, and the rapidly evolving type system. And I believe that the type system can continue evolving so that we can offload a lot of the correctness to the type checker, and reap runtime speed benefits. That’s what this post is about.
#OK, OK, whatever, but why “structs”?I’ll confess that I am an advocate of functional programming (FP), with little compromise. But the tortured kind, that can’t be bothered to learn Haskell, or study Lisp, and seems to end up rewriting the same handful of patterns in every language. So, it’s not the structs alone that I am after. It’s the sum types and pattern matching.
Long story short, I use sum types and pattern matching everywhere, all the time, from Rust to embedded C, from Typescript to Python, from JSON to CBOR. Even if your not an FP…enthusiast, you’ve likely used them in Python without thinking of them as such, when reaching for MyType | None (an Option or Maybe type).
This example imagines that some immutable device info burned onto a ROM is versioned V1 and V2. V1 guaranteed presence of the serial number, but not the manufactured date. V2 guarantees both and adds a bootloader SHA.
from typing import NamedTuple
class DeviceInfoV1(NamedTuple):
serial_number: str
manufactured_utc_ms: int | None
class DeviceInfoV2(NamedTuple):
serial_number: str
manufactured_utc_ms: int
bootloader_sha: int
type DeviceInfo = DeviceInfoV1 | DeviceInfoV2
DeviceInfo is a sum type of two product types,DeviceInfoV1 andDeviceInfoV2, and there are only two representable states, each validated by the type system, not at runtime. Here’s what the naive product type would look like:
class DeviceInfo(NamedTuple):
serial_number: str
manufactured_utc_ms: int | None
bootloader_sha: int | None
Invalid runtime states are now possible: DeviceInfo(serial_number="abc", manufactured_utc_ms=None, bootloader_sha=123) is a valid instance of the naive product type, but it is not a valid DeviceInfoV1 or DeviceInfoV2.
Using a product type instead of a sum type shifts the burden of correctness from the type system to the runtime.
#Aw, f&*#!I promised myself I wouldn’t evangelize FP (Day 583). 💀
It’s not really about FP, that just happens to be my motivation. There are plenty of different ways to utilize Abstract Data Types (ADTs) in Python, and if you care about Python startup time, then I think you’ll enjoy these benchmark results.
Besides, this can’t be about FP, because functional programmers don’t care about performance, memory, or know anything about compilers and instruction sets.
“Functional programming, strictly defined, is dumb…the way you manage mutable state is by making an entire copy of the data structure with the changes in the new copy of the data structure…here’s the problem: computers, they’re all bags of mutable state.”
Chris Lattner, Creator Of Swift On Functional Programming (YouTube)
Odd for the creator of LLVM, Clang, Swift, and Mojo to mischaracterize FP as anything other than an abstraction. I wasn’t aware of the “functional” instruction sets competing with x86 and ARM.
#WTF are we testing again?I use NamedTuple all the time, mostly because it means I don’t have to add @dataclass(frozen=true) everywhere, but in the back of my mind I have always believed that NamedTuple must be super efficient and compact, like const struct in C or struct in Rust. Once I realized that I’d been carrying on with this belief for years, I decided to setup this benchmark to understand how much I was truly paying for my types.
*author’s commentary italicized to avoid bias
record-type): “Manual Record Type” oh god that’s even worse, this IS a waste of time, we’re going to turn Python into Java or somethingtyping module: NamedTuple pewwwwww pew pew pewwwwwwoooodataclasses: dataclass(frozen=True) boooooooooo metaprogramming suuuuuuuuuuucks…unless it’s rust’s bs…or constexpr…at least it’s not C macros…but booooooooooooooooooorecord-type a new hopemsgspec and record-type *WITH ATTRIBUTION wait, I’m calling it record-type (C)!?!?! hey, 20 minutes is not bad, it usually costs me $200 to get slop CPython!attrs 🎺 medieval horns, but in tune 🎺msgspec what is JSON for anymore?| Implementation | Description |
|---|---|
| native slots | Bare-bones slotted class with Final-annotated fields, the closest thing to a naive native record. |
| manual record | A slotted class with common capabilities based on record-type |
| NamedTuple | From the standard library’s typing module |
| dataclass | From the standard library’s dataclasses module |
| frozen dataclass | Standard library @dataclass(frozen=true) |
| record-type | A proof-of-concept record type for Python |
| record-type (C) | A C extension based on record-type and msgspec |
| attrs | The venerable Python library for class generation |
| msgspec | A fast serialization and validation library |
Each of these implementations will be evaluated with and without mypyc compilation, and as a cold start (no bytecode cache) and warm start (bytecode cache present), when relevant. All of the implementations are tested on a struct type of three ints:
struct StructUnderTest {
a: int
b: int
c: int
}
Refer to the methodology section for details on how the benchmarks were run.
#Module CostWhen you import your base type or decorator, you also must pay a one time cost, regardless of how many types you define, for that module’s source tree. The cold import is roughly 6-8× a warm one, because the whole transitive source tree has to be recompiled to bytecode.
One-time dependency import, cold vs warm. The native slots record imports no library at all (a slotted class is pure builtins, and Final is erased at runtime), so it pays nothing. The standout is record-type (C): a compiled .so loads in ~0.2 ms — ~95× lighter than msgspec — and is the one dependency with no cold penalty at all (cold == warm), because a .so has no Python source to recompile. #Type CostSo, how much does it cost to define a type? Remember that this cost is paid once on every program start, or at least when it is first imported.
Per-type definition cost: cold (recompile to bytecode), warm (cached bytecode), and mypyc-compiled.Many of these benefit greatly from a warm start, which is the most common use of a Python program. Cold start is included because it’s the first impression that a user gets: “how fast to --help?”
Looking just at the warm start, we can start to see 3 performance tiers:
native slots, record-type (C), msgspec, and manual recordNamedTuple, record-typedataclass, dataclass(frozen=True), attrsThe tiers come down to how many methods each implementation has to generate when the type is defined.
Use the table below to sort relative performance.
| implementation | |||
|---|---|---|---|
| 0.10× | 0.60× | 0.11× | |
| 0.11× | 0.35× | — | |
| 0.13× | 0.42× | — | |
| 0.15× | 2.1× | 0.18× | |
| 1.00× | 1.00× | 1.00× | |
| 1.3× | 1.2× | — | |
| 3.0× | 2.5× | 3.0× | |
| 4.0× | 3.2× | — | |
| 4.9× | 3.8× | 5.2× |
Per-type cost — each cell is × the baseline row (NamedTuple by default; click any row to re-base). Lower is faster to define. Click a column header to sort.
#So what’s the fastest startup?Total startup (tstartupt_{\text{startup}}) is calculated as the fixed dependency import (the module cost, mmodulem_{\text{module}}) plus the number of types (NN) × the per-type definition cost (ctypec_{\text{type}}).
tstartup=mmodule+N⋅ctypet_{\text{startup}} = m_{\text{module}} + N \cdot c_{\text{type}}
The interactive chart below shows the startup time on the Y axis and the number of types defined on the X axis. The scales can be toggled together between log (Y log10, X log2 from 1 to 4,096 types) and linear (Y clipped to 0ms–1,000ms, X from 0 to 4,096 types). For each implementation, the solid line is the warm time, and the dotted line is the cold time. Click on a name in the legend to toggle, double click to isolate, and double click on a disabled name to reset.
solid line = warm (cached bytecode), dotted = cold (recompile) #ConclusionFor my purposes, I can draw a few conclusions from this.
NamedTuple (my goto) is sorta in the middle and is probably not dragging start times too much. But, it’s per-type cost is ~8× the native/C implementations, so as the program grows, it will start to add up.msgspec is faster than NamedTuple above ~256 (warm) type definitions. But this assumes absolute dependency discipline that negates some of the upsides of Python’s ecosystem. If you import msgspec, or dataclass, anywhere, or if any of your dependencies have a high module or type cost, then NamedTuple’s low module cost is dwarfed and you may as well have started with a cheaper struct implementation.dataclass, record-type, and attrs) all have a high type cost, but with that comes (evil-runtime-) metaprogamming capabilities.msgspec in the future. I wasn’t familiar with it before working on this report, but it’s very exciting to see these numbers, not to mention that it has de/serialization on top of being a basic struct. I’d love to see CDDL/CBOR 🔥 and postcard ✉️ de/serializers!Here lives more stuff that wasn’t directly relevant to my goal of assessing startup time, but is still fun.
#Instance CostWhat can I say, since the benchmark suite was setup, I couldn’t resist. The instance costs are relevant to the program speed once it’s begun, and you’ll see that they are quite a bit tighter than the module and type cost comparisons. There’s a total spread of under 4x, from ~60ns up to ~220ns per instance.
#Construction Per-instance construction time, interpreted vs mypyc-compiled.| implementation | ||
|---|---|---|
| 0.44× | — | |
| 0.45× | — | |
| 0.63× | 0.53× | |
| 0.63× | 0.77× | |
| 1.00× | 1.00× | |
| 1.5× | — | |
| 1.6× | 0.55× | |
| 1.6× | 1.6× | |
| 1.6× | — |
Per-instance construction cost — each cell is × the baseline row (NamedTuple by default; click any row to re-base). Lower is faster. Click a column header to sort.
#MemoryMemory is driven by object layout. Freezing a type never changes its footprint —
frozen=True only changes the write path, not the storage. mypyc trades a few
bytes per instance (one pointer to its method table, akin to a C++ vtable) for
speed,2 and gives every compiled class a fixed layout even without __slots__.3
Immutability sometimes costs time or space and is never more efficient.
Memory: freezing never changes the footprint — frozen=True is a write-path flag, not a layout change. Import / type-construction: freezing adds ~60% for dataclass and ~15% for attrs (extra generated methods), and is essentially free for msgspec. Construction: freezing adds a write-path cost for dataclass and attrs (each field through object.__setattr__), and is essentially free for msgspec. #native slots
A plain slotted class with Final fields.
from typing import Final
class NativeFinal:
__slots__ = ("a", "b", "c")
def __init__(self, a: int, b: int, c: int) -> None:
self.a: Final = a
self.b: Final = b
self.c: Final = c
The Final is for the static checker, meaning that it has zero runtime cost.4 mypy rejects o.a = 99, but the assignment succeeds anyway, on the
interpreted class and the compiled .so. So this is the closest thing to a
native record mypyc can produce — a compact slotted object (64 bytes; 72
compiled) whose __init__ it lowers to C-level slot stores, but it is not
actually immutable at runtime (zero cost abstraction).
manual record
native slots is cheap precisely because it
does less. It has no __eq__, __hash__, or __repr__, and — as we saw — it
isn’t even immutable. Every other record here gives you all of that. So here is
Brett Cannon’s record-type pattern: a complete, genuinely-immutable hand-written record with
__slots__, __match_args__, a real __setattr__ guard, and
__eq__/__hash__/__repr__:
class ManualRecord:
__slots__ = ("a", "b", "c")
__match_args__ = ("a", "b", "c")
def __init__(self, a: int, b: int, c: int) -> None:
object.__setattr__(self, "a", a)
object.__setattr__(self, "b", b)
object.__setattr__(self, "c", c)
def __setattr__(self, _attr, _val):
raise TypeError("immutable")
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
return self.a == other.a and self.b == other.b and self.c == other.c
def __hash__(self):
return hash((self.a, self.b, self.c))
#record-type (C)
The manual record marks the pure-Python performance ceiling: complete and immutable, with
near-zero import, but either slow to construct (222 ns) or — once mypyc lowers its
object.__setattr__ init — fast (78 ns) yet larger (96 bytes). msgspec.Struct
shows C clears that ceiling: compact (64 bytes), immutable, ~62 ns construction,
~10 µs/type. Its one catch is the module cost. import msgspec runs
~19 ms, because it’s a serialization library and you can’t get just the struct without importing the whole kitchen sink.5
Can you get msgspec’s record qualities without its import tax? A research
prototype (read: LLM slop) on a branch of Brett Cannon’s record-type
answers yes. It’s a ~600-(slop)-line C extension: an inheritable Record base you
subclass (subtype) exactly like NamedTuple:
from native_record import Record
class Point(Record):
a: int
b: int
c: int
A C metaclass reads the class-body annotations directly (no inspect, no
exec) and builds a frozen, slotted type whose constructor is a C-level
vectorcall, borrowing msgspec’s type-creation trick, with none of its codec machinery.
And you saw in the charts above that it wins in every category.
It’s a research prototype, not a release. It lives on a
PR branch, not PyPI. And there’s one real semantic limit: a class body can’t
express Python’s full parameter grammar (positional-only, keyword-only, *args,
**kwargs) the way @record’s function signature can — fine for the
record-shaped common case, but not literally 1:1 with the decorator. (Per-type
here is measured exactly like every other construct — module self-time ÷ K, which
includes the ~7 µs the bare class statement costs regardless — so it is directly
comparable to the figures above.)
native slots, record-type (C), msgspec, and manual recordNamedTuple, record-typedataclass, dataclass(frozen=True), attrsThe single best predictor turned out to
be how many methods each construct has to generate at class-creation: zero,
one, or several. (Trace it yourself with
codegen_probe.py,
which captures every exec / eval / compile a single definition triggers.)
native slots and manual record are
hand-written, so their methods compile once into the .pyc and the class
statement only has to build the type. msgspec and record-type (C) generate no
Python either. A C metaclass assembles the type directly.
collections.namedtuple
builds a tuple subclass — a descriptor per field and a single eval’d __new__:
lambda _cls, a, b, c: _tuple_new(_cls, (a, b, c))
with typing.NamedTuple
adding PEP 649 annotation handling on top.
record-type’s @record
takes the other road — inspect.signature to read the fields, then one exec’d
class whose only generated logic is the __init__ (__eq__ / __hash__ /
__repr__ come from a Record base):
class C(Record):
__slots__ = ('a', 'b', 'c')
def __init__(self, /, a, b, c) -> None:
object.__setattr__(self, 'a', a)
object.__setattr__(self, 'b', b)
object.__setattr__(self, 'c', c)
A metaclass-plus-factory and a decorator-plus-inspect: different machinery, the
same one-method’s-worth of work, the same tier.
dataclass
turns the annotations into Field objects and generates __init__, __repr__,
and __eq__ in one shot (a factory that returns the three):
def __create_fn__(
__dataclass_type_a__,
__dataclass_type_b__,
__dataclass_type_c__,
__dataclass_HAS_DEFAULT_FACTORY__,
__dataclass_builtins_object__,
__dataclass___init___return_type__,
__dataclasses_recursive_repr
):
def __init__(
self,
a:__dataclass_type_a__,
b:__dataclass_type_b__,
c:__dataclass_type_c__
) -> __dataclass___init___return_type__:
self.a=a
self.b=b
self.c=c
@__dataclasses_recursive_repr()
def __repr__(self):
return f"{self.__class__.__qualname__}(a={self.a!r}, b={self.b!r}, c={self.c!r})"
def __eq__(self,other):
if self is other:
return True
if other.__class__ is self.__class__:
return self.a==other.a and self.b==other.b and self.c==other.c
return NotImplemented
return (__init__,__repr__,__eq__,)
frozen=True adds three more: __setattr__, __delattr__, __hash__ — and
slots=True creates the class a second time,
since slots can’t be added in place.
attrs
is a more layered version of the same idea.
I was really hoping that mypyc was going to compile NamedTuple to a native struct. Compiling the module changes almost nothing about the NamedTuple, while it transforms native slots:
| metric | NamedTuple interpreted | NamedTuple mypyc | native slots interpreted | native slots mypyc |
|---|---|---|---|---|
isinstance(_, tuple) | yes | yes | no | no |
| bytes / instance | 88 | 88 | 64 | 72 |
__new__ (type) instructions | 7 bytecodes | 7 bytecodes | C | C |
__init__ (instance) instructions | C | C | 9 bytecodes | C |
| instance (ns) | 138 | 142 | 87.5 | 75.7 |
The NamedTuple columns are identical: same footprint, same construct time. Its __new__ is still seven interpreted bytecodes inside the compiled extension module, building a tuple and handing it to tuple.__new__:
1 RESUME 0
LOAD_GLOBAL 1 (_tuple_new + NULL)
LOAD_FAST_BORROW_LOAD_FAST_BORROW 1 (_cls, a)
LOAD_FAST_BORROW_LOAD_FAST_BORROW 35 (b, c)
BUILD_TUPLE 3
CALL 2
RETURN_VALUE
Contrast the native record. It has no __new__ at all; its __init__ writes
the three fields straight into their slots with STORE_ATTR — no tuple, no
length field, no boxed item array. (The Final annotations add zero bytecode;
they’re a pure type-checker hint, so this is byte-for-byte a plain slotted
class.)
11 RESUME 0
12 LOAD_FAST_BORROW_LOAD_FAST_BORROW 16 (a, self)
STORE_ATTR 0 (a)
13 LOAD_FAST_BORROW_LOAD_FAST_BORROW 32 (b, self)
STORE_ATTR 1 (b)
14 LOAD_FAST_BORROW_LOAD_FAST_BORROW 48 (c, self)
STORE_ATTR 2 (c)
LOAD_CONST 0 (None)
RETURN_VALUE
mypyc does lower this __init__ to C — recall its 9 bytecodes became C-level in
the compiled column. But for this record you barely see it in the construction
numbers (87 → 76 ns, within run-to-run noise): the __init__ is only three STORE_ATTRs, and the interpreted timeit loop crosses the
interpreter↔native boundary on every call, which caps any gain. Where compiling a
hand-written __init__ does pay off is when it does real interpreted work —
manual record routes every
field through object.__setattr__ and drops from 222 to 78 ns once compiled, a
speedup a frozen dataclass can’t get. NamedTuple’s __new__, by contrast, stays
interpreted even when compiled and there’s nothing for mypyc to lower at all without
breaking the tuple contract.
So, I’ve been right to reach for NamedTuple as a cheaper immutable type than dataclass(frozen=True), but I was wrong to think that it was perfectly efficient and compact like a C struct.
struct Point(x: int, y: int) spelling), with the proof-of-concept
record decorator already on PyPI. As proposed it standardizes the boilerplate
— a concise frozen, slotted dataclass — rather than adding a performance
primitive: a decorator’s generated __init__ stays interpreted, so it can’t
push past the pure-Python floor the manual record maps out.i64/i32) — just not yet for user-defined records. Open since 2021 with no
implementation: a direction, not a date, and nothing to benchmark yet.All measurements were taken on a single machine: CPython 3.14.0 (installed and
managed with uv), mypy/mypyc 2.1.0, attrs 26.1.0,
msgspec 0.21.1, and record-type 2023.1.post1, on x86_64 Linux (WSL2) with gcc
13.3. The C-backed record-type (C) is built from the
branch linked above (a
research prototype, not a release). Absolute numbers will differ on your hardware
and Python build; the relative shape is the takeaway. Every struct carries the
same three int fields.
The standard-library constructs (plain classes, slotted, Final-slotted,
NamedTuple, and the dataclass variants) live in one module that is the unit of
compilation: mypyc containers.py produces a containers.*.so. An interpreted
driver imports that module and detects which form it got by testing whether
__file__ ends in .so. This mirrors how mypyc is actually used — you compile
the definitions and call into them from ordinary interpreted code. The attrs,
msgspec, and both record-type classes are defined in the driver itself, not in
the compiled module, so there is no mypyc form to measure — the charts and tables
leave their mypyc column empty rather than copy in the interpreted value.
(record-type (C) is already a compiled C extension, so mypyc has nothing to
add — it is the native form.)
Even inside the compiled .so, the @dataclass decorator and the NamedTuple
metaclass run as interpreted CPython, and the __init__ / __new__ they generate
stay interpreted bytecode: mypyc compiles the module’s own code, not the code those
tools synthesize at runtime.
sys.getsizeof reports one object’s size but doesn’t follow the __dict__
pointer, so it understates classes that carry one.6 The headline figures instead
come from a bulk tracemalloc measurement — allocate 200,000 instances and
subtract a same-length [None] * n list measured the same way, so the list’s own
backing storage cancels and what remains is the instances’ allocation (GC header
included):
import gc, tracemalloc
def mem_per_instance(ctor, args, n=200_000):
gc.collect()
tracemalloc.start()
base = [None] * n
base_cur, _ = tracemalloc.get_traced_memory()
objs = [ctor(*args) for _ in range(n)]
cur, _ = tracemalloc.get_traced_memory()
tracemalloc.stop()
return (cur - base_cur) / n
Treat the per-instance figure as ±one allocator alignment word.
#BytecodeAllocation bytecode is counted with dis.get_instructions on __new__ and
__init__ (unwrapping the staticmethod that wraps a NamedTuple’s __new__),
and disassembled with dis.dis for the listings above. Deallocation has no
Python bytecode to count: teardown is C-level tp_dealloc / tp_free unless a
class defines a Python __del__, which none of these do.7
Construction and attribute access are timed with timeit — the minimum of seven
repeats of 1,000,000 iterations for construction, 5,000,000 for access, reported
as nanoseconds per operation.8 The timeit loop is interpreted, so every iteration
crosses the interpreter↔native boundary. mypyc’s attribute-access and call
speedups land on the compiled→compiled path, so an interpreted loop reaching into
a compiled class won’t see them (and can read slightly slower) — which is why the
compiled instantiation numbers sit on top of the interpreted ones rather than
below.
The obvious approach — timeit on make_dataclass() or namedtuple() —
measures the wrong thing. The dynamic factory forms differ from the @dataclass
and class C(NamedTuple) forms you actually write (the functional NamedTuple(...)
call understates the class-statement form by roughly 3×), and timeit is blind to
both mypyc and the one-time cost of importing the supporting library, since those
happen before the loop starts.
So every import number comes from a fresh interpreter under
python -X importtime, reading the self time attributed to the module — self
time excludes child imports, so the supporting library isn’t double-counted:
importtime_sweep.py reports). Dividing by K folds a small
fixed per-module overhead into each figure. What that per-type cost consists
of — the methods each construct generates at class-creation — is dissected in
Why three type-cost tiers.__pycache__/*.pyc already written;
“cold” deletes __pycache__ first, so the source is recompiled to bytecode
in-process. Their difference is the source→bytecode compile cost (tens of
µs/type — ~25–55 here, scaling with each class’s source size).python -X importtime -c "import LIB" in a fresh
interpreter gives the cumulative cost of first-importing a library. The cold
variant points PYTHONPYCACHEPREFIX at an empty directory so the whole
transitive source tree must recompile.mypyc and the resulting
.so imported under the same harness. A compiled extension has no Python
source to recompile, so there’s no cold/warm gap — yet its per-type creation
cost is barely lower than interpreted, likely because type creation is dominated by
CPython’s PyType_Ready, which runs either way.The startup chart is a model, not a direct measurement: total startup is taken as
a fixed dependency import plus N × the measured per-type construction cost,
evaluated for cold and warm. The crossover is where two such lines meet —
N = (dep_b - dep_a) / (per_type_a - per_type_b). It assumes a single dependency
imported once and a linear per-type cost (both hold well here); the cold curves
roll up shared sub-dependencies, so several of these libraries imported together
cost less than the sum of their individual lines.
Bytes and counts are integers; timing data is quoted to three significant figures.
Import timings vary run to run, so each is reported as the median of five fresh
processes; instantiation is the minimum of seven timeit repeats (the
conventional low-noise estimator). Treat the per-instance nanosecond figures as
±10% — the construct-to-construct shape is what’s robust, not the third digit.
msgspec’s ~19 ms import is library-wide. There is no struct-only import to
isolate — the codec comes with it — so it’s a fair number to report but not a
pure struct-definition cost.5record-type (C) is a research prototype. Its numbers may shift once it’s
hardened and packaged.Everything here is reproducible from the
python-struct-profiling
repository — the data in this post was produced at commit
b2f2eb7.
Two committed harnesses produce every number, and a third dissects the
type-definition mechanism — all on the same machine, all carrying the identical
three-int-field shape:
bench.py — memory (tracemalloc), bytecode (dis), and instantiation
(timeit), run once against the interpreted module and once against the
mypyc-compiled containers.so.importtime_sweep.py — the import / type-creation axis: it generates a module
of K real class-statement / decorator forms per construct, imports it under
python -X importtime in a fresh interpreter, and divides the module self-time
by K. The figures here are --k 200 --runs 5.codegen_probe.py (added at d8acfd5) — the mechanism behind the
three type-cost tiers: it traces the exec /
eval / compile each construct runs at class-creation and counts how many
methods each one generates (zero, one, or several).Every figure above is derived from this one table set (the charts and these tables read the same array, so they cannot disagree):
Table 1 — Import / type-creation cost, µs per class (median of 5 fresh
-X importtime runs, K = 200). mypyc is the compiled .so;
“—” means the construct is off the compiled axis (attrs, msgspec, and both record-types are
defined outside the compiled module; record-type (C) is already a C extension).
| construct | variant | warm | cold | mypyc |
|---|---|---|---|---|
| native slots | mutable | 7.3 | 59.3 | 6.9 |
| native slots | frozen | 7.4 | 62.2 | 6.9 |
| manual record | frozen | 11.5 | 214.5 | 11.1 |
| NamedTuple | frozen | 76.2 | 104.3 | 63.3 |
| dataclass | mutable | 228.4 | 261.0 | 190.3 |
| dataclass | frozen | 373.4 | 401.2 | 328.5 |
| record-type | frozen | 96.4 | 122.4 | — |
| record-type (C) | frozen | 8.6 | 36.0 | — |
| attrs | mutable | 264.6 | 288.7 | — |
| attrs | frozen | 301.4 | 332.2 | — |
| msgspec | mutable | 10.5 | 40.1 | — |
| msgspec | frozen | 10.2 | 44.0 | — |
Table 2 — One-time dependency import, milliseconds cumulative in a fresh interpreter. Paid once per process regardless of how many types you define. The native record imports no library.
| library | warm | cold |
|---|---|---|
| native (none) | 0.0 | 0.0 |
| manual (none) | 0.0 | 0.0 |
| typing | 4.0 | 33.9 |
| dataclasses | 11.5 | 81.9 |
| record-type | 12.5 | 91.3 |
| record-type (C) | 0.2 | 0.2 |
| attrs | 22.2 | 128.5 |
| msgspec | 19.1 | 131.7 |
Table 3 — Per-instance memory, bytes (tracemalloc, GC header included). Freezing never changes the footprint; mypyc adds one 8-byte vtable word to the native classes it compiles.
| construct | variant | interpreted | mypyc |
|---|---|---|---|
| native slots | mutable | 64 | 72 |
| native slots | frozen | 64 | 72 |
| manual record | frozen | 64 | 96 |
| NamedTuple | frozen | 88 | 88 |
| dataclass | mutable | 64 | 72 |
| dataclass | frozen | 64 | 72 |
| record-type | frozen | 64 | — |
| record-type (C) | frozen | 64 | — |
| attrs | mutable | 80 | — |
| attrs | frozen | 80 | — |
| msgspec | mutable | 64 | — |
| msgspec | frozen | 64 | — |
Table 4 — Instantiation, nanoseconds (min of 7 timeit repeats of 1e6 iterations). The timeit loop is interpreted, so a compiled class called from it shows no mypyc speedup — and can read noticeably slower from the per-call interpreter↔native boundary (e.g. mutable dataclass 87.5→109.5). Treat these as ±10%; the construct-to-construct shape is the robust signal, not small interpreted-vs-mypyc deltas.
| construct | variant | interpreted | mypyc |
|---|---|---|---|
| native slots | mutable | 87.3 | 75.2 |
| native slots | frozen | 87.5 | 75.7 |
| manual record | frozen | 222.5 | 78.4 |
| NamedTuple | frozen | 138.3 | 141.5 |
| dataclass | mutable | 87.5 | 109.5 |
| dataclass | frozen | 224.3 | 226.0 |
| record-type | frozen | 227.0 | — |
| record-type (C) | frozen | 61.2 | — |
| attrs | mutable | 88.5 | — |
| attrs | frozen | 209.1 | — |
| msgspec | mutable | 63.0 | — |
| msgspec | frozen | 62.5 | — |
Table 5 — Construction bytecode, instruction counts from dis.
“C” = no Python bytecode (C-level). Freezing is what turns the 9-instruction
__init__ into 25 (every field routed through object.__setattr__);
these counts are unchanged inside the compiled module except the native
__init__, which mypyc lowers to C.
| construct | __new__ | __init__ (mutable) | __init__ (frozen) |
|---|---|---|---|
| native slots | C | 9 | 9 |
| manual record | C | — | 24 |
| NamedTuple | 7 | — | C |
| dataclass | C | 9 | 25 |
| record-type | C | — | 24 |
| record-type (C) | C | — | C |
| attrs | C | 9 | 25 |
| msgspec | C | C | C |
Derived: the NamedTuple ↔ msgspec startup crossover sits at 229 types (warm) and 1,622 types (cold), computed from Tables 1 and 2.
If you have any ideas, please LMK so I can explain it to my family. ↩
“Introduction”. mypyc.readthedocs.io. Retrieved 2026-06-21. “Classes are compiled to C extension classes. They use vtables for fast method calls and attribute access.” ↩
“Native classes”. mypyc.readthedocs.io. Retrieved 2026-06-21. “Only attributes defined within a class definition (or in a base class) can be assigned to (similar to using __slots__).” ↩
“typing.Final”. docs.python.org. Retrieved 2026-06-21. “There is no runtime checking of these properties.” (See also PEP 591.) ↩
src/msgspec/__init__.py. github.com/jcrist/msgspec. Retrieved 2026-06-21. Struct is imported from the compiled ._core extension, and importing the package eagerly runs from . import inspect, json, msgpack, structs, toml, yaml; the codecs in json.py / msgpack.py re-export from that same _core, so there is no struct-only import to isolate. ↩ ↩2
“sys.getsizeof”. docs.python.org. Retrieved 2026-06-21. “Only the memory consumption directly attributed to the object is accounted for, not the memory consumption of objects it refers to.” ↩
“tp_dealloc”. docs.python.org. Retrieved 2026-06-21. “A pointer to the instance destructor function. […] free all memory buffers owned by the instance, and call the type’s tp_free function to free the object itself.” ↩
“timeit”. docs.python.org. Retrieved 2026-06-21. The module “provides a simple way to time small bits of Python code”; the minimum is reported because “the lowest value gives a lower bound for how fast your machine can run the given code snippet; higher values in the result vector are typically not caused by variability in Python’s speed, but by other processes interfering with your timing accuracy. So the min() of the result is probably the only number you should be interested in.” ↩
“Comparing WSL Versions”. learn.microsoft.com. Retrieved 2026-06-21. “WSL 2 is running as a Hyper-V virtual machine.” The Windows host beside it is itself a partition on that same hypervisor — “Hyper-V Architecture”: “The Microsoft hypervisor must have at least one parent, or root, partition, running Windows … [which] has direct access to hardware devices.” ↩
© 2026 by JP Hutchins. Published under a Creative Commons Attribution-NonCommercial 4.0 International (CC BY-NC 4.0) license.
| # | Наименование новости | Тональность | Информативность | Дата публикации |
|---|---|---|---|---|
| 1 | Python⇒Speed: Unit testing your code's performance, part 1: Big-O scaling | 0 | 30 | 10-01-2026 |
| 2 | Параметры Python, которые должен знать питонист | 0 | 6.9 | 04-01-2026 |
| 3 | What Every Python Developer Should Know About the CPython ABI | 0 | 10 | 19-07-2026 |
| 4 | The Hidden Cost of Python Dictionaries | 0 | 8.52 | 29-01-2026 |
| 5 | django-query-doctor: Diagnose Slow Django Queries | 0 | 30 | 02-08-2026 |
| 6 | Free-threaded Python: past, present, and future | 0 | 26.67 | 27-06-2026 |
| 7 | 6× faster binary search: from compiled code to mechanical sympathy | 0 | 8.18 | 13-07-2026 |
| 8 | Explicit Lazy Imports Are Coming to Python 3.15 | 0 | 22.5 | 27-06-2026 |
| 9 | Custom Data Structures in E-Graphs | 0 | 30 | 07-03-2026 |
| 10 | fstache: Fast, Typed, Mustache Renderer | 0 | 10 | 28-07-2026 |