-
Notifications
You must be signed in to change notification settings - Fork 444
Optimize key formatter #597
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
rafaelfranca
merged 9 commits into
rails:main
from
affinity:moberegger/optimize_key_formatter
Jul 18, 2025
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7c58bcc
Optimize key formatter
moberegger b524e6c
Reuse key cache across template renders
moberegger 47824af
Save on some memory allocation during initialization
moberegger a6f1eed
Use mutex for thread safety
moberegger 964ef0b
Optimize jbuilder initialization
moberegger 34814e7
Repurpose test to validate default formatter cache is used
moberegger 5ea899b
Invert options nil check
moberegger be609e7
Cleanup initializer
moberegger e0d60fd
Save on call to ::Kernal.block_given?
moberegger 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 |
|---|---|---|
| @@ -1,34 +1,26 @@ | ||
| require 'jbuilder/jbuilder' | ||
| require 'active_support/core_ext/array' | ||
|
|
||
| class Jbuilder | ||
| class KeyFormatter | ||
| def initialize(*args) | ||
| @format = {} | ||
| @cache = {} | ||
| def initialize(*formats, **formats_with_options) | ||
| @cache = | ||
| Hash.new do |hash, key| | ||
| value = key.is_a?(Symbol) ? key.name : key.to_s | ||
|
|
||
| options = args.extract_options! | ||
| args.each do |name| | ||
| @format[name] = [] | ||
| end | ||
| options.each do |name, parameters| | ||
| @format[name] = parameters | ||
| end | ||
| end | ||
| formats.each do |func| | ||
| value = func.is_a?(Proc) ? func.call(value) : value.send(func) | ||
| end | ||
|
|
||
| formats_with_options.each do |func, params| | ||
| value = func.is_a?(Proc) ? func.call(value, *params) : value.send(func, *params) | ||
| end | ||
|
|
||
| def initialize_copy(original) | ||
| @cache = {} | ||
| hash[key] = value | ||
| end | ||
| end | ||
|
|
||
| def format(key) | ||
| @cache[key] ||= @format.inject(key.to_s) do |result, args| | ||
| func, args = args | ||
| if ::Proc === func | ||
| func.call result, *args | ||
| else | ||
| result.send func, *args | ||
| end | ||
| end | ||
| @cache[key] | ||
| 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
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.