|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# Copyright 2024 - 2026 Block, Inc. |
| 3 | +# |
| 4 | +# Use of this source code is governed by an MIT-style |
| 5 | +# license that can be found in the LICENSE file or at |
| 6 | +# https://opensource.org/licenses/MIT. |
| 7 | +# |
| 8 | +# frozen_string_literal: true |
| 9 | + |
| 10 | +# Benchmarks the single-item fast path in ElasticGraph::Support::Threading.parallel_map |
| 11 | +# using local copies of the before/after implementations. |
| 12 | +# |
| 13 | +# Run with: |
| 14 | +# bundle exec ruby benchmarks/threading/parallel_map_single_item.rb |
| 15 | + |
| 16 | +require "benchmark/ips" |
| 17 | + |
| 18 | +module OriginalThreadingImplementation |
| 19 | + def self.parallel_map(items) |
| 20 | + threads = _ = items.map do |item| |
| 21 | + ::Thread.new do |
| 22 | + ::Thread.current.report_on_exception = false |
| 23 | + |
| 24 | + yield item |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + threads.map(&:value) |
| 29 | + rescue => e |
| 30 | + e.set_backtrace(e.backtrace + caller) |
| 31 | + raise e |
| 32 | + end |
| 33 | +end |
| 34 | + |
| 35 | +module UpdatedThreadingImplementation |
| 36 | + def self.parallel_map(items) |
| 37 | + return _ = items.map { |item| yield item } if items.size < 2 |
| 38 | + |
| 39 | + begin |
| 40 | + threads = _ = items.map do |item| |
| 41 | + ::Thread.new do |
| 42 | + ::Thread.current.report_on_exception = false |
| 43 | + |
| 44 | + yield item |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + threads.map(&:value) |
| 49 | + rescue => e |
| 50 | + e.set_backtrace(e.backtrace + caller) |
| 51 | + raise e |
| 52 | + end |
| 53 | + end |
| 54 | +end |
| 55 | + |
| 56 | +SINGLE_ARRAY = ["a"].freeze |
| 57 | +EMPTY_ARRAY = [].freeze |
| 58 | +MULTI_ITEM_ARRAY = %w[a b c d].freeze |
| 59 | +SINGLE_HASH = {"orders" => [1, 2, 3]}.freeze |
| 60 | +EMPTY_HASH = {}.freeze |
| 61 | +MULTI_ENTRY_HASH = { |
| 62 | + "orders" => [1, 2, 3], |
| 63 | + "payments" => [4, 5, 6], |
| 64 | + "refunds" => [7, 8, 9], |
| 65 | + "disputes" => [10, 11, 12] |
| 66 | +}.freeze |
| 67 | + |
| 68 | +def updated_parallel_map(items, &block) |
| 69 | + UpdatedThreadingImplementation.parallel_map(items, &block) |
| 70 | +end |
| 71 | + |
| 72 | +def assert_same_result(label) |
| 73 | + original = yield OriginalThreadingImplementation |
| 74 | + updated = yield UpdatedThreadingImplementation |
| 75 | + |
| 76 | + abort "#{label} produced different results: #{original.inspect} != #{updated.inspect}" unless original == updated |
| 77 | +end |
| 78 | + |
| 79 | +assert_same_result("single array") { |implementation| implementation.parallel_map(SINGLE_ARRAY, &:next) } |
| 80 | +assert_same_result("empty array") { |implementation| implementation.parallel_map(EMPTY_ARRAY, &:next) } |
| 81 | +assert_same_result("multi item array") { |implementation| implementation.parallel_map(MULTI_ITEM_ARRAY, &:next) } |
| 82 | +assert_same_result("single hash") { |implementation| implementation.parallel_map(SINGLE_HASH) { |key, values| [key, values.size] } } |
| 83 | +assert_same_result("empty hash") { |implementation| implementation.parallel_map(EMPTY_HASH) { |key, values| [key, values.size] } } |
| 84 | +assert_same_result("multi entry hash") do |implementation| |
| 85 | + implementation.parallel_map(MULTI_ENTRY_HASH) { |key, values| [key, values.size] } |
| 86 | +end |
| 87 | + |
| 88 | +def run_ips(title) |
| 89 | + puts |
| 90 | + puts "=" * 80 |
| 91 | + puts title |
| 92 | + puts "=" * 80 |
| 93 | + |
| 94 | + Benchmark.ips do |x| |
| 95 | + x.config(time: 5, warmup: 2) |
| 96 | + yield x |
| 97 | + x.compare! |
| 98 | + end |
| 99 | +end |
| 100 | + |
| 101 | +run_ips("single item array") do |x| |
| 102 | + x.report("before: always spawn thread") { OriginalThreadingImplementation.parallel_map(SINGLE_ARRAY, &:next) } |
| 103 | + x.report("after: fast path") { updated_parallel_map(SINGLE_ARRAY, &:next) } |
| 104 | +end |
| 105 | + |
| 106 | +run_ips("single entry hash, like one datastore-client msearch fanout") do |x| |
| 107 | + x.report("before: always spawn thread") do |
| 108 | + OriginalThreadingImplementation.parallel_map(SINGLE_HASH) { |key, values| [key, values.size] } |
| 109 | + end |
| 110 | + |
| 111 | + x.report("after: fast path") do |
| 112 | + updated_parallel_map(SINGLE_HASH) { |key, values| [key, values.size] } |
| 113 | + end |
| 114 | +end |
| 115 | + |
| 116 | +run_ips("empty array") do |x| |
| 117 | + x.report("before: always spawn thread") { OriginalThreadingImplementation.parallel_map(EMPTY_ARRAY, &:next) } |
| 118 | + x.report("after: fast path") { updated_parallel_map(EMPTY_ARRAY, &:next) } |
| 119 | +end |
| 120 | + |
| 121 | +run_ips("empty hash") do |x| |
| 122 | + x.report("before: always spawn thread") do |
| 123 | + OriginalThreadingImplementation.parallel_map(EMPTY_HASH) { |key, values| [key, values.size] } |
| 124 | + end |
| 125 | + |
| 126 | + x.report("after: fast path") do |
| 127 | + updated_parallel_map(EMPTY_HASH) { |key, values| [key, values.size] } |
| 128 | + end |
| 129 | +end |
| 130 | + |
| 131 | +run_ips("multi item array, expected to stay on the threaded path") do |x| |
| 132 | + x.report("before: always spawn thread") { OriginalThreadingImplementation.parallel_map(MULTI_ITEM_ARRAY, &:next) } |
| 133 | + x.report("after: current implementation") { updated_parallel_map(MULTI_ITEM_ARRAY, &:next) } |
| 134 | +end |
| 135 | + |
| 136 | +run_ips("multi entry hash, expected to stay on the threaded path") do |x| |
| 137 | + x.report("before: always spawn thread") do |
| 138 | + OriginalThreadingImplementation.parallel_map(MULTI_ENTRY_HASH) { |key, values| [key, values.size] } |
| 139 | + end |
| 140 | + |
| 141 | + x.report("after: current implementation") do |
| 142 | + updated_parallel_map(MULTI_ENTRY_HASH) { |key, values| [key, values.size] } |
| 143 | + end |
| 144 | +end |
| 145 | + |
| 146 | +module ThreadNewCounter |
| 147 | + def self.count |
| 148 | + @count ||= 0 |
| 149 | + end |
| 150 | + |
| 151 | + def self.count=(count) |
| 152 | + @count = count |
| 153 | + end |
| 154 | + |
| 155 | + def new(...) |
| 156 | + ThreadNewCounter.count += 1 |
| 157 | + super |
| 158 | + end |
| 159 | +end |
| 160 | + |
| 161 | +class << Thread |
| 162 | + prepend ThreadNewCounter |
| 163 | +end |
| 164 | + |
| 165 | +def count_thread_new_calls |
| 166 | + ThreadNewCounter.count = 0 |
| 167 | + yield |
| 168 | + ThreadNewCounter.count |
| 169 | +end |
| 170 | + |
| 171 | +thread_count_iterations = 1_000 |
| 172 | + |
| 173 | +puts |
| 174 | +puts "Thread.new calls for #{thread_count_iterations} map calls" |
| 175 | +puts "-" * 80 |
| 176 | +{ |
| 177 | + "before single array" => -> { OriginalThreadingImplementation.parallel_map(SINGLE_ARRAY, &:next) }, |
| 178 | + "after single array" => -> { updated_parallel_map(SINGLE_ARRAY, &:next) }, |
| 179 | + "before single hash" => -> { OriginalThreadingImplementation.parallel_map(SINGLE_HASH) { |key, values| [key, values.size] } }, |
| 180 | + "after single hash" => -> { updated_parallel_map(SINGLE_HASH) { |key, values| [key, values.size] } }, |
| 181 | + "before empty array" => -> { OriginalThreadingImplementation.parallel_map(EMPTY_ARRAY, &:next) }, |
| 182 | + "after empty array" => -> { updated_parallel_map(EMPTY_ARRAY, &:next) }, |
| 183 | + "before empty hash" => -> { OriginalThreadingImplementation.parallel_map(EMPTY_HASH) { |key, values| [key, values.size] } }, |
| 184 | + "after empty hash" => -> { updated_parallel_map(EMPTY_HASH) { |key, values| [key, values.size] } }, |
| 185 | + "before multi item array" => -> { OriginalThreadingImplementation.parallel_map(MULTI_ITEM_ARRAY, &:next) }, |
| 186 | + "after multi item array" => -> { updated_parallel_map(MULTI_ITEM_ARRAY, &:next) }, |
| 187 | + "before multi entry hash" => -> { OriginalThreadingImplementation.parallel_map(MULTI_ENTRY_HASH) { |key, values| [key, values.size] } }, |
| 188 | + "after multi entry hash" => -> { updated_parallel_map(MULTI_ENTRY_HASH) { |key, values| [key, values.size] } } |
| 189 | +}.each do |label, callable| |
| 190 | + count = count_thread_new_calls do |
| 191 | + thread_count_iterations.times { callable.call } |
| 192 | + end |
| 193 | + |
| 194 | + puts "#{label.ljust(28)} #{count}" |
| 195 | +end |
0 commit comments