[Groonga-commit] droonga/fluent-plugin-droonga at b3f8e6b [master] Use Integer as hash key instead of Hash

Back to archive index

Kouhei Sutou null+****@clear*****
Sun Mar 23 17:28:45 JST 2014


Kouhei Sutou	2014-03-23 17:28:45 +0900 (Sun, 23 Mar 2014)

  New Revision: b3f8e6b1359c6eccc972e0f4e47d0c9c563a7d41
  https://github.com/droonga/fluent-plugin-droonga/commit/b3f8e6b1359c6eccc972e0f4e47d0c9c563a7d41

  Message:
    Use Integer as hash key instead of Hash
    
    Because Integer#hash is faster than Hash#hash.
    
        % ruby2.0 -v /tmp/hash-benchmark.rb
        ruby 2.0.0p384 (2014-01-12) [x86_64-linux-gnu]
        Rehearsal -----------------------------------------------------------
        Integer#hash              0.000000   0.000000   0.000000 (  0.000012)
        Hash#hash (1 element)     0.000000   0.000000   0.000000 (  0.000060)
        Hash#hash (10 elements)   0.000000   0.000000   0.000000 (  0.000274)
        -------------------------------------------------- total: 0.000000sec
    
                                      user     system      total        real
        Integer#hash              0.000000   0.000000   0.000000 (  0.000016)
        Hash#hash (1 element)     0.000000   0.000000   0.000000 (  0.000122)
        Hash#hash (10 elements)   0.000000   0.000000   0.000000 (  0.000248)
    
    /tmp/hash-benchmark.rb:
    
        require "benchmark"
    
        N = 100
    
        integer = 10
        hash_1 = {"a" => "b"}
        hash_10 = {}
        10.times do |i|
          hash_10[i] = "a"
        end
    
        Benchmark.bmbm do |benchmark|
          benchmark.report("Integer#hash") do
            N.times do
              integer.hash
            end
          end
          benchmark.report("Hash#hash (1 element)") do
            N.times do
              hash_1.hash
            end
          end
          benchmark.report("Hash#hash (10 elements)") do
            N.times do
              hash_10.hash
            end
          end
        end

  Modified files:
    lib/droonga/distributor.rb

  Modified: lib/droonga/distributor.rb (+10 -3)
===================================================================
--- lib/droonga/distributor.rb    2014-03-23 17:15:44 +0900 (888f962)
+++ lib/droonga/distributor.rb    2014-03-23 17:28:45 +0900 (e2f509e)
@@ -47,7 +47,9 @@ module Droonga
       steps = []
       each_strongly_connected_component do |nodes|
         raise CyclicStepsError.new(nodes) if nodes.size > 1
-        steps.concat(nodes) unless nodes.first.is_a? String
+        nodes.each do |node|
+          steps << @step_maps[node] if node.is_a?(Integer)
+        end
       end
       @dispatcher.dispatch_steps(steps)
     end
@@ -55,11 +57,16 @@ module Droonga
     private
     def build_dependencies
       @dependencies = {}
+      @step_maps = {}
+      step_id = 0
       @plan.each do |step|
-        @dependencies[step] = step["inputs"]
+        # Integer#hash (step_id.hash) is very faster than Hash#hash (step.hash).
+        @step_maps[step_id] = step
+        step_id += 1
+        @dependencies[step_id] = step["inputs"]
         next unless step["outputs"]
         step["outputs"].each do |output|
-          @dependencies[output] = [step]
+          @dependencies[output] = [step_id]
         end
       end
     end
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index