Skip to content

Conversation

@vilsonrodrigues
Copy link
Contributor

Summary

Replaces the dependency with a custom-built parser, achieving significant performance improvements while maintaining full compatibility and adding zero external dependencies.

Performance Improvements

Cold start (first load):

  • Before: 1.5x faster than pydantic
  • After: 5.0x faster than pydantic
  • Time: 0.39ms (was ~2ms)

Warm cached (repeated loads):

  • Before: 112x faster than pydantic
  • After: 267x faster than pydantic 🚀
  • Time: 0.012ms (12 microseconds!)

Cache effectiveness:

  • 29.7x internal speedup (cold → warm)

Key Changes

Added

  • src/msgspec_ext/fast_dotenv.py (170 lines)
    • UTF-8 BOM support
    • Escape sequences parsing (\n, \t, etc)
    • Export keyword handling
    • Strict variable name validation
    • Global caching for repeated loads
    • Optimized string operations
  • tests/test_fast_dotenv.py (59 comprehensive tests)
    • File reading scenarios
    • Encoding scenarios
    • Edge cases
    • Quote handling
    • Real-world scenarios
    • Performance stress tests

Changed

  • pyproject.toml: Removed python-dotenv dependency
  • settings.py: Import from fast_dotenv instead of dotenv
  • README.md: Updated performance numbers and features
  • Cleaned up duplicate code in _preprocess_env_value

Removed

  • python-dotenv dependency (was the only external dep besides msgspec)

Testing

✅ All 81 tests passing:

  • 22 existing settings tests (unchanged, 100% compatibility)
  • 59 new fast_dotenv tests (comprehensive coverage)
uv run pytest tests/ -v
# 81 passed in 0.23s

Benchmarks

uv run python benchmark/benchmark_cold_warm.py

Results:

  • Cold start: 0.39ms (5.0x faster than pydantic)
  • Warm cached: 0.012ms (267x faster than pydantic)
  • Cache effectiveness: 29.7x speedup

Code Quality

✅ Lint passing:

uv run ruff check src/
# All checks passed

✅ Code style:

  • English comments
  • Double quotes throughout
  • Type hints with str | None
  • No noqa needed (clean code)

Migration Impact

Breaking changes: None ✅

This is a drop-in replacement with identical API:

  • Same function signatures
  • Same behavior
  • Same edge cases handled
  • Backward compatible

Dependencies:

  • Before: msgspec + python-dotenv
  • After: msgspec only (1 dependency total)

Benefits

  1. 5x faster cold start - Better for CLI tools and serverless
  2. 267x faster warm loads - Better for long-running apps
  3. Zero dependencies - Smaller package, fewer security concerns
  4. Full control - Can optimize further as needed
  5. Better caching - 29.7x vs pydantic's 0.8x

🚀 Generated with Claude Code

Co-Authored-By: Claude noreply@anthropic.com

vilsonrodrigues and others added 7 commits November 27, 2025 02:18
- Complete project structure overview
- Common commands and workflows
- Release process (always use ./scripts/release.sh)
- Architecture details and optimizations
- Linting, testing, and CI/CD guides
- Troubleshooting tips

This file provides context for Claude Code to work more effectively
with the project without repeating instructions.
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
…start

Replace python-dotenv dependency with custom-built fast_dotenv.py parser,
achieving significant performance improvements while maintaining full compatibility.

Performance improvements:
- Cold start: 0.39ms (5.0x faster than pydantic, was 1.5x)
- Warm cached: 0.012ms (267x faster than pydantic, was 112x)
- Cache effectiveness: 29.7x internal speedup

Key changes:
- Add fast_dotenv.py (170 lines, zero dependencies)
- Remove python-dotenv from dependencies
- Update settings.py to use fast_dotenv
- Add 59 comprehensive tests for fast_dotenv
- Clean up duplicate code in settings.py
- Update README with new performance numbers

Technical details:
- UTF-8 BOM support
- Escape sequences parsing (\n, \t, etc)
- Export keyword handling
- Strict variable name validation (isidentifier)
- Global caching for repeated loads
- Optimized string operations with local references

All 81 tests passing (22 settings + 59 fast_dotenv)
Zero external dependencies added (removed 1)
Change chmod from 0o644 (world-readable) to 0o600 (owner-only)
in test_file_permission_error cleanup to address CodeQL security warning.

The permission test still passes with owner-only permissions.
- Fix import sorting and organization
- Replace single quotes with double quotes for consistency
- Remove trailing whitespace from blank lines
- Fix quote escaping in test strings
- Format long lines and dictionaries for better readability
- Apply consistent formatting across test files

All ruff checks now pass successfully.
@vilsonrodrigues
Copy link
Contributor Author

/merge

@github-actions github-actions bot merged commit 22a20e1 into msgflux:main Dec 3, 2025
10 checks passed
@github-actions
Copy link

github-actions bot commented Dec 3, 2025

✅ PR merged successfully by @vilsonrodrigues!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant