$ show lyft/toasted-marshmallow
Toasted Marshmallow is a JIT compiler for the Marshmallow serialization library that speeds up object dumping and loading by generating type-specialized serialization methods at runtime. It works by creating optimized serializers for instance objects, dictionaries, and hybrid objects (those with __getitem__), then using a proxy to adaptively select the best serializer based on input type patterns. The project includes a forked Marshmallow submodule to provide necessary hooks for JIT integration. Architecture-wise, it's a Python library with core logic in toastedmarshmallow/jit.py, utility modules for code generation and compatibility, and performance benchmarks. Entry points involve setting schema.jit = toastedmarshmallow.Jit on a Marshmallow Schema instance.
The LLM identified 2 files worth highlighting for reuse:
The IndentedString class provides a simple, reusable utility for generating indented strings via a context manager. It has no dependencies beyond the standard library, making it easily extractable for any code generation task. Demonstrates clean implementation of a common pattern in code generators with minimal coupling.
The SerializeProxy class implements an adaptive dispatcher that traces calls to three different serializer functions (for dict, hybrid, and instance objects) and specializes to the most frequently used type after a threshold. This pattern enables just-in-time optimization for polymorphic functions and is completely generic, depending only on the three functions passed to it. Can be extracted and used in any scenario where you want to optimize a function based on input type distribution.