Sha256: 436e74cb20eebd5878064b130017a20f20adf0afc8083fc7a5675ffcf18350fb

Contents?: true

Size: 747 Bytes

Versions: 3

Compression:

Stored size: 747 Bytes

Contents

# frozen_string_literal: true

require "concurrent"

module Speculation
  # @private
  module Pmap
    if RUBY_PLATFORM == "java"
      def pmap(coll, &block)
        Pmap.pmap_jruby(coll, &block)
      end
    else
      def pmap(coll, &block)
        coll.map(&block)
      end
    end

    def self.pmap_jruby(coll, &block)
      thread_count = [1, Concurrent.processor_count - 1].max
      pool = Concurrent::FixedThreadPool.new(thread_count, :auto_terminate  => true,
                                                           :fallback_policy => :abort)

      coll.
        map { |x| Concurrent::Future.execute(:executor => pool) { block.call(x) } }.
        map { |f| f.value || f.reason }
    ensure
      pool.shutdown
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
speculation-0.4.2 lib/speculation/pmap.rb
speculation-0.4.0 lib/speculation/pmap.rb
speculation-0.3.1 lib/speculation/pmap.rb