-
Notifications
You must be signed in to change notification settings - Fork 0
Closed
Labels
enhancementNew feature or requestNew feature or requestperformancePerformance optimizationPerformance optimization
Description
Overview
Replace the current in-memory graph-based cycle detection with a more scalable, database-native approach using a recursive Common Table Expression (CTE). This avoids loading all dependencies into application memory.
Current Implementation
Location: src/implementations/dependency-repository.ts:154 + src/services/handlers/dependency-handler.ts:80-88
The current approach loads ALL dependencies into memory for cycle detection:
// Build graph from all dependencies (synchronous)
const allDepsRows = this.findAllStmt.all() as Record<string, any>[];
const allDeps = allDepsRows.map(row => this.rowToDependency(row));
graph = new DependencyGraph(allDeps);Problem
- Loads entire dependency graph into memory on every
addDependencycall - Doesn't scale to large graphs (10K+ tasks with complex dependencies)
- O(V + E) memory usage where V = tasks, E = dependencies
Proposed Solution
Use SQLite recursive CTE for cycle detection:
wouldCreateCycleInDB(taskId, dependsOnTaskId) {
const query = `
WITH RECURSIVE dependency_path(task) AS (
SELECT ?
UNION ALL
SELECT T.depends_on_task_id FROM task_dependencies T, dependency_path P
WHERE T.task_id = P.task
)
SELECT 1 FROM dependency_path WHERE task = ?;
`;
const result = this.db.prepare(query).get(dependsOnTaskId, taskId);
return !!result;
}Benefits
- O(1) memory usage - query runs entirely in SQLite
- Better scalability for large graphs
- Leverages database indexes for performance
- Eliminates need for in-memory DependencyGraph during insertion
Trade-offs
- Removes graph caching optimization (but cache is invalidated on every insert anyway)
- Slightly more complex SQL (but standard recursive CTE pattern)
- Requires benchmarking to validate performance improvement
Implementation Plan
- Add
wouldCreateCycleInDB()method toSQLiteDependencyRepository - Replace in-memory cycle detection in
addDependency()transaction - Update tests to verify cycle detection still works correctly
- Benchmark performance vs current implementation
- Remove
DependencyGraphusage fromDependencyRepository(can still be used by handlers for topological sort)
References
- Identified in PR feat: Task Dependencies v0.3.0 - Post-Review Fixes #9 review by qodo-merge-for-open-source
- Related: SQLite Recursive CTEs
Target
v0.3.1 - Performance Optimizations
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or requestperformancePerformance optimizationPerformance optimization