Lazy-allocate @new_values in Grape::Util::BaseInheritable#2739
Open
ericproulx wants to merge 1 commit into
Open
Lazy-allocate @new_values in Grape::Util::BaseInheritable#2739ericproulx wants to merge 1 commit into
ericproulx wants to merge 1 commit into
Conversation
`BaseInheritable#initialize` used to eagerly allocate an empty
`@new_values = {}` per instance. Most settings layers in a typical API
only inherit from a parent and never write any *new* values of their
own, so the empty Hash was just retained dead weight.
Defer the allocation: `@new_values` starts nil, the `[]=` writers do
`(@new_values ||= {})[name] = ...`, and the read paths in
`InheritableValues#[]` / `StackableValues#[]` short-circuit on nil so
they skip the lookup entirely instead of touching an empty Hash.
`initialize_copy` skips the `.dup` when the source has no own values.
Measured on master with a small benchmark:
Grape::Util::InheritableSetting.new ×100
before: 1600 objects, 183.6 kB
after: 1200 objects, 121.1 kB (-34%)
parent.point_in_time_copy ×100
before: 2500 objects, 277.3 kB
after: 1700 objects, 152.3 kB (-45%)
The bigger win on `point_in_time_copy` matches the
`base_inheritable.rb:27` (`new_values.dup`) hot spot in the boot-time
memory profile: that line previously dup'd four empty Hashes per copy
and now skips them entirely.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
4da13c7 to
faf5e4a
Compare
Danger ReportErrors
Markdowns* [#PRNUMBER](https://github.com/ruby-grape/grape/pull/PRNUMBER): Lazy-allocate `@new_values` in `Grape::Util::BaseInheritable` so settings layers that only inherit never carry an empty Hash; readers in `InheritableValues`/`StackableValues` handle nil - [@ericproulx](https://github.com/ericproulx).
does not include a pull request link |
Danger ReportNo issues found. |
3 tasks
ericproulx
added a commit
that referenced
this pull request
May 23, 2026
…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>
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
BaseInheritable#initializeused to eagerly allocate an empty@new_values = {}per instance. Most settings layers in a typical API only inherit from a parent and never write any new values of their own, so the empty Hash was just retained dead weight.@new_valuesstartsnil,[]=writers do(@new_values ||= {})[...] = ..., and the readers inInheritableValues#[]/StackableValues#[]short-circuit on nil instead of touching an empty Hash.initialize_copyskips the.dupwhen the source has no own values.Benchmarks
Measured against master with
MemoryProfiler.report:Grape::Util::InheritableSetting.new × 100parent.point_in_time_copy × 100The bigger
point_in_time_copywin matches thebase_inheritable.rb:27(new_values.dup) hot spot in the boot-time memory profile — that line used to dup four empty Hashes per copy and now skips them entirely.Test plan
bundle exec rspec— 2313 examples, 0 failuresbundle exec rubocop— clean on touched files🤖 Generated with Claude Code