-
Notifications
You must be signed in to change notification settings - Fork 3
Performance improvements #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'bundler/setup' | ||
| require 'benchmark/ips' | ||
| require 'hash_kit' | ||
|
|
||
| helper = HashKit::Helper.new | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Hash builders – every benchmark iteration gets a fresh hash so that | ||
| # default_proc assignment is always exercised (no short-circuit). | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| def build_small_flat_hash | ||
| { 'name' => 'Alice', 'age' => 30, 'active' => true } | ||
| end | ||
|
|
||
| def build_large_flat_hash(n = 100) | ||
| (0...n).each_with_object({}) { |i, h| h["key_#{i}"] = "value_#{i}" } | ||
| end | ||
|
|
||
| def build_nested_hash(depth = 5) | ||
| hash = { 'leaf' => 'value' } | ||
| depth.times { |i| hash = { "level_#{i}" => hash, "sibling_#{i}" => 'data' } } | ||
| hash | ||
| end | ||
|
|
||
| def build_hash_with_arrays | ||
| { | ||
| 'users' => [ | ||
| { 'name' => 'Alice', 'roles' => [{ 'id' => 1, 'name' => 'admin' }] }, | ||
| { 'name' => 'Bob', 'roles' => [{ 'id' => 2, 'name' => 'editor' }] }, | ||
| { 'name' => 'Carol', 'roles' => [{ 'id' => 3, 'name' => 'viewer' }] } | ||
| ], | ||
| 'meta' => { 'page' => 1, 'total' => 3 } | ||
| } | ||
| end | ||
|
|
||
| def build_large_nested_hash(width = 20, depth = 4) | ||
| return (0...width).each_with_object({}) { |i, h| h["key_#{i}"] = "val_#{i}" } if depth == 0 | ||
|
|
||
| (0...width).each_with_object({}) do |i, h| | ||
| h["node_#{i}"] = build_large_nested_hash(width, depth - 1) | ||
| end | ||
| end | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Benchmark suite | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| puts 'HashKit::Helper#indifferent! benchmark' | ||
| puts "Ruby #{RUBY_VERSION} / benchmark-ips #{Benchmark::IPS::VERSION}" | ||
| puts '=' * 60 | ||
|
|
||
| Benchmark.ips do |x| | ||
| x.config(warmup: 2, time: 5) | ||
|
|
||
| # --- Scenario 1: small flat hash (3 keys) --- | ||
| x.report('small flat hash (3 keys)') do | ||
| helper.indifferent!(build_small_flat_hash) | ||
| end | ||
|
|
||
| # --- Scenario 2: large flat hash (100 keys) --- | ||
| x.report('large flat hash (100 keys)') do | ||
| helper.indifferent!(build_large_flat_hash(100)) | ||
| end | ||
|
|
||
| # --- Scenario 3: deeply nested hash (5 levels) --- | ||
| x.report('nested hash (depth=5)') do | ||
| helper.indifferent!(build_nested_hash(5)) | ||
| end | ||
|
|
||
| # --- Scenario 4: hash containing arrays of hashes --- | ||
| x.report('hash with arrays') do | ||
| helper.indifferent!(build_hash_with_arrays) | ||
| end | ||
|
|
||
| # --- Scenario 5: large nested hash (wide + deep) --- | ||
| x.report('large nested (10x3)') do | ||
| helper.indifferent!(build_large_nested_hash(10, 3)) | ||
| end | ||
|
|
||
| # --- Scenario 6: many sequential calls on small hashes --- | ||
| x.report('50x sequential small hashes') do | ||
| 50.times { helper.indifferent!(build_small_flat_hash) } | ||
| end | ||
|
|
||
| # --- Scenario 7: many sequential calls on medium hashes --- | ||
| x.report('50x sequential nested hashes') do | ||
| 50.times { helper.indifferent!(build_nested_hash(3)) } | ||
| end | ||
|
|
||
| x.compare! | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| services: | ||
| gem_test_runner: | ||
| image: ruby:2.7.8-bullseye | ||
| image: ruby:4 | ||
| container_name: gem_test_runner | ||
| command: bash -c "while true; do echo 'Container is running...'; sleep 2; done" | ||
| volumes: | ||
| - ./:/gem_src | ||
| - ./:/gem_src |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.