Skip to content
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

improve memory usage for releasable concurrency keys #161

Merged
merged 1 commit into from
Mar 1, 2024

Conversation

songjiz
Copy link
Contributor

@songjiz songjiz commented Feb 29, 2024

Detail

Improve memory usage

Additional information

Benchmark script:

require "benchmark/ips"
require "benchmark/memory"

def releasable(concurrency_keys)
  semaphores = SolidQueue::Semaphore.where(key: concurrency_keys).select(:key, :value).index_by(&:key)

  # Concurrency keys without semaphore + concurrency keys with open semaphore
  (concurrency_keys - semaphores.keys) | semaphores.select { |key, semaphore| semaphore.value > 0 }.map(&:first)
end

def refactored_releasable(concurrency_keys)
  semaphores = SolidQueue::Semaphore.where(key: concurrency_keys).pluck(:key, :value).to_h

  # Concurrency keys without semaphore + concurrency keys with open semaphore
  (concurrency_keys - semaphores.keys) | semaphores.select { |_key, value| value > 0 }.keys
end

def concurrency_keys
  ('a'..'z').to_a
end

def seed
  SolidQueue::Semaphore.where(key: concurrency_keys).delete_all
  concurrency_keys.each_with_index do |key, i|
    SolidQueue::Semaphore.create(key: key, value: i, expires_at: 5.minutes.since)
  end
end

Benchmark.memory do |x|
  seed
  x.report("original") do
    releasable(concurrency_keys)
  end
  
  seed
  x.report("refactored") do
    refactored_releasable(concurrency_keys)
  end
end

Benchmark.ips do |x|
  seed
  x.report("original") do
    releasable(concurrency_keys)
  end
  
  seed
  x.report("refactored") do
    refactored_releasable(concurrency_keys)
  end
end

Benchmark result:

Calculating -------------------------------------
            original    44.648k memsize (     2.592k retained)
                       458.000  objects (    16.000  retained)
                        50.000  strings (     2.000  retained)
          refactored    20.896k memsize (     2.128k retained)
                       278.000  objects (    11.000  retained)
                        50.000  strings (     4.000  retained)

Warming up --------------------------------------
            original   105.000 i/100ms
          refactored   301.000 i/100ms
Calculating -------------------------------------
            original      2.137k (±35.4%) i/s -      7.350k in   5.084206s
          refactored    609.056 (±27.3%) i/s -      3.010k in   5.199923s

Copy link
Member

@rosa rosa left a comment

Choose a reason for hiding this comment

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

Nice! Thank you 🙏

@rosa rosa merged commit 041b904 into rails:main Mar 1, 2024
3 checks passed
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.

None yet

2 participants