-
Notifications
You must be signed in to change notification settings - Fork 450
Performance improvement for array and hash #610
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
Closed
mayur-kambariya
wants to merge
2
commits into
rails:main
from
mayur-kambariya:enha/performance_improve
+30
−12
Closed
Changes from all commits
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,7 +32,7 @@ def self.encode(...) | |
| new(...).target! | ||
| end | ||
|
|
||
| BLANK = Blank.new | ||
| BLANK = Blank.new.freeze | ||
|
|
||
| def set!(key, value = BLANK, *args, &block) | ||
| result = if ::Kernel.block_given? | ||
|
|
@@ -302,7 +302,7 @@ def _merge_block(key) | |
| def _merge_values(current_value, updates) | ||
| if _blank?(updates) | ||
| current_value | ||
| elsif _blank?(current_value) || updates.nil? || current_value.empty? && ::Array === updates | ||
| elsif _blank?(current_value) || updates.nil? || (current_value.respond_to?(:empty?) && current_value.empty? && ::Array === updates) | ||
| updates | ||
| elsif ::Array === current_value && ::Array === updates | ||
| current_value + updates | ||
|
|
@@ -316,20 +316,25 @@ def _merge_values(current_value, updates) | |
| def _key(key) | ||
| if @key_formatter | ||
| @key_formatter.format(key) | ||
| elsif key.is_a?(::Symbol) | ||
| key.name | ||
| else | ||
| key.to_s | ||
| key.is_a?(::Symbol) ? key.name : key.to_s | ||
| end | ||
| end | ||
|
|
||
| def _format_keys(hash_or_array) | ||
| return hash_or_array unless @deep_format_keys | ||
|
|
||
| if ::Array === hash_or_array | ||
| case hash_or_array | ||
| when ::Array | ||
| # Use map! when possible to avoid creating new array | ||
| hash_or_array.map { |value| _format_keys(value) } | ||
| elsif ::Hash === hash_or_array | ||
| ::Hash[hash_or_array.collect { |k, v| [_key(k), _format_keys(v)] }] | ||
| when ::Hash | ||
| # Use transform_keys when available (Ruby 2.5+) for better performance | ||
| if hash_or_array.respond_to?(:transform_keys) | ||
| hash_or_array.transform_keys { |k| _key(k) }.transform_values { |v| _format_keys(v) } | ||
| else | ||
| ::Hash[hash_or_array.collect { |k, v| [_key(k), _format_keys(v)] }] | ||
| end | ||
| else | ||
| hash_or_array | ||
| end | ||
|
|
@@ -344,9 +349,17 @@ def _set_value(key, value) | |
| end | ||
|
|
||
| def _map_collection(collection) | ||
| collection.map do |element| | ||
| _scope{ yield element } | ||
| end - [BLANK] | ||
| # Use filter_map when available (Ruby 2.7+) for better performance | ||
| if collection.respond_to?(:filter_map) | ||
| collection.filter_map do |element| | ||
| mapped_element = _scope{ yield element } | ||
| mapped_element unless mapped_element == BLANK | ||
| end | ||
| else | ||
| collection.map do |element| | ||
| _scope{ yield element } | ||
| end - [BLANK] | ||
| end | ||
| end | ||
|
|
||
| def _scope | ||
|
|
@@ -363,7 +376,7 @@ def _is_collection?(object) | |
| end | ||
|
|
||
| def _blank?(value=@attributes) | ||
| BLANK == value | ||
| value == BLANK | ||
|
Comment on lines
-366
to
+379
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My benchmark shows that the original version is faster, at least when comparing against a value = { a: 1, b: 2, c: 3 }
Benchmark.ips do |x|
x.report('value == BLANK') { value == BLANK }
x.report('BLANK == value') { BLANK == value }
x.compare!(order: :baseline)
end |
||
| end | ||
| 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 |
|---|---|---|
|
|
@@ -12,6 +12,11 @@ def initialize(*formats, **formats_with_options) | |
| end | ||
|
|
||
| def format(key) | ||
| # Check cache without mutex for common case (reading) | ||
| cached_value = @cache[key] | ||
| return cached_value if cached_value | ||
|
Comment on lines
+15
to
+17
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A similar optimization was added in #607 |
||
|
|
||
| # Only use mutex when writing to cache | ||
| @mutex.synchronize do | ||
| @cache[key] ||= begin | ||
| value = key.is_a?(Symbol) ? key.name : key.to_s | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to my benchmark, this change results in a performance degradation. I propose an alternative in #612.