Skip to content

Instantly share code, notes, and snippets.

@technicalpickles
Last active September 11, 2023 21:25
Show Gist options
  • Save technicalpickles/91933d34dcd47bfc99d6ac0e53fcdb1e to your computer and use it in GitHub Desktop.
Save technicalpickles/91933d34dcd47bfc99d6ac0e53fcdb1e to your computer and use it in GitHub Desktop.

Revisions

  1. technicalpickles revised this gist Aug 30, 2023. 2 changed files with 57 additions and 41 deletions.
    19 changes: 14 additions & 5 deletions benchmark.rb
    Original file line number Diff line number Diff line change
    @@ -4,11 +4,10 @@
    require 'bundler'
    class Bundler::Settings
    def original(name)
    key = key_for(name)
    config_with_key = configs.values.detect {|config| config[key] }
    value = configs.values.map {|config| config[key] }.compact.first
    key = key_for(name)
    value = configs.values.map {|config| config[key] }.compact.first

    converted_value(value, name)
    converted_value(value, name)
    end

    def bang_method(name)
    @@ -96,6 +95,13 @@ def correct_manual_with_each_config(name)
    converted_value(value, name)
    end

    def with_flat_map(name)
    key = key_for(name)
    value = configs.flat_map {|_, config| break config[key] unless config[key].nil? }

    converted_value(value, name)
    end

    private

    def each_config
    @@ -116,10 +122,11 @@ def memoized_configs
    :default => DEFAULT_CONFIG,
    }.freeze
    end

    end

    # warm up memoization
    Bundler.settings.manual_loop_with_memoized_configs(:force_ruby_platform)
    Bundler.settings.manual_loop_with_memoized_configs(:silence_deprecations)

    Benchmark.memory do |x|
    x.report("original") { Bundler.settings.original(:force_ruby_platform) }
    @@ -131,6 +138,7 @@ def memoized_configs
    x.report("correct manual_loop") { Bundler.settings.correct_manual_loop(:force_ruby_platform) }
    x.report("correct manual_loop w/ memoized configs") { Bundler.settings.correct_manual_loop_with_memoized_configs(:force_ruby_platform) }
    x.report("correct manual_loop w/ each_config") { Bundler.settings.correct_manual_with_each_config(:force_ruby_platform) }
    x.report("with_flat_map") { Bundler.settings.with_flat_map(:force_ruby_platform) }

    x.compare!
    end
    @@ -145,6 +153,7 @@ def memoized_configs
    x.report("correct manual_loop") { Bundler.settings.correct_manual_loop(:force_ruby_platform) }
    x.report("correct manual_loop w/ memoized configs") { Bundler.settings.correct_manual_loop_with_memoized_configs(:force_ruby_platform) }
    x.report("correct manual_loop w/ each_config") { Bundler.settings.correct_manual_with_each_config(:force_ruby_platform) }
    x.report("with_flat_map") { Bundler.settings.with_flat_map(:force_ruby_platform) }

    x.compare! order: :baseline
    end
    79 changes: 43 additions & 36 deletions results.txt
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,12 @@
    Calculating -------------------------------------
    original 968.000 memsize ( 0.000 retained)
    12.000 objects ( 0.000 retained)
    original 720.000 memsize ( 0.000 retained)
    10.000 objects ( 0.000 retained)
    3.000 strings ( 0.000 retained)
    bang_method 560.000 memsize ( 0.000 retained)
    8.000 objects ( 0.000 retained)
    3.000 strings ( 0.000 retained)
    with_detect 560.000 memsize ( 0.000 retained)
    8.000 objects ( 0.000 retained)
    with_detect 560.000 memsize ( 80.000 retained)
    8.000 objects ( 1.000 retained)
    3.000 strings ( 0.000 retained)
    with_detect w/ memoized configs
    560.000 memsize ( 0.000 retained)
    @@ -30,53 +30,60 @@ correct manual_loop w/ each_config
    312.000 memsize ( 0.000 retained)
    6.000 objects ( 0.000 retained)
    3.000 strings ( 0.000 retained)
    with_flat_map 920.000 memsize ( 168.000 retained)
    14.000 objects ( 1.000 retained)
    3.000 strings ( 0.000 retained)

    Comparison:
    manual_loop w/ memoized configs: 312 allocated
    correct manual_loop w/ memoized configs: 312 allocated
    manual_loop w/ memoized configs: 312 allocated - same
    correct manual_loop w/ each_config: 312 allocated - same
    correct manual_loop w/ memoized configs: 312 allocated - same
    manual_loop: 480 allocated - 1.54x more
    correct manual_loop: 480 allocated - 1.54x more
    manual_loop: 480 allocated - 1.54x more
    with_detect: 560 allocated - 1.79x more
    with_detect w/ memoized configs: 560 allocated - 1.79x more
    bang_method: 560 allocated - 1.79x more
    with_detect: 560 allocated - 1.79x more
    original: 968 allocated - 3.10x more
    original: 720 allocated - 2.31x more
    with_flat_map: 920 allocated - 2.95x more
    Warming up --------------------------------------
    original 62.419k i/100ms
    bang_method 93.181k i/100ms
    with_detect 87.615k i/100ms
    original 90.970k i/100ms
    bang_method 90.281k i/100ms
    with_detect 85.338k i/100ms
    with_detect w/ memoized configs
    88.067k i/100ms
    manual_loop 92.334k i/100ms
    89.005k i/100ms
    manual_loop 92.693k i/100ms
    manual_loop w/ memoized configs
    104.202k i/100ms
    correct manual_loop 89.741k i/100ms
    103.907k i/100ms
    correct manual_loop 88.976k i/100ms
    correct manual_loop w/ memoized configs
    102.730k i/100ms
    100.113k i/100ms
    correct manual_loop w/ each_config
    112.958k i/100ms
    112.645k i/100ms
    with_flat_map 52.569k i/100ms
    Calculating -------------------------------------
    original 621.189k (± 2.6%) i/s - 3.121M in 5.027919s
    bang_method 926.525k1.1%) i/s - 4.659M in 5.029196s
    with_detect 871.602k (± 1.5%) i/s - 4.381M in 5.027277s
    original 900.679k (± 2.7%) i/s - 4.548M in 5.054191s
    bang_method 921.335k5.2%) i/s - 4.604M in 5.017818s
    with_detect 874.357k (± 1.2%) i/s - 4.438M in 5.075983s
    with_detect w/ memoized configs
    863.151k2.8%) i/s - 4.315M in 5.003791s
    manual_loop 879.905k10.6%) i/s - 4.340M in 5.020435s
    874.308k1.0%) i/s - 4.450M in 5.090577s
    manual_loop 923.377k 0.9%) i/s - 4.635M in 5.019649s
    manual_loop w/ memoized configs
    1.005M (± 1.9%) i/s - 5.106M in 5.080247s
    correct manual_loop 875.233k2.0%) i/s - 4.397M in 5.026134s
    1.042M (± 1.5%) i/s - 5.299M in 5.087562s
    correct manual_loop 892.766k1.2%) i/s - 4.538M in 5.083618s
    correct manual_loop w/ memoized configs
    1.012M1.1%) i/s - 5.136M in 5.078291s
    1.023M0.7%) i/s - 5.206M in 5.086753s
    correct manual_loop w/ each_config
    1.117M (± 1.0%) i/s - 5.648M in 5.056954s
    1.136M (± 0.7%) i/s - 5.745M in 5.058002s
    with_flat_map 540.224k (± 0.7%) i/s - 2.734M in 5.060378s

    Comparison:
    original: 621189.3 i/s
    correct manual_loop w/ each_config: 1116979.1 i/s - 1.80x faster
    correct manual_loop w/ memoized configs: 1011586.8 i/s - 1.63x faster
    manual_loop w/ memoized configs: 1005409.0 i/s - 1.62x faster
    bang_method: 926525.3 i/s - 1.49x faster
    manual_loop: 879904.8 i/s - 1.42x faster
    correct manual_loop: 875233.3 i/s - 1.41x faster
    with_detect: 871602.0 i/s - 1.40x faster
    with_detect w/ memoized configs: 863151.4 i/s - 1.39x faster
    original: 900679.3 i/s
    correct manual_loop w/ each_config: 1135861.2 i/s - 1.26x faster
    manual_loop w/ memoized configs: 1041836.7 i/s - 1.16x faster
    correct manual_loop w/ memoized configs: 1023470.5 i/s - 1.14x faster
    manual_loop: 923377.4 i/s - same-ish: difference falls within error
    bang_method: 921335.0 i/s - same-ish: difference falls within error
    correct manual_loop: 892766.1 i/s - same-ish: difference falls within error
    with_detect: 874357.4 i/s - same-ish: difference falls within error
    with_detect w/ memoized configs: 874308.3 i/s - same-ish: difference falls within error
    with_flat_map: 540224.1 i/s - 1.67x slower
  2. technicalpickles revised this gist Aug 30, 2023. 2 changed files with 114 additions and 37 deletions.
    50 changes: 50 additions & 0 deletions benchmark.rb
    Original file line number Diff line number Diff line change
    @@ -50,6 +50,7 @@ def manual_loop(name)
    converted_value(value, name)
    end


    def manual_loop_with_memoized_configs(name)
    key = key_for(name)
    value = nil
    @@ -62,8 +63,50 @@ def manual_loop_with_memoized_configs(name)
    converted_value(value, name)
    end

    def correct_manual_loop(name)
    key = key_for(name)
    value = nil
    configs.each do |_, config|
    value = config[key]
    next if value.nil?
    break
    end
    converted_value(value, name)
    end

    def correct_manual_loop_with_memoized_configs(name)
    key = key_for(name)
    value = nil
    memoized_configs.each do |_, config|
    value = config[key]
    next if value.nil?
    break
    end
    converted_value(value, name)
    end

    def correct_manual_with_each_config(name)
    key = key_for(name)
    value = nil
    each_config do |config|
    value = config[key]
    next if value.nil?
    break
    end
    converted_value(value, name)
    end

    private

    def each_config
    yield @temporary
    yield @local_config
    yield @env_config
    yield @global_config
    yield DEFAULT_CONFIG
    end


    def memoized_configs
    @memoized_configs ||= {
    :temporary => @temporary,
    @@ -75,6 +118,7 @@ def memoized_configs
    end
    end

    # warm up memoization
    Bundler.settings.manual_loop_with_memoized_configs(:force_ruby_platform)

    Benchmark.memory do |x|
    @@ -84,6 +128,9 @@ def memoized_configs
    x.report("with_detect w/ memoized configs") { Bundler.settings.with_detect_with_memoized_configs(:force_ruby_platform) }
    x.report("manual_loop") { Bundler.settings.manual_loop(:force_ruby_platform) }
    x.report("manual_loop w/ memoized configs") { Bundler.settings.manual_loop_with_memoized_configs(:force_ruby_platform) }
    x.report("correct manual_loop") { Bundler.settings.correct_manual_loop(:force_ruby_platform) }
    x.report("correct manual_loop w/ memoized configs") { Bundler.settings.correct_manual_loop_with_memoized_configs(:force_ruby_platform) }
    x.report("correct manual_loop w/ each_config") { Bundler.settings.correct_manual_with_each_config(:force_ruby_platform) }

    x.compare!
    end
    @@ -95,6 +142,9 @@ def memoized_configs
    x.report("with_detect w/ memoized configs") { Bundler.settings.with_detect_with_memoized_configs(:force_ruby_platform) }
    x.report("manual_loop") { Bundler.settings.manual_loop(:force_ruby_platform) }
    x.report("manual_loop w/ memoized configs") { Bundler.settings.manual_loop_with_memoized_configs(:force_ruby_platform) }
    x.report("correct manual_loop") { Bundler.settings.correct_manual_loop(:force_ruby_platform) }
    x.report("correct manual_loop w/ memoized configs") { Bundler.settings.correct_manual_loop_with_memoized_configs(:force_ruby_platform) }
    x.report("correct manual_loop w/ each_config") { Bundler.settings.correct_manual_with_each_config(:force_ruby_platform) }

    x.compare! order: :baseline
    end
    101 changes: 64 additions & 37 deletions results.txt
    Original file line number Diff line number Diff line change
    @@ -1,55 +1,82 @@
    Calculating -------------------------------------
    original 800.000 memsize ( 168.000 retained)
    11.000 objects ( 1.000 retained)
    original 968.000 memsize ( 0.000 retained)
    12.000 objects ( 0.000 retained)
    3.000 strings ( 0.000 retained)
    bang_method 560.000 memsize ( 0.000 retained)
    8.000 objects ( 0.000 retained)
    3.000 strings ( 0.000 retained)
    with_detect 560.000 memsize ( 0.000 retained)
    8.000 objects ( 0.000 retained)
    3.000 strings ( 0.000 retained)
    bang_method 392.000 memsize ( 72.000 retained)
    7.000 objects ( 1.000 retained)
    3.000 strings ( 1.000 retained)
    with_detect 392.000 memsize ( 72.000 retained)
    7.000 objects ( 1.000 retained)
    3.000 strings ( 1.000 retained)
    with_detect w/ memoized configs
    392.000 memsize ( 72.000 retained)
    7.000 objects ( 1.000 retained)
    3.000 strings ( 1.000 retained)
    manual_loop 312.000 memsize ( 72.000 retained)
    560.000 memsize ( 0.000 retained)
    8.000 objects ( 0.000 retained)
    3.000 strings ( 0.000 retained)
    manual_loop 480.000 memsize ( 0.000 retained)
    7.000 objects ( 0.000 retained)
    3.000 strings ( 0.000 retained)
    manual_loop w/ memoized configs
    312.000 memsize ( 72.000 retained)
    6.000 objects ( 1.000 retained)
    3.000 strings ( 1.000 retained)
    manual_loop w/ memoized configs
    correct manual_loop 480.000 memsize ( 0.000 retained)
    7.000 objects ( 0.000 retained)
    3.000 strings ( 0.000 retained)
    correct manual_loop w/ memoized configs
    312.000 memsize ( 72.000 retained)
    6.000 objects ( 1.000 retained)
    3.000 strings ( 1.000 retained)
    correct manual_loop w/ each_config
    312.000 memsize ( 0.000 retained)
    6.000 objects ( 0.000 retained)
    3.000 strings ( 0.000 retained)

    Comparison:
    manual_loop: 312 allocated
    manual_loop w/ memoized configs: 312 allocated - same
    bang_method: 392 allocated - 1.26x more
    with_detect: 392 allocated - 1.26x more
    with_detect w/ memoized configs: 392 allocated - 1.26x more
    original: 800 allocated - 2.56x more
    manual_loop w/ memoized configs: 312 allocated
    correct manual_loop w/ each_config: 312 allocated - same
    correct manual_loop w/ memoized configs: 312 allocated - same
    manual_loop: 480 allocated - 1.54x more
    correct manual_loop: 480 allocated - 1.54x more
    with_detect w/ memoized configs: 560 allocated - 1.79x more
    bang_method: 560 allocated - 1.79x more
    with_detect: 560 allocated - 1.79x more
    original: 968 allocated - 3.10x more
    Warming up --------------------------------------
    original 74.589k i/100ms
    bang_method 104.215k i/100ms
    with_detect 99.974k i/100ms
    original 62.419k i/100ms
    bang_method 93.181k i/100ms
    with_detect 87.615k i/100ms
    with_detect w/ memoized configs
    100.748k i/100ms
    manual_loop 104.471k i/100ms
    88.067k i/100ms
    manual_loop 92.334k i/100ms
    manual_loop w/ memoized configs
    105.599k i/100ms
    104.202k i/100ms
    correct manual_loop 89.741k i/100ms
    correct manual_loop w/ memoized configs
    102.730k i/100ms
    correct manual_loop w/ each_config
    112.958k i/100ms
    Calculating -------------------------------------
    original 753.388k1.7%) i/s - 3.804M in 5.050732s
    bang_method 1.038M (± 1.4%) i/s - 5.211M in 5.022015s
    with_detect 1.003M0.9%) i/s - 5.099M in 5.084331s
    original 621.189k2.6%) i/s - 3.121M in 5.027919s
    bang_method 926.525k (± 1.1%) i/s - 4.659M in 5.029196s
    with_detect 871.602k1.5%) i/s - 4.381M in 5.027277s
    with_detect w/ memoized configs
    1.004M1.0%) i/s - 5.037M in 5.018705s
    manual_loop 1.042M 0.8%) i/s - 5.224M in 5.011204s
    863.151k2.8%) i/s - 4.315M in 5.003791s
    manual_loop 879.905k10.6%) i/s - 4.340M in 5.020435s
    manual_loop w/ memoized configs
    1.056M (± 1.2%) i/s - 5.386M in 5.100790s
    1.005M (± 1.9%) i/s - 5.106M in 5.080247s
    correct manual_loop 875.233k (± 2.0%) i/s - 4.397M in 5.026134s
    correct manual_loop w/ memoized configs
    1.012M (± 1.1%) i/s - 5.136M in 5.078291s
    correct manual_loop w/ each_config
    1.117M (± 1.0%) i/s - 5.648M in 5.056954s

    Comparison:
    original: 753387.7 i/s
    manual_loop w/ memoized configs: 1055974.9 i/s - 1.40x faster
    manual_loop: 1042434.4 i/s - 1.38x faster
    bang_method: 1037808.1 i/s - 1.38x faster
    with_detect w/ memoized configs: 1003831.7 i/s - 1.33x faster
    with_detect: 1002897.1 i/s - 1.33x faster
    original: 621189.3 i/s
    correct manual_loop w/ each_config: 1116979.1 i/s - 1.80x faster
    correct manual_loop w/ memoized configs: 1011586.8 i/s - 1.63x faster
    manual_loop w/ memoized configs: 1005409.0 i/s - 1.62x faster
    bang_method: 926525.3 i/s - 1.49x faster
    manual_loop: 879904.8 i/s - 1.42x faster
    correct manual_loop: 875233.3 i/s - 1.41x faster
    with_detect: 871602.0 i/s - 1.40x faster
    with_detect w/ memoized configs: 863151.4 i/s - 1.39x faster
  3. technicalpickles created this gist Aug 29, 2023.
    100 changes: 100 additions & 0 deletions benchmark.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,100 @@
    require "benchmark/ips"
    require "benchmark/memory"

    require 'bundler'
    class Bundler::Settings
    def original(name)
    key = key_for(name)
    config_with_key = configs.values.detect {|config| config[key] }
    value = configs.values.map {|config| config[key] }.compact.first

    converted_value(value, name)
    end

    def bang_method(name)
    key = key_for(name)

    values = configs.values
    values.map! {|config| config[key] }
    values.compact!
    value = values.first

    converted_value(value, name)
    end

    def with_detect(name)
    key = key_for(name)
    config_with_key = configs.values.detect {|config| config[key] }
    value = config_with_key[key] if config_with_key

    converted_value(value, name)
    end

    def with_detect_with_memoized_configs(name)
    key = key_for(name)
    config_with_key = configs.values.detect {|config| config[key] }
    value = config_with_key[key] if config_with_key

    converted_value(value, name)
    end

    def manual_loop(name)
    key = key_for(name)
    value = nil
    configs.each do |_, config|
    if config[key]
    value = config[key]
    break
    end
    end
    converted_value(value, name)
    end

    def manual_loop_with_memoized_configs(name)
    key = key_for(name)
    value = nil
    memoized_configs.each do |_, config|
    if config[key]
    value = config[key]
    break
    end
    end
    converted_value(value, name)
    end

    private

    def memoized_configs
    @memoized_configs ||= {
    :temporary => @temporary,
    :local => @local_config,
    :env => @env_config,
    :global => @global_config,
    :default => DEFAULT_CONFIG,
    }.freeze
    end
    end

    Bundler.settings.manual_loop_with_memoized_configs(:force_ruby_platform)

    Benchmark.memory do |x|
    x.report("original") { Bundler.settings.original(:force_ruby_platform) }
    x.report("bang_method") { Bundler.settings.bang_method(:force_ruby_platform) }
    x.report("with_detect") { Bundler.settings.with_detect(:force_ruby_platform) }
    x.report("with_detect w/ memoized configs") { Bundler.settings.with_detect_with_memoized_configs(:force_ruby_platform) }
    x.report("manual_loop") { Bundler.settings.manual_loop(:force_ruby_platform) }
    x.report("manual_loop w/ memoized configs") { Bundler.settings.manual_loop_with_memoized_configs(:force_ruby_platform) }

    x.compare!
    end

    Benchmark.ips do |x|
    x.report("original") { Bundler.settings.original(:force_ruby_platform) }
    x.report("bang_method") { Bundler.settings.bang_method(:force_ruby_platform) }
    x.report("with_detect") { Bundler.settings.with_detect(:force_ruby_platform) }
    x.report("with_detect w/ memoized configs") { Bundler.settings.with_detect_with_memoized_configs(:force_ruby_platform) }
    x.report("manual_loop") { Bundler.settings.manual_loop(:force_ruby_platform) }
    x.report("manual_loop w/ memoized configs") { Bundler.settings.manual_loop_with_memoized_configs(:force_ruby_platform) }

    x.compare! order: :baseline
    end
    55 changes: 55 additions & 0 deletions results.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    Calculating -------------------------------------
    original 800.000 memsize ( 168.000 retained)
    11.000 objects ( 1.000 retained)
    3.000 strings ( 0.000 retained)
    bang_method 392.000 memsize ( 72.000 retained)
    7.000 objects ( 1.000 retained)
    3.000 strings ( 1.000 retained)
    with_detect 392.000 memsize ( 72.000 retained)
    7.000 objects ( 1.000 retained)
    3.000 strings ( 1.000 retained)
    with_detect w/ memoized configs
    392.000 memsize ( 72.000 retained)
    7.000 objects ( 1.000 retained)
    3.000 strings ( 1.000 retained)
    manual_loop 312.000 memsize ( 72.000 retained)
    6.000 objects ( 1.000 retained)
    3.000 strings ( 1.000 retained)
    manual_loop w/ memoized configs
    312.000 memsize ( 72.000 retained)
    6.000 objects ( 1.000 retained)
    3.000 strings ( 1.000 retained)

    Comparison:
    manual_loop: 312 allocated
    manual_loop w/ memoized configs: 312 allocated - same
    bang_method: 392 allocated - 1.26x more
    with_detect: 392 allocated - 1.26x more
    with_detect w/ memoized configs: 392 allocated - 1.26x more
    original: 800 allocated - 2.56x more
    Warming up --------------------------------------
    original 74.589k i/100ms
    bang_method 104.215k i/100ms
    with_detect 99.974k i/100ms
    with_detect w/ memoized configs
    100.748k i/100ms
    manual_loop 104.471k i/100ms
    manual_loop w/ memoized configs
    105.599k i/100ms
    Calculating -------------------------------------
    original 753.388k (± 1.7%) i/s - 3.804M in 5.050732s
    bang_method 1.038M (± 1.4%) i/s - 5.211M in 5.022015s
    with_detect 1.003M (± 0.9%) i/s - 5.099M in 5.084331s
    with_detect w/ memoized configs
    1.004M (± 1.0%) i/s - 5.037M in 5.018705s
    manual_loop 1.042M (± 0.8%) i/s - 5.224M in 5.011204s
    manual_loop w/ memoized configs
    1.056M (± 1.2%) i/s - 5.386M in 5.100790s

    Comparison:
    original: 753387.7 i/s
    manual_loop w/ memoized configs: 1055974.9 i/s - 1.40x faster
    manual_loop: 1042434.4 i/s - 1.38x faster
    bang_method: 1037808.1 i/s - 1.38x faster
    with_detect w/ memoized configs: 1003831.7 i/s - 1.33x faster
    with_detect: 1002897.1 i/s - 1.33x faster