Sha256: 9607d7b0a998ea1e461c5451b049a983b725488b95633df1a48963af78c48890

Contents?: true

Size: 746 Bytes

Versions: 2

Compression:

Stored size: 746 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

2 entries across 2 versions & 1 rubygems

Version Path
speculation-0.3.0 lib/speculation/pmap.rb
speculation-0.2.0 lib/speculation/pmap.rb