Lazy-allocate @api_class and @point_in_time_copies on InheritableSetting#2740
Open
ericproulx wants to merge 1 commit into
Open
Lazy-allocate @api_class and @point_in_time_copies on InheritableSetting#2740ericproulx wants to merge 1 commit into
ericproulx wants to merge 1 commit into
Conversation
…Setting
`InheritableSetting#initialize` used to eagerly allocate both fields up
front. Neither is touched on most settings layers:
- `@api_class` is only written by external code (e.g. plugins) via
`setting.api_class[:k] = v`; nothing in `lib/` writes to it.
- `@point_in_time_copies` only fills up on settings that get cloned via
`point_in_time_copy` — typically the API-class-level setting, not the
per-endpoint copies that make up the bulk of allocations at boot.
Switch both to memoizing readers (`@x ||= …`), drop the eager init, and
rewrite the `inherit_from` propagation loop to access the ivar directly
with `&.each` so the no-copies path doesn't allocate the Array just to
iterate zero elements.
Measured against master:
Grape::Util::InheritableSetting.new × 100
before: 1600 objects, 183.6 kB
after: 1400 objects, 164.1 kB (-11%)
Stacks with #2739 (lazy `@new_values`) for compounded boot-time savings.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
4ed4652 to
6bea55d
Compare
Danger ReportNo issues found. |
dblock
approved these changes
May 25, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
InheritableSetting#initializeused to eagerly allocate@api_class = {}and@point_in_time_copies = []per instance, even though most settings layers never touch either:@api_classis only written by external code (plugins) viasetting.api_class[:k] = v; nothing inlib/writes to it.@point_in_time_copiesonly fills up on settings that get cloned viapoint_in_time_copy— typically the API-class-level setting, not the per-endpoint copies that dominate boot-time allocations.@x ||= …), drop the eager init, and rewrite theinherit_frompropagation loop to access the ivar directly with&.eachso the no-copies path doesn't allocate the Array just to iterate zero elements.Benchmarks
Grape::Util::InheritableSetting.new × 100Smaller absolute win than #2739, but stacks with it for compounded boot-time savings.
Test plan
bundle exec rspec— 2313 examples, 0 failuresbundle exec rubocop— clean on touched file🤖 Generated with Claude Code