Skip to content

Conversation

Copy link

Copilot AI commented Oct 29, 2025

Identified and fixed multiple performance bottlenecks: N+1 queries, inefficient algorithms, and repeated filesystem searches.

Database Query Optimization

GetBackupStats - Eliminated N+1 pattern by combining queries and moving duration calculation to SQL:

// Before: 2 queries + loop with time parsing
rows := db.Query("SELECT started_time, completed_time FROM backups...")
for rows.Next() {
    startTime, _ := common.ParseTime(startStr)
    endTime, _ := common.ParseTime(endStr)
    duration := endTime.Sub(startTime).Minutes()
}

// After: Single query with SQL-based calculation
db.QueryRow(`
    SELECT COUNT(*), SUM(size),
    AVG(CAST((julianday(completed_time) - julianday(started_time)) * 86400 AS REAL))
    FROM backups...
`)

ListByUserID - Replaced correlated subquery with window function:

// Before: Correlated subquery executed per connection
LEFT JOIN backups b ON c.id = b.connection_id
    AND b.completed_time = (SELECT MAX(completed_time) FROM backups WHERE connection_id = c.id)

// After: Window function with single scan
LEFT JOIN (
    SELECT connection_id, completed_time,
    ROW_NUMBER() OVER (PARTITION BY connection_id ORDER BY completed_time DESC) as rn
    FROM backups
) b ON c.id = b.connection_id AND b.rn = 1

Algorithm Optimization

generateDiff - Reduced time complexity from O(n*m) to O(n+m) using hash map for line lookups instead of nested loops.

readBackupFile - Added 10MB size limit and pre-allocated buffer to prevent OOM on large files.

Caching

FindBinaryPath - Added thread-safe cache for binary path lookups to eliminate repeated glob operations and stat calls. Only caches successful lookups to allow for binaries installed later.

executeCronBackup - Uses cron.Entry.Next instead of reparsing schedule string on every execution.

Original prompt

Identify and suggest improvements to slow or inefficient code


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@vercel
Copy link

vercel bot commented Oct 29, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
velld Ready Ready Preview Comment Oct 29, 2025 2:52am

Co-authored-by: dendianugerah <86966660+dendianugerah@users.noreply.github.com>
Co-authored-by: dendianugerah <86966660+dendianugerah@users.noreply.github.com>
…uccessful lookups

Co-authored-by: dendianugerah <86966660+dendianugerah@users.noreply.github.com>
Co-authored-by: dendianugerah <86966660+dendianugerah@users.noreply.github.com>
Copilot AI changed the title [WIP] Identify and suggest improvements for slow code Optimize database queries, diff algorithm, and filesystem operations Oct 29, 2025
Copilot AI requested a review from dendianugerah October 29, 2025 02:52
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.

2 participants