{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreicir5omvdesxevyow2fypl77khce4vq5kn6u7pffaghfja25lsvte",
"uri": "at://did:plc:aqup5zgsczspdqgk5joab42k/app.bsky.feed.post/3motqynxd42c2"
},
"path": "/2026-06-21-python-struct-profiling",
"publishedAt": "2026-06-21T00:00:00.000Z",
"site": "https://www.crumpledpaper.tech",
"tags": [
"Python Cost Model",
"1",
"instance cost",
"analysis",
"match",
"mypyc",
"clap",
"sum type",
"pattern matching",
"exhaustiveness at compile time",
"this YouTube video by No Boilerplate",
"returns",
"Creator Of Swift On Functional Programming (YouTube)",
"Pydantic",
"pretty slow",
"type time",
"Why not…?",
"Structs Under Test (SUTs)",
"Module Cost",
"Type Cost",
"So what’s the fastest startup?",
"Conclusion",
"Appendix",
"Instance Cost",
"Construction",
"Memory",
"The cost of immutability",
"native slots",
"manual record",
"record-type (C)",
"Why three type-cost tiers?",
"NamedTuple in mypyc",
"Further reading",
"Methodology",
"Interpreted vs compiled",
"Memory footprint",
"Bytecode",
"Per-instance timing",
"Import / type-construction time",
"The crossover model",
"Reporting",
"Limitations and cross-validation",
"Reproducing",
"Raw data",
"NamedTuple",
"dataclass",
"frozen dataclass",
"record-type",
"attrs",
"msgspec",
"methodology",
"how many methods each implementation has to generate when the\ntype is defined",
"PEP 810 - Explicit lazy imports",
"module cost",
"CDDL/CBOR 🔥",
"postcard ✉️",
"2",
"3",
"4",
"Brett Cannon’s record-type pattern",
"5",
"branch of Brett Cannon’s record-type",
"codegen_probe.py",
"collections.namedtuple",
"typing.NamedTuple",
"PEP 649",
"@record",
"creates the class a second time",
"Compiling the module",
"record-type proposal",
"mypyc#841",
"uv",
"branch linked above",
"6",
"7",
"timeit",
"8",
"Why three type-cost tiers",
"9",
"python-struct-profiling",
"b2f2eb7",
"d8acfd5",
"three type-cost tiers",
"↩",
"“Introduction”",
"“Native classes”",
"“typing.Final”",
"PEP 591",
"src/msgspec/__init__.py",
"↩2",
"“sys.getsizeof”",
"“tp_dealloc”",
"“timeit”",
"“Comparing WSL Versions”",
"“Hyper-V Architecture”",
"@dataclass",
"@__dataclasses_recursive_repr"
],
"textContent": "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.\n\nSo 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\n\nBut! 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_**.\n\nSo 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\n\n * serialization\n * instantiation\n * attribute access\n * validation\n * memory\n\n\n\nRight, 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.\n\n> But I did include instance cost benchmarks if you’re curious. 😻\n\nIf 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.\n\n## #”how fast to `--help`”\n\nI 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.\n\nUnlike 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.\n\nCan 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_\n\n`__pycache__/*.pyc` and mypyc\n\nPython is not _completely_ “uncompiled”, and we’ll be assessing both cold (no python bytecode cache) and warm (with python bytecode cache) startup times, and look at how mypyc can help with some of the costs.\n\nBut 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.\n\n## #OK, OK, whatever, but why “structs”?\n\nI’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**.\n\nAlgebraic Data Types\n\nsum type is the FP term for a type union. `enum` in Rust, tagged `union` in C, `std::variant` in C++, discriminated unions in TypeScript, are common examples. A plain struct (or record, implying immutability) is a _product type_ in FP.\n\npattern matching is the related control flow aspect. `switch` in C & C++ do not pattern match (though they can guarantee exhaustiveness at compile time), but `match` in Rust, Python, and Haskell do.\n\nIf you’re not feeling motivated, then this YouTube video by No Boilerplate is a great intro, though you’ll have to extrapolate that this is (more or less precariously) applicable to many type systems, not just Rust - but Rust just did it well ❤️🦀 (for a procedural language 😜).\n\nLong 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).\n\nThis 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.\n\n\n from typing import NamedTuple\n\n class DeviceInfoV1(NamedTuple):\n \tserial_number: str\n \tmanufactured_utc_ms: int | None\n\n class DeviceInfoV2(NamedTuple):\n \tserial_number: str\n \tmanufactured_utc_ms: int\n \tbootloader_sha: int\n\n type DeviceInfo = DeviceInfoV1 | DeviceInfoV2\n\n`DeviceInfo` is a _sum type_ of two _product types_ ,`DeviceInfoV1` and`DeviceInfoV2`, 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:\n\n\n class DeviceInfo(NamedTuple):\n \tserial_number: str\n \tmanufactured_utc_ms: int | None\n \tbootloader_sha: int | None\n\nInvalid 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`.\n\nThe sum is smaller than the product\n\nWhere the _sum type_ version can only represent the valid V1 and V2 states, the _product type_ can create an invalid runtime state.\n\nVariant| serial_number| manufactured_utc_ms| bootloader_sha\n---|---|---|---\nv1 or v2| `str`| `int`| `None`\nv1| `str`| `None`| `None`\nv2| `str`| `int`| `int`\nInvalid| `str`| `None`| `int`\n\nUsing a product type instead of a sum type shifts the burden of correctness from the type system to the runtime.\n\nResult Type\n\nYou can do _`Result`-like_ generic types in Python.\n\n\n from typing import NamedTuple\n\n class Ok[V](NamedTuple):\n \tvalue: V\n\n class Err[E](NamedTuple):\n \terror: E\n\n type Result[V, E] = Ok[V] | Err[E]\n\nA `Result` is either an `Ok` with a value of type `V`, or an `Err` with an error of type `E`. You can then pattern match on the result, and because it’s covered by type checkers before runtime, it’s an abstraction that gives you _compile-time correctness_ with _zero runtime cost_.\n\n\n match result:\n \tcase Ok(value):\n \t\tprint(f\"Got a value: {value}\")\n \tcase Err(error):\n \t\tprint(f\"Got an error: {error}\")\n\nWhy not just `type Result[V, E] = V | E`? Because the wrapper is what we are discriminating on, not the content.\n\n_Look at returns before rolling your own FP type system in Python! Or whatever, have fun!_\n\n## #Aw, f&*#!\n\nI promised myself I wouldn’t evangelize FP (Day 583). 💀\n\nIt’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.\n\nBesides, this _can’t_ be about FP, because functional programmers don’t care about performance, memory, or know anything about compilers and instruction sets.\n\n> “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.”\n>\n> Chris Lattner, Creator Of Swift On Functional Programming (YouTube)\n\nOdd 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.\n\n## #WTF are we testing again?\n\nI 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.\n\n### #THE CONTENDERS\n\n*author’s commentary italicized to avoid bias\n\n * manual python slotted class: **“Native Final Slots”** _ewwwwwwwwwwww_\n * manual python slotted class (Brett Cannon’s manual `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 something_\n * from Python’s standard library, `typing` module: **`NamedTuple`** _pewwwwww pew pew pewwwwwwoooo_\n * also from the standard library, `dataclasses`: **`dataclass(frozen=True)`** _boooooooooo metaprogramming suuuuuuuuuuucks…unless it’s rust’s bs…or constexpr…at least it’s not C macros…but booooooooooooooooooo_\n * from legendary Python core developer Br Br Bre Brett Cannon, iiiiiiiit’s **`record-type`** _a new hope_\n * from a 20 minute Claude hallucination that rips off `msgspec` 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!_\n * weighing in at 11 years of development, the original **`attrs`** 🎺 _medieval horns, but in tune_ 🎺\n * fast AF and only ~14.3% vowels iiiiiiiiiiiiiiit’s **`msgspec`** _what is JSON for anymore?_\n\n\n\nNo Pydantic?\n\nPydantic serves a very different purpose than the structs above, and is pretty slow because it’s doing so much, even at type time. attr’s Why not…? page has a good explanation.\n\n## #Table of Contents\n\n * Structs Under Test (SUTs)\n * Module Cost\n * Type Cost\n * So what’s the fastest startup?\n * Conclusion\n * Appendix\n * Instance Cost\n * Construction\n * Memory\n * The cost of immutability\n * native slots\n * manual record\n * record-type (C)\n * Why three type-cost tiers?\n * NamedTuple in mypyc\n * Further reading\n * Methodology\n * Interpreted vs compiled\n * Memory footprint\n * Bytecode\n * Per-instance timing\n * Import / type-construction time\n * The crossover model\n * Reporting\n * Limitations and cross-validation\n * Reproducing\n * Raw data\n\n\n\n## #Structs Under Test (SUTs)\n\nImplementation| Description\n---|---\nnative slots| Bare-bones slotted class with `Final`-annotated fields, the closest thing to a naive native record.\nmanual record| A slotted class with common capabilities based on `record-type`\nNamedTuple| From the standard library’s `typing` module\ndataclass| From the standard library’s `dataclasses` module\nfrozen dataclass| Standard library `@dataclass(frozen=true)`\nrecord-type| A proof-of-concept `record` type for Python\nrecord-type (C)| A C extension based on `record-type` and `msgspec`\nattrs| The venerable Python library for class generation\nmsgspec| A fast serialization and validation library\n\nEach 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:\n\n\n struct StructUnderTest {\n \ta: int\n \tb: int\n \tc: int\n }\n\nRefer to the methodology section for details on how the benchmarks were run.\n\n## #Module Cost\n\nWhen 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.\n\nOne-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.\n\n## #Type Cost\n\nSo, 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**.\n\nPer-type definition cost: cold (recompile to bytecode), warm (cached bytecode), and mypyc-compiled.\n\nMany 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`?”_\n\nLooking just at the warm start, we can start to see 3 performance tiers:\n\n 1. ~7-12 µs: `native slots`, `record-type (C)`, `msgspec`, and `manual record`\n 2. ~76-96 µs (~8× slower): `NamedTuple`, `record-type`\n 3. ~200-370 µs (~20-30× slower): `dataclass`, `dataclass(frozen=True)`, `attrs`\n\n\n\nThe tiers come down to how many methods each implementation has to generate when the\ntype is defined.\n\nUse the table below to sort relative performance.\n\nimplementation | warm | cold | mypyc\n---|---|---|---\nnative slots | 0.10× | 0.60× | 0.11×\nrecord-type (C) | 0.11× | 0.35× | —\nmsgspec | 0.13× | 0.42× | —\nmanual record | 0.15× | 2.1× | 0.18×\nNamedTuple | 1.00× | 1.00× | 1.00×\nrecord-type | 1.3× | 1.2× | —\ndataclass | 3.0× | 2.5× | 3.0×\nattrs | 4.0× | 3.2× | —\nfrozen dataclass | 4.9× | 3.8× | 5.2×\n\nPer-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.\n\nPEP 810 - Explicit lazy imports\n\nI’m very excited for PEP 810 - Explicit lazy imports, coming in Python 3.15 Fall of 2026! This will improve the outlook substantially for idiomatic statically typed libs that want to improve start times by deferring imports until they’re needed.\n\n## #So what’s the fastest startup?\n\nTotal startup (tstartupt_{\\text{startup}}tstartup) is calculated as the fixed dependency import (the module cost, mmodulem_{\\text{module}}mmodule) plus the number of types (NNN) × the per-type definition cost (ctypec_{\\text{type}}ctype).\n\ntstartup=mmodule+N⋅ctypet_{\\text{startup}} = m_{\\text{module}} + N \\cdot c_{\\text{type}}tstartup=mmodule+N⋅ctype\n\nThe 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.\n\nsolid line = warm (cached bytecode), dotted = cold (recompile)\n\n## #Conclusion\n\nFor my purposes, I can draw a few conclusions from this.\n\n 1. `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.\n 2. `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.\n 3. The decorator-based implementations (`dataclass`, `record-type`, and `attrs`) all have a high type cost, but with that comes (evil-runtime-) metaprogamming capabilities.\n 4. The C implementation of record-type is good enough (wins by every metric) that I’ll be rewriting it and getting it under a test suite. **_It may be too good to be true_** - I will update this article once I have a tested implementation!\n 5. I will definitely be trying out `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!\n\n\n\n## #Appendix\n\nHere lives more stuff that wasn’t directly relevant to my goal of assessing startup time, but is still fun.\n\n## #Instance Cost\n\nWhat 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.\n\n### #Construction\n\nPer-instance construction time, interpreted vs mypyc-compiled.\n\nimplementation | interpreted | mypyc\n---|---|---\nrecord-type (C) | 0.44× | —\nmsgspec | 0.45× | —\nnative slots | 0.63× | 0.53×\ndataclass | 0.63× | 0.77×\nNamedTuple | 1.00× | 1.00×\nattrs | 1.5× | —\nmanual record | 1.6× | 0.55×\nfrozen dataclass | 1.6× | 1.6×\nrecord-type | 1.6× | —\n\nPer-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.\n\n### #Memory\n\nMemory 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\n\nBytes per instance, measured with tracemalloc (GC header included). The manual record *is* genuinely immutable — but its custom __setattr__ costs mypyc the compact layout. record-type (C) shows the way out: a hand-built C struct that is immutable *and* compact (64 bytes, like msgspec) — no Python __setattr__ to defeat the layout. attrs, msgspec, and both record-types are defined outside the compiled module, so they have no mypyc bar.\n\n### #The cost of immutability\n\nImmutability sometimes costs time or space and is never more efficient.\n\nMemory: freezing never changes the footprint — frozen=True is a write-path flag, not a layout change.\n\nImport / type-construction: freezing adds ~60% for dataclass and ~15% for attrs (extra generated methods), and is essentially free for msgspec.\n\nConstruction: freezing adds a write-path cost for dataclass and attrs (each field through object.__setattr__), and is essentially free for msgspec.\n\n### #`native slots`\n\nA plain slotted class with `Final` fields.\n\n\n from typing import Final\n\n class NativeFinal:\n \t__slots__ = (\"a\", \"b\", \"c\")\n\n \tdef __init__(self, a: int, b: int, c: int) -> None:\n \t\tself.a: Final = a\n \t\tself.b: Final = b\n \t\tself.c: Final = c\n\nThe `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).\n\n### #`manual record`\n\n`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__`:\n\n\n class ManualRecord:\n \t__slots__ = (\"a\", \"b\", \"c\")\n \t__match_args__ = (\"a\", \"b\", \"c\")\n\n \tdef __init__(self, a: int, b: int, c: int) -> None:\n \t\tobject.__setattr__(self, \"a\", a)\n \t\tobject.__setattr__(self, \"b\", b)\n \t\tobject.__setattr__(self, \"c\", c)\n\n \tdef __setattr__(self, _attr, _val):\n \t\traise TypeError(\"immutable\")\n\n \tdef __eq__(self, other):\n \t\tif not isinstance(other, type(self)):\n \t\t\treturn NotImplemented\n \t\treturn self.a == other.a and self.b == other.b and self.c == other.c\n\n \tdef __hash__(self):\n \t\treturn hash((self.a, self.b, self.c))\n\n### #record-type (C)\n\nThe 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\n\nCan 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`:\n\n\n from native_record import Record\n\n class Point(Record):\n \ta: int\n \tb: int\n \tc: int\n\nA 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.\n\n#### #buuuuuuuuuuuuut…\n\nIt’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.)\n\n### #Why three type-cost tiers?\n\n 1. fastest: `native slots`, `record-type (C)`, `msgspec`, and `manual record`\n 2. ~8× slower: `NamedTuple`, `record-type`\n 3. ~20-30× slower: `dataclass`, `dataclass(frozen=True)`, `attrs`\n\n\n\nThe 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.)\n\n#### #Tier 1 — nothing generated.\n\n`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.\n\n#### #Tier 2 — one generated method.\n\ncollections.namedtuple builds a `tuple` subclass — a descriptor per field and a single `eval`’d `__new__`:\n\n\n lambda _cls, a, b, c: _tuple_new(_cls, (a, b, c))\n\nwith 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):\n\n\n class C(Record):\n \t__slots__ = ('a', 'b', 'c')\n\n \tdef __init__(self, /, a, b, c) -> None:\n \t\tobject.__setattr__(self, 'a', a)\n \t\tobject.__setattr__(self, 'b', b)\n \t\tobject.__setattr__(self, 'c', c)\n\nA metaclass-plus-factory and a decorator-plus-`inspect`: different machinery, the same one-method’s-worth of work, the same tier.\n\n#### #Tier 3 — several generated methods, plus field work and a rebuild.\n\ndataclass turns the annotations into `Field` objects and generates `__init__`, `__repr__`, and `__eq__` in one shot (a factory that returns the three):\n\n\n def __create_fn__(\n \t__dataclass_type_a__,\n \t__dataclass_type_b__,\n \t__dataclass_type_c__,\n \t__dataclass_HAS_DEFAULT_FACTORY__,\n \t__dataclass_builtins_object__,\n \t__dataclass___init___return_type__,\n \t__dataclasses_recursive_repr\n ):\n \tdef __init__(\n \t\tself,\n \t\ta:__dataclass_type_a__,\n \t\tb:__dataclass_type_b__,\n \t\tc:__dataclass_type_c__\n \t) -> __dataclass___init___return_type__:\n \t\tself.a=a\n \t\tself.b=b\n \t\tself.c=c\n \t@__dataclasses_recursive_repr()\n \tdef __repr__(self):\n \t\treturn f\"{self.__class__.__qualname__}(a={self.a!r}, b={self.b!r}, c={self.c!r})\"\n \tdef __eq__(self,other):\n \t\tif self is other:\n \t\t\treturn True\n \t\tif other.__class__ is self.__class__:\n \t\t\treturn self.a==other.a and self.b==other.b and self.c==other.c\n \t\treturn NotImplemented\n \treturn (__init__,__repr__,__eq__,)\n\n`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.\n\n### #NamedTuple in mypyc\n\nI 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`:\n\nmetric| NamedTuple\ninterpreted| NamedTuple\nmypyc| native slots\ninterpreted| native slots\nmypyc\n---|---|---|---|---\n`isinstance(_, tuple)`| yes| yes| no| no\nbytes / instance| 88| 88| 64| 72\n`__new__` (type) instructions| 7 bytecodes| 7 bytecodes| C| C\n`__init__` (instance) instructions| C| C| 9 bytecodes| C\ninstance (ns)| 138| 142| 87.5| 75.7\n\nThe 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__`:\n\n\n 1 RESUME 0\n LOAD_GLOBAL 1 (_tuple_new + NULL)\n LOAD_FAST_BORROW_LOAD_FAST_BORROW 1 (_cls, a)\n LOAD_FAST_BORROW_LOAD_FAST_BORROW 35 (b, c)\n BUILD_TUPLE 3\n CALL 2\n RETURN_VALUE\n\nContrast 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.)\n\n\n 11 RESUME 0\n 12 LOAD_FAST_BORROW_LOAD_FAST_BORROW 16 (a, self)\n STORE_ATTR 0 (a)\n 13 LOAD_FAST_BORROW_LOAD_FAST_BORROW 32 (b, self)\n STORE_ATTR 1 (b)\n 14 LOAD_FAST_BORROW_LOAD_FAST_BORROW 48 (c, self)\n STORE_ATTR 2 (c)\n LOAD_CONST 0 (None)\n RETURN_VALUE\n\nmypyc _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_ATTR`s, 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.\n\nSo, 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.\n\n### #Further reading\n\n * **A first-class record type for Python.** Brett Cannon’s record-type proposal (and a terser `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.\n * **Unboxed value types in mypyc.** mypyc#841 tracks the performance angle these benchmarks can’t reach: user-defined _unboxed_ value types (≈16 bytes vs 40 for a heap object), passed around in native code and boxed only when they enter a Python container. mypyc already does this for native integers (`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.\n\n\n\n## #Methodology\n\nLLM Disclosure\n\nThe benchmark suite and this methodology section were written by an LLM (`claude-opus-4-8`) to the author’s design, and reviewed by the author.\n\nAll 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.\n\n### #Interpreted vs compiled\n\nThe 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.)\n\nEven 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.\n\n### #Memory footprint\n\n`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):\n\n\n import gc, tracemalloc\n\n def mem_per_instance(ctor, args, n=200_000):\n \tgc.collect()\n \ttracemalloc.start()\n \tbase = [None] * n\n \tbase_cur, _ = tracemalloc.get_traced_memory()\n \tobjs = [ctor(*args) for _ in range(n)]\n \tcur, _ = tracemalloc.get_traced_memory()\n \ttracemalloc.stop()\n \treturn (cur - base_cur) / n\n\nTreat the per-instance figure as ±one allocator alignment word.\n\n### #Bytecode\n\nAllocation 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\n\n### #Per-instance timing\n\nConstruction 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.\n\n### #Import / type-construction time\n\nThe 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.\n\nSo 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:\n\n * **Per-type cost.** Generate a module of K = 200 identical-shape classes in the real class-statement form, import it, and read its self-time; the per-type figure is that self-time ÷ 200, the median of five fresh interpreters (this is what the committed `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.\n * **Cold vs warm.** “Warm” imports with the `__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).\n * **Dependency import.** `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.\n * **mypyc axis.** The generated module is compiled with `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.\n\n\n\n### #The crossover model\n\nThe 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.\n\n### #Reporting\n\nBytes 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.\n\n### #Limitations and cross-validation\n\n * **One machine, no isolation.** Everything ran on a single WSL2 host — which sits on Hyper-V, as does the Windows install beside it, so there’s no bare-metal baseline on this box (and no WSL2-specific virtualization penalty to factor out either)9 — with no CPU pinning or frequency-scaling control. Repeating on separate hardware, several Python versions, and a second OS would confirm the shape; pinning the CPU steadies the absolute numbers.\n * **Compiled construction is timed from an interpreted loop.** That measures the common interpreted-caller-into-compiled-class case, not compiled→compiled throughput. A benchmark loop itself compiled with mypyc would show whether its call and attribute speedups close the gap.\n * **“Cold” is a cold _bytecode_ cache, not a cold disk.** The source stays in the OS page cache between runs, so the cold figures isolate source→bytecode compilation, not first-read I/O.\n * **Per-type cost is self-time ÷ K.** That folds a small fixed per-module overhead into each figure; a regression over several values of K would separate the fixed cost from the per-type slope (the correction is sub-microsecond for the cheap constructs).\n * **`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.5\n * **`record-type (C)` is a research prototype.** Its numbers may shift once it’s hardened and packaged.\n * **Five runs is modest.** More repeats, and reporting dispersion alongside the median, would tighten the import figures.\n\n\n\n### #Reproducing\n\nEverything 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:\n\n * `bench.py` — memory (`tracemalloc`), bytecode (`dis`), and instantiation (`timeit`), run once against the interpreted module and once against the `mypyc`-compiled `containers.so`.\n * `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`.\n * `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).\n\n\n\n### #Raw data\n\nEvery figure above is derived from this one table set (the charts and these tables read the same array, so they cannot disagree):\n\n**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).\n\nconstruct| variant| warm| cold| mypyc\n---|---|---|---|---\nnative slots | mutable | 7.3 | 59.3 | 6.9\nnative slots | frozen | 7.4 | 62.2 | 6.9\nmanual record | frozen | 11.5 | 214.5 | 11.1\nNamedTuple | frozen | 76.2 | 104.3 | 63.3\ndataclass | mutable | 228.4 | 261.0 | 190.3\ndataclass | frozen | 373.4 | 401.2 | 328.5\nrecord-type | frozen | 96.4 | 122.4 | —\nrecord-type (C) | frozen | 8.6 | 36.0 | —\nattrs | mutable | 264.6 | 288.7 | —\nattrs | frozen | 301.4 | 332.2 | —\nmsgspec | mutable | 10.5 | 40.1 | —\nmsgspec | frozen | 10.2 | 44.0 | —\n\n**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.\n\nlibrary| warm| cold\n---|---|---\nnative (none) | 0.0 | 0.0\nmanual (none) | 0.0 | 0.0\ntyping | 4.0 | 33.9\ndataclasses | 11.5 | 81.9\nrecord-type | 12.5 | 91.3\nrecord-type (C) | 0.2 | 0.2\nattrs | 22.2 | 128.5\nmsgspec | 19.1 | 131.7\n\n**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.\n\nconstruct| variant| interpreted| mypyc\n---|---|---|---\nnative slots | mutable | 64 | 72\nnative slots | frozen | 64 | 72\nmanual record | frozen | 64 | 96\nNamedTuple | frozen | 88 | 88\ndataclass | mutable | 64 | 72\ndataclass | frozen | 64 | 72\nrecord-type | frozen | 64 | —\nrecord-type (C) | frozen | 64 | —\nattrs | mutable | 80 | —\nattrs | frozen | 80 | —\nmsgspec | mutable | 64 | —\nmsgspec | frozen | 64 | —\n\n**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.\n\nconstruct| variant| interpreted| mypyc\n---|---|---|---\nnative slots | mutable | 87.3 | 75.2\nnative slots | frozen | 87.5 | 75.7\nmanual record | frozen | 222.5 | 78.4\nNamedTuple | frozen | 138.3 | 141.5\ndataclass | mutable | 87.5 | 109.5\ndataclass | frozen | 224.3 | 226.0\nrecord-type | frozen | 227.0 | —\nrecord-type (C) | frozen | 61.2 | —\nattrs | mutable | 88.5 | —\nattrs | frozen | 209.1 | —\nmsgspec | mutable | 63.0 | —\nmsgspec | frozen | 62.5 | —\n\n**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.\n\nconstruct| `__new__`| `__init__` (mutable)| `__init__` (frozen)\n---|---|---|---\nnative slots | C | 9 | 9\nmanual record | C | — | 24\nNamedTuple | 7 | — | C\ndataclass | C | 9 | 25\nrecord-type | C | — | 24\nrecord-type (C) | C | — | C\nattrs | C | 9 | 25\nmsgspec | C | C | C\n\nDerived: the NamedTuple ↔ msgspec startup crossover sits at **229** types (warm) and **1,622** types (cold), computed from Tables 1 and 2.\n\n## #Footnotes\n\n 1. If you have any ideas, please LMK so I can explain it to my family. ↩\n\n 2. “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.” ↩\n\n 3. “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__`).” ↩\n\n 4. “typing.Final”. docs.python.org. Retrieved 2026-06-21. “There is no runtime checking of these properties.” (See also PEP 591.) ↩\n\n 5. 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\n\n 6. “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.” ↩\n\n 7. “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.” ↩\n\n 8. “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.” ↩\n\n 9. “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.” ↩\n\n\n",
"title": "The Fastest Python Struct?",
"updatedAt": "2026-06-22T02:48:09.810Z"
}