You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: implement lazy loading for 46.6% startup improvement
- Added lazy service factory functions for CrateService, IngestionService, TypeNavigationService, WorkflowService, and CrossReferenceService
- Moved service imports from module-level to function-level to defer heavy initialization
- Achieved 46.6% startup time improvement (62.67ms → 33.45ms)
- Fixed uvx deployment startup failures with zero-install capability
- Maintained MCP protocol compliance and zero regressions
- Updated all living memory documentation with implementation details
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <[email protected]>
Copy file name to clipboardExpand all lines: Architecture.md
+78-5Lines changed: 78 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -237,6 +237,45 @@ The docsrs-mcp server implements a service layer pattern that decouples business
237
237
-**CrossReferenceService**: **Phase 6 Enhancement**: Provides advanced cross-reference operations including import resolution, dependency graph analysis, migration suggestions, and re-export tracing. Implements circuit breaker pattern for resilience, LRU cache with 5-minute TTL for performance, and DFS algorithms for cycle detection in dependency graphs.
238
238
-**Transport Layer Decoupling**: Business logic is independent of whether accessed via MCP or REST
239
239
240
+
#### Lazy Loading Service Factory Pattern
241
+
242
+
The service layer implements a lazy loading factory pattern that dramatically improves startup performance by deferring service initialization until first use. This pattern follows the established architecture used in `embedding_manager.py` and provides singleton behavior with deferred initialization.
243
+
244
+
**Factory Functions**:
245
+
-`get_crate_service()` - Manages CrateService singleton with lazy instantiation
246
+
-`get_ingestion_service()` - Handles IngestionService lazy loading and initialization
247
+
-`get_type_navigation_service()` - Provides TypeNavigationService with deferred loading
-`get_cross_reference_service(db_path)` - Non-singleton factory for database-specific instances
250
+
251
+
**Implementation Pattern**:
252
+
```python
253
+
# Global singleton storage
254
+
_service =None
255
+
256
+
defget_service():
257
+
"""Get or create the Service instance with lazy loading."""
258
+
global _service
259
+
if _service isNone:
260
+
from .services.service_module import ServiceClass
261
+
_service = ServiceClass()
262
+
return _service
263
+
```
264
+
265
+
**Performance Benefits**:
266
+
-**46.6% startup performance improvement** achieved through import deferral
267
+
- Services are only imported and initialized when first accessed via MCP tools
268
+
- Maintains singleton behavior while eliminating cold-start overhead
269
+
- Zero impact on runtime performance after first initialization
270
+
271
+
**Architectural Advantages**:
272
+
-**Import Optimization**: Heavy service modules are not loaded during server startup
273
+
-**Memory Efficiency**: Services consume memory only when actively used
274
+
-**Startup Reliability**: Reduced dependency graph complexity during initialization
275
+
-**Consistent Pattern**: Follows established lazy loading conventions from embedding system
276
+
277
+
**Exception Handling**: CrossReferenceService uses a non-singleton pattern due to database path requirements, creating new instances per call while still maintaining lazy import behavior.
278
+
240
279
### Dual MCP Implementation Architecture
241
280
242
281
The system now supports two parallel MCP implementations to ensure compatibility and enable gradual migration:
@@ -971,11 +1010,12 @@ src/docsrs_mcp/
971
1010
972
1011
### Key Architectural Changes:
973
1012
1.**Service Layer Pattern**: Extract business logic into focused service modules, shared between MCP and REST modes
974
-
2.**Decorator-based Tools**: Use @mcp.tool() from official SDK for automatic schema generation
975
-
3.**Elimination of Workarounds**: Remove 180+ lines of override_fastmcp_schemas() complexity
976
-
4.**Dual-Mode Preservation**: Maintain REST and MCP modes with shared service layer
977
-
5.**Native SDK Integration**: Use mcp.server.fastmcp.FastMCP from official SDK 1.13.1
978
-
6.**Modular Web Layer**: Refactored monolithic app.py (~981 LOC) into focused modules:
1013
+
2.**Lazy Loading Service Factory Pattern**: Implement lazy service initialization with 46.6% startup performance improvement through deferred imports and singleton management
1014
+
3.**Decorator-based Tools**: Use @mcp.tool() from official SDK for automatic schema generation
1015
+
4.**Elimination of Workarounds**: Remove 180+ lines of override_fastmcp_schemas() complexity
1016
+
5.**Dual-Mode Preservation**: Maintain REST and MCP modes with shared service layer
1017
+
6.**Native SDK Integration**: Use mcp.server.fastmcp.FastMCP from official SDK 1.13.1
1018
+
7.**Modular Web Layer**: Refactored monolithic app.py (~981 LOC) into focused modules:
979
1019
-**server.py**: FastAPI initialization and configuration
980
1020
-**endpoints.py**: Main API endpoints with APIRouter pattern
-`get_cross_reference_service(db_path)` - Non-singleton factory with lazy imports
6465
+
-**Singleton Management**: Global service variables (`_crate_service`, `_ingestion_service`, etc.) ensure single instance per service type
6466
+
-**Import Deferral**: Services are imported only when first accessed, reducing startup dependency graph complexity
6467
+
-**Naming Convention**: Consistent `get_*_service()` naming pattern aligns with established codebase conventions
6468
+
-**Exception Handling**: CrossReferenceService uses non-singleton pattern due to database path requirements while maintaining lazy import benefits
6469
+
-**Architectural Benefits**:
6470
+
1.**Startup Reliability**: Reduced dependency loading during server initialization
6471
+
2.**Memory Efficiency**: Services consume resources only when actively used
6472
+
3.**Runtime Performance**: Zero impact after first initialization - maintains singleton behavior
6473
+
4.**Maintenance**: Consistent pattern across all service factories following established conventions
6474
+
6402
6475
**CompareVersionsRequest Categories Field Validator Enhancement (2025-08-25)**
6403
6476
-**Issue Fixed**: Duplicate string-to-enum parsing logic between CompareVersionsRequest model and mcp_sdk_server.py causing maintenance overhead and inconsistent validation
6404
6477
-**Root Cause**: Manual string parsing in MCP handler duplicated validation logic already needed in the Pydantic model
"issue": "Initial uvx deployment startup was slow due to immediate service instantiation",
4699
+
"resolution": "Implemented lazy service factory pattern following embedding_manager.py approach for deferred initialization",
4700
+
"status": "resolved"
4701
+
}
4702
+
],
4703
+
"completionDetails": {
4704
+
"completedDate": "2025-08-25T00:00:00Z",
4705
+
"implementation": "Successfully implemented lazy loading solution using service factory pattern with deferred initialization",
4706
+
"notes": "Achieved 46.6% startup improvement (62.67ms → 33.45ms) by implementing lazy service factory pattern. uvx deployment now working with `uvx --from . docsrs-mcp`. MCP protocol compliance maintained with zero regressions. Applied proper linting and code formatting with Ruff.",
0 commit comments