Skip to content

Commit df3fa75

Browse files
committed
use Etc.nprocessors for processor count when available
1 parent 4186ffc commit df3fa75

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

CHANGELOG.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## unreleased
44

5+
* Use `Etc.nprocessors` for default number of threads with fallback to manual way [@toy](https://github.com/toy)
56
* Correct environment variable to specify `jpeg-recompress` location [@toy](https://github.com/toy)
67
* Added --benchmark, to compare performance of each tool [#217](https://github.com/toy/image_optim/issues/217) [#218](https://github.com/toy/image_optim/pull/218) [@gurgeous](https://github.com/gurgeous)
78

lib/image_optim/config.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
require 'image_optim/hash_helpers'
66
require 'image_optim/worker'
77
require 'image_optim/cmd'
8+
require 'etc'
89
require 'set'
910
require 'yaml'
1011

1112
class ImageOptim
1213
# Read, merge and parse configuration
13-
class Config
14+
class Config # rubocop:disable Metrics/ClassLength
1415
include OptionHelpers
1516

1617
# Global config path at `$XDG_CONFIG_HOME/image_optim.yml` (by default
@@ -205,9 +206,17 @@ def to_s
205206

206207
private
207208

208-
# http://stackoverflow.com/a/6420817
209209
def processor_count
210-
@processor_count ||= case host_os = RbConfig::CONFIG['host_os']
210+
@processor_count ||= if Etc.respond_to?(:nprocessors)
211+
Etc.nprocessors
212+
else
213+
processor_count_manual
214+
end
215+
end
216+
217+
# http://stackoverflow.com/a/6420817
218+
def processor_count_manual
219+
case host_os = RbConfig::CONFIG['host_os']
211220
when /darwin9/
212221
Cmd.capture 'hwprefs cpu_count'
213222
when /darwin/

0 commit comments

Comments
 (0)