Skip to content

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

Merged
ericproulx merged 2 commits into
masterfrom
perf/lazy-base-inheritable-new-values
May 27, 2026
Merged

Lazy-allocate @new_values in Grape::Util::BaseInheritable#2739
ericproulx merged 2 commits 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

@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

github-actions Bot commented May 23, 2026

Danger Report

No issues found.

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
ericproulx added a commit that referenced this pull request May 26, 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>
`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 faf5e4a to dd7377c Compare May 26, 2026 18:50
@ericproulx ericproulx merged commit ff888ff into master May 27, 2026
120 of 121 checks passed
@ericproulx ericproulx deleted the perf/lazy-base-inheritable-new-values branch May 27, 2026 06:11
ericproulx added a commit that referenced this pull request May 27, 2026
…Setting (#2740)

`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>
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.

2 participants