Skip to content

Optimize queue performance: eliminate defer accumulation, improve sorting complexity, cache keys#63

Merged
ren0503 merged 3 commits intomasterfrom
copilot/improve-slow-code-performance
Jan 8, 2026
Merged

Optimize queue performance: eliminate defer accumulation, improve sorting complexity, cache keys#63
ren0503 merged 3 commits intomasterfrom
copilot/improve-slow-code-performance

Conversation

Copy link
Contributor

Copilot AI commented Jan 8, 2026

Multiple performance bottlenecks identified in queue processing: defer accumulation in loops, O(n log n) sorts on every job addition, repeated string operations for key generation, and a race condition in concurrent slice access.

Changes

Memory and Concurrency

  • Moved context cancellation outside loops in Run() and Retry() - defer cancel() inside loops accumulates deferred functions indefinitely
  • Added mutex protection for finishedJob slice accessed by concurrent goroutines in Retry()

Sorting Optimization

  • AddJob: O(n log n) → O(log n) - binary search for insertion point instead of full sort
    // Before: sort.SliceStable(q.jobs, ...) on every add
    // After: sort.Search() to find position, insert directly
    insertIdx := sort.Search(len(q.jobs), func(i int) bool {
        return q.jobs[i].Priority < job.Priority
    })
  • BulkAddJob: O(n log n) → O(n+m) - merge sorted slices instead of re-sorting everything
    // Sort new jobs once, merge with existing sorted slice
    q.jobs = mergeSortedJobs(q.jobs, newJobs)

Key Caching

  • Pre-compute queue key in constructor - eliminates repeated strings.ToLower() and concatenation on every access
    type Queue struct {
        // ...
        cachedKey string // computed once at initialization
    }

Impact

High-frequency operations (job additions, key lookups) are now logarithmic or constant time instead of linearithmic. Eliminates memory accumulation issue in long-running queues.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • vuln.go.dev
    • Triggering command: /tmp/go-build2296908605/b001/exe/govulncheck /tmp/go-build2296908605/b001/exe/govulncheck ./... (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

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.

@coderabbitai
Copy link

coderabbitai bot commented Jan 8, 2026

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI and others added 2 commits January 8, 2026 15:26
…d key caching

Co-authored-by: Ren0503 <54244073+Ren0503@users.noreply.github.com>
…ll consistency

Co-authored-by: Ren0503 <54244073+Ren0503@users.noreply.github.com>
Copilot AI changed the title [WIP] Identify and suggest improvements for slow code Optimize queue performance: eliminate defer accumulation, improve sorting complexity, cache keys Jan 8, 2026
Copilot AI requested a review from ren0503 January 8, 2026 15:30
@ren0503 ren0503 marked this pull request as ready for review January 8, 2026 15:32
@ren0503 ren0503 merged commit 3829156 into master Jan 8, 2026
2 checks passed
@ren0503 ren0503 added this to the Queue v2.1.2 milestone Jan 18, 2026
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