Skip to content

Lazy-allocate @new_values in Grape::Util::BaseInheritable#2739

Open
ericproulx wants to merge 1 commit into
masterfrom
perf/lazy-base-inheritable-new-values
Open

Lazy-allocate @new_values in Grape::Util::BaseInheritable#2739
ericproulx wants to merge 1 commit into
masterfrom
perf/lazy-base-inheritable-new-values

Conversation

@ericproulx
Copy link
Copy Markdown
Contributor

Summary

  • 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, []= writers do (@new_values ||= {})[...] = ..., and the readers in InheritableValues#[] / StackableValues#[] short-circuit on nil instead of touching an empty Hash.
  • initialize_copy skips the .dup when the source has no own values.

Benchmarks

Measured against master with MemoryProfiler.report:

Scenario Before After Delta
Grape::Util::InheritableSetting.new × 100 1600 objects / 183.6 kB 1200 objects / 121.1 kB −34%
parent.point_in_time_copy × 100 2500 objects / 277.3 kB 1700 objects / 152.3 kB −45%

The bigger point_in_time_copy win matches the base_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 failures
  • bundle exec rubocop — clean on touched files
  • CI green

🤖 Generated with Claude Code

`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>
@ericproulx ericproulx force-pushed the perf/lazy-base-inheritable-new-values branch from 4da13c7 to faf5e4a Compare May 23, 2026 17:36
@github-actions
Copy link
Copy Markdown

Danger Report

Errors

  • One of the lines below found in CHANGELOG.md doesn't match the expected format. Please make it look like the other lines, pay attention to version numbers, periods, spaces and date formats.

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

View run

@github-actions
Copy link
Copy Markdown

Danger Report

No issues found.

View run

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>
@ericproulx ericproulx requested a review from dblock May 23, 2026 17:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant