Skip to content

Commit

Permalink
Reduce CPU load
Browse files Browse the repository at this point in the history
`Fiber.yield` sleeps for 0 seconds. When used in a loop, this,
effectively, maxes out CPU if there are no other resumable fibers.
  • Loading branch information
akadusei committed Feb 14, 2024
1 parent 164c471 commit 4b958e0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased] -

### Fixed
- Replace `Fiber.yield` with `sleep 1.microsecond` in loops to reduce CPU load

## [1.0.0] - 2023-05-29

### Added
Expand Down
6 changes: 3 additions & 3 deletions src/pond.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Pond
return unless @done == false

until size == 0
Fiber.yield
sleep 1.microsecond
end

sync do
Expand All @@ -51,7 +51,7 @@ class Pond
end

until @done
Fiber.yield
sleep 1.microsecond
end
end

Expand All @@ -73,7 +73,7 @@ class Pond
spawn do
until @done.nil?
sync { @fibers.reject!(&.dead?) }
Fiber.yield
sleep 1.microsecond
end

sync do
Expand Down

0 comments on commit 4b958e0

Please sign in to comment.