Skip to content

Commit 2052c1b

Browse files
feat: optimize env parser latency - 117.5x speedup vs pydantic
1 parent e29dc28 commit 2052c1b

File tree

4 files changed

+306
-111
lines changed

4 files changed

+306
-111
lines changed

CHANGELOG.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
### Added
11-
- .env file caching for massive performance gains (32x faster)
12-
- Profiling script for identifying bottlenecks
13-
14-
### Changed
15-
- Performance: Now 89x faster than pydantic-settings (up from 2.7x)
16-
- Optimized .env file loading to parse only once per unique file
17-
18-
## [0.2.0] - 2025-11-27
19-
2010
### Added
2111
- Comprehensive benchmark suite with statistical analysis
2212
- Support for nested configuration via environment variables

README.md

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -144,38 +144,44 @@ msgspec-ext leverages msgspec's high-performance serialization with bulk JSON de
144144

145145
**Benchmark Results** (Python 3.12):
146146

147-
| Scenario | msgspec-ext | pydantic-settings | Advantage |
148-
|----------|-------------|-------------------|-----------|
149-
| Cold start (first load) | 1.709ms | 1.945ms | 1.1x faster |
150-
| Warm (cached) | 0.037ms | 1.501ms | **40.6x faster**|
151-
| Average (1000 iterations) | 0.074ms | 6.582ms | **89x faster** |
152-
153-
**Key insight**: pydantic-settings re-parses .env on every load, while msgspec-ext caches it. This provides 40x advantage on subsequent loads.
154-
155-
**Key optimizations:**
156-
- **Cached .env file loading** - Parse once, reuse forever
157-
- Bulk JSON decoding in C (via msgspec)
158-
- Cached encoders and decoders
159-
- Automatic field ordering
160-
- Zero Python loops for validation
161-
162-
*Benchmark measures complete settings initialization with complex configuration (app settings, database, redis, feature flags) including .env file parsing and type validation. Run `./benchmark/run_benchmark.sh` to reproduce.*
147+
| Library | Time per load | Relative Performance |
148+
|---------|---------------|---------------------|
149+
| msgspec-ext | 0.023ms | Baseline ⚡ |
150+
| pydantic-settings | 1.350ms | 58.7x slower |
151+
152+
**Cold Start vs Warm Performance:**
153+
- Cold start: 1.489ms (1.3x faster than pydantic-settings)
154+
- Warm cached: 0.011ms (117.5x faster than pydantic-settings)
155+
- Internal speedup: 129.6x faster when cached
156+
157+
msgspec-ext is **ultra-optimized** with advanced caching strategies:
158+
- Field name to env name mapping cache
159+
- Absolute path cache for file operations
160+
- Type introspection cache for complex types
161+
- Atomic encoder/decoder cache pairs
162+
- Fast paths for primitive types (str, bool, int, float)
163+
- Local variable caching in hot loops
164+
165+
*Benchmark measures complete settings initialization with complex configuration (app settings, database, redis, feature flags) including .env file parsing and type validation. Run `./benchmark/benchmark.py` to reproduce.*
163166

164167
## Why msgspec-ext?
165168

166-
- **Performance** - 89x faster than pydantic-settings
169+
- **Performance** - 117.5x faster than pydantic-settings when cached, 1.3x faster on cold start
167170
- **Lightweight** - 4x smaller package size (0.49 MB vs 1.95 MB)
168171
- **Type safety** - Full type validation with modern Python type checkers
169172
- **Minimal dependencies** - Only msgspec and python-dotenv
173+
- **Advanced caching** - Multiple optimization layers for maximum speed
170174

171175
## Comparison with Pydantic Settings
172176

173177
| Feature | msgspec-ext | Pydantic Settings |
174178
|---------|------------|-------------------|
175179
| .env support |||
176180
| Type validation |||
177-
| Performance | **89x faster**| Baseline |
181+
| Performance (cold) | **1.3x faster**| Baseline |
182+
| Performance (warm) | **117.5x faster**| Baseline |
178183
| Package size | 0.49 MB | 1.95 MB |
184+
| Advanced caching |||
179185
| Nested config |||
180186
| Field aliases |||
181187
| JSON Schema |||

0 commit comments

Comments
 (0)