~michalr/blog2gmi

e4ad857cc5b885c7e732a363038a481b88e9a7cd — MichaƂ Rudowicz 4 years ago 639650a
Moved map_parallel to a separate shard
5 files changed, 7 insertions(+), 33 deletions(-)

M shard.lock
M shard.yml
D spec/map_parallel_spec.cr
M src/main.cr
D src/map_parallel.cr
M shard.lock => shard.lock +4 -0
@@ 1,5 1,9 @@
version: 2.0
shards:
  map_parallel:
    git: https://git.sr.ht/~michalr/map_parallel
    version: 0.0.1+git.commit.b8db351b7066ed039d7a1a906fa814595dedf46c

  markd:
    git: https://github.com/icyleaf/markd.git
    version: 0.2.1

M shard.yml => shard.yml +2 -0
@@ 10,6 10,8 @@ description: |
dependencies:
  markd:
    github: icyleaf/markd
  map_parallel:
    git: https://git.sr.ht/~michalr/map_parallel

license: GPLv3


D spec/map_parallel_spec.cr => spec/map_parallel_spec.cr +0 -21
@@ 1,21 0,0 @@
require "benchmark"

require "./spec_helper"
require "../src/map_parallel"

describe "MapParallel" do
  it "has the same output effect as simple map" do
    input = (0..5000)

    input.map_parallel { |x| x + 1 }.should eq(input.map { |x| x + 1 })
  end

  it "should be faster than running all blocks serially" do
    ts = Time::Span.new(nanoseconds: 10_000)
    executions = 5000
    input = (0..executions)

    Benchmark.realtime { input.map_parallel { |x| sleep(ts) } }
      .should be < (ts*executions*0.5)
  end
end

M src/main.cr => src/main.cr +1 -2
@@ 1,10 1,9 @@
require "markd"
require "./gmi_renderer"
require "./map_parallel"
require "map_parallel"

Dir.new(".").entries
  .select! { |x| /\.md$/ =~ x }
  .tap { |x| files_to_process = x.size }
  .map_parallel do |fname|
    {fname, GmiRenderer.new.render(
      Markd::Parser.parse(

D src/map_parallel.cr => src/map_parallel.cr +0 -10
@@ 1,10 0,0 @@
module Enumerable(T)
  def map_parallel(&block : T -> U) forall U
    output_ch = Channel(U).new
    self.each do |x|
      spawn { output_ch.send(block.call(x)) }
    end
    Array.new(self.size, nil)
      .map { |x| output_ch.receive }
  end
end