[PR #916] [CLOSED] perf: Reuse rand for shuffling queues #1922

Closed
opened 2026-03-07 22:13:53 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/hibiken/asynq/pull/916
Author: @l0nax
Created: 9/12/2024
Status: Closed

Base: masterHead: perf-reuse-rand


📝 Commits (1)

  • f4d037f perf: Reuse rand for shuffling queues

📊 Changes

1 file changed (+6 additions, -2 deletions)

View changed files

📝 processor.go (+6 -2)

📄 Description

Instead of creating a new rand.Rand every time we need the next queues, we create it once and reuse it.

The reason for this change comes from monitoring our application: After a few minutes, the github.com/hibiken/asynq.(*processor).queues method will consume a tremendous amount of CPU time and create a lot of garbage.

Since the queue method is called in its own goroutine, there is no need to add any synchronization primitives.

Here is the profiling flame graph of our application in a time window of 5 minutes:
image


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/hibiken/asynq/pull/916 **Author:** [@l0nax](https://github.com/l0nax) **Created:** 9/12/2024 **Status:** ❌ Closed **Base:** `master` ← **Head:** `perf-reuse-rand` --- ### 📝 Commits (1) - [`f4d037f`](https://github.com/hibiken/asynq/commit/f4d037f5f7dee3af6e3d704bea0a6ee9f5686a82) perf: Reuse rand for shuffling queues ### 📊 Changes **1 file changed** (+6 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `processor.go` (+6 -2) </details> ### 📄 Description Instead of creating a new `rand.Rand` every time we need the next queues, we create it once and reuse it. The reason for this change comes from monitoring our application: After a few minutes, the `github.com/hibiken/asynq.(*processor).queues` method will consume a tremendous amount of CPU time and create a lot of garbage. Since the `queue` method is called in its own goroutine, there is no need to add any synchronization primitives. Here is the profiling flame graph of our application in a time window of 5 minutes: ![image](https://github.com/user-attachments/assets/d6b08fdf-f921-446f-a059-0b8caae444c7) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-03-07 22:13:53 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/asynq#1922
No description provided.