Skip to content

Reduce LARGE CPU consumption by using min-heap instead of sorting all entries every time#423

Open
tovenja wants to merge 7 commits into
robfig:masterfrom
tovenja:master
Open

Reduce LARGE CPU consumption by using min-heap instead of sorting all entries every time#423
tovenja wants to merge 7 commits into
robfig:masterfrom
tovenja:master

Conversation

@tovenja

@tovenja tovenja commented Oct 19, 2021

Copy link
Copy Markdown

You can also use the fork version,https://github.com/tovenja/cron

all test cases PASS

@zeewell

zeewell commented Jan 17, 2022

Copy link
Copy Markdown

Good Job! But, maybe you should keep the module name.

SamPeng87 pushed a commit to SamPeng87/cron that referenced this pull request May 9, 2022
fufuok added a commit to fufuok/cron that referenced this pull request Feb 17, 2023
@janrnc

janrnc commented Sep 25, 2023

Copy link
Copy Markdown

Hi @tovenja, we're working on a fork of this project and we'd like to have your contribution to it.
This Min Heap structure is reasonable, would you be interested in opening a PR there?

CybotTM added a commit to netresearch/go-cron that referenced this pull request Nov 26, 2025
Replace O(n) linear sort with O(log n) min-heap data structure for
job scheduling, significantly improving performance for large numbers
of scheduled jobs.

Changes:
- Add entryHeap type implementing container/heap.Interface
- Add heapIndex field to Entry for efficient heap updates
- Replace sortEntriesByTime with heap operations in scheduler loop
- Use heap.Push for job addition, heap.Fix for updates
- Maintain sorted order in Entries() snapshot for API compatibility
- Add comprehensive tests and benchmarks for heap operations

Performance:
- Insert/remove: O(log n) vs O(n log n)
- Get next job: O(1) vs O(1)
- Update job time: O(log n) vs O(n log n)

Reference: robfig/cron PR robfig#423

Closes #3
CybotTM added a commit to netresearch/go-cron that referenced this pull request Nov 26, 2025
Replace O(n) linear sort with O(log n) min-heap data structure for
job scheduling, significantly improving performance for large numbers
of scheduled jobs.

Changes:
- Add entryHeap type implementing container/heap.Interface
- Add heapIndex field to Entry for efficient heap updates
- Replace sortEntriesByTime with heap operations in scheduler loop
- Use heap.Push for job addition, heap.Fix for updates
- Maintain sorted order in Entries() snapshot for API compatibility
- Add comprehensive tests and benchmarks for heap operations

Performance:
- Insert/remove: O(log n) vs O(n log n)
- Get next job: O(1) vs O(1)
- Update job time: O(log n) vs O(n log n)

Reference: robfig/cron PR robfig#423

Closes #3
@CybotTM

CybotTM commented Dec 4, 2025

Copy link
Copy Markdown

Solution available in maintained fork

This feature has been implemented in netresearch/go-cron, a maintained fork of this library.

Implementation: Replaced O(n log n) sorting with O(log n) min-heap data structure for job scheduling:

Operation Before (sort) After (heap)
Find next O(1) O(1)
Add entry O(n log n) O(log n)
Remove entry O(n) O(log n)
Update entry O(n log n) O(log n)

How to migrate:

// Change import from:
import "github.com/robfig/cron/v3"

// To:
import cron "github.com/netresearch/go-cron"

The API is 100% backward compatible. See the migration guide for details.

Related: netresearch#3, netresearch#35

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.

4 participants