Skip to content

Reset concurrency to the initial default #336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions lib/vips.rb
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,10 @@ def to_s
attach_function :vips_leak_set, [:int], :void
attach_function :vips_vector_set_enabled, [:int], :void
attach_function :vips_concurrency_set, [:int], :void
attach_function :vips_concurrency_get, [], :int

# Track the original default concurrency so we can reset to it.
DEFAULT_CONCURRENCY = vips_concurrency_get
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read the default right away so we can reset to it. (Note Vips.vips_init just above which sets the initial vips__concurrency to VIPS_CONCURRENCY or CPU count.)


# vips_foreign_get_suffixes was added in libvips 8.8
begin
Expand Down Expand Up @@ -663,10 +667,23 @@ def self.cache_set_max_files size
vips_cache_set_max_files size
end

# Set the size of the libvips worker pool. This defaults to the number of
# hardware threads on your computer. Set to 1 to disable threading.
# Get the size of libvips worker pools. Defaults to the VIPS_CONCURRENCY env
# var or the number of hardware threads on your computer.
def self.concurrency
vips_concurrency_get
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For inspection, logging, metrics, and the like.


# Get the default size of libvips worker pools.
def self.concurrency_default
DEFAULT_CONCURRENCY
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useful to expose VIPS_CONCURRENCY or CPU count.


# Set the size of each libvips worker pool. Max 1024 threads. Set to 1 to
# disable threading. Set to 0 or nil to reset to default.
def self.concurrency_set n
n = DEFAULT_CONCURRENCY if n.to_i == 0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assign 0 or nil to restore the initial default.

vips_concurrency_set n
concurrency
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to return the value that was set.

end

# Enable or disable SIMD and the run-time compiler. This can give a nice
Expand Down
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Set default concurrency so we can check against it later. Must be set
# before Vips.init sets concurrency to the default.
DEFAULT_VIPS_CONCURRENCY = 5
ENV["VIPS_CONCURRENCY"] = DEFAULT_VIPS_CONCURRENCY.to_s

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For predictable testing. Set our own VIPS_CONCURRENCY rather than let it default to CPU count.

# Disable stderr output since we purposefully trigger warn-able behavior.
ENV["VIPS_WARNING"] = "1"

require "vips"

require "tempfile"
Expand Down
25 changes: 24 additions & 1 deletion spec/vips_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,31 @@

RSpec.describe Vips do
describe "Vips" do
it "can get default concurrency" do
expect(Vips.concurrency_default).to eq DEFAULT_VIPS_CONCURRENCY
end

it "can get concurrency" do
expect(Vips.concurrency).to eq Vips.concurrency_default
end

it "can set concurrency" do
Vips.concurrency_set 12
expect(Vips.concurrency_set(12)).to eq 12
expect(Vips.concurrency).to eq 12
end

it "clips concurrency to 1024" do
expect(Vips.concurrency_set(1025)).to eq 1024
end

it "can set concurrency to 0 to reset to default" do
Vips.concurrency_set(rand(100))
expect(Vips.concurrency_set(0)).to eq Vips.concurrency_default
end

it "can set concurrency to nil to reset to default" do
Vips.concurrency_set(rand(100))
expect(Vips.concurrency_set(nil)).to eq Vips.concurrency_default
end

it "can set SIMD" do
Expand Down