Draft: Defer building in-memory application settings in specs

What does this MR do and why?

Every example under spec_helper calls stub_application_setting three times from global before hooks: admin_mode, Snowplow, and the CI job token signing key. Each call stubbed Gitlab::CurrentSettings.current_application_settings directly, and that builds the in-memory ApplicationSetting from defaults. spec_helper clears the object after every example, so every example rebuilt it, even examples that never read a setting. ApplicationSetting.build_from_defaults is slow because each of its 63 jsonb_accessor columns runs an after_initialize callback.

stub_application_setting still stubs allow_any_instance_of(ApplicationSetting) right away. When the in-memory object has not been built yet, it now saves the per-object stub and applies it, in order, the first time the object is built. spec_helper prepends StubConfiguration::DeferredApplicationSettingStubs onto Gitlab::ApplicationSettingFetcher to apply the saved stubs on build. clear_in_memory_application_settings! drops them, just as the old stubs were dropped with the cleared object. When the object is already built, when it is cached in SafeRequestStore, or when IN_MEMORY_APPLICATION_SETTINGS is not true, the helper stubs the object right away, as before. The opt-out tags (do_not_mock_admin_mode_setting, do_not_stub_snowplow_by_default, do_not_stub_ci_job_token_signing_key) are unchanged.

This changes spec support only: no application code and no feature flag.

Measurements

These are local measurements in a development container. CI numbers will differ.

Timings are from bundle exec rspec --order defined, using Finished in (file load excluded). There were five runs per side, alternating master and branch:

Suite Master mean (sd) Branch mean (sd) Change
1,000 empty examples 17.29 s (0.61) 11.70 s (0.74) −5.59 s, −32% (≈ −5.6 ms per example)
spec/policies/global_policy_spec.rb + spec/models/key_spec.rb (273 examples) 19.65 s (1.05) 19.31 s (1.12) −0.34 s, within noise

The saving applies only to examples that never read an application setting. Most examples in the two real spec files read settings (for example, through SshKeyValidator), so they still build the object and show no measurable change. How much CI time this saves depends on how many examples never touch settings.

Alternatives considered

These alternatives come from review. Numbers are per example, measured in-process from before(:suite) to after(:suite), with GC.stat(:total_allocated_objects). Timings are the mean of 5 runs (3 for the dup rows); allocation counts are deterministic.

Variant 1,000 empty examples global_policy_spec + key_spec 200 settings reads per example
master 14.86 ms, 14,525 allocs 67.2 ms, 40,061 allocs 42.9 ms, 23,687 allocs
this MR 10.59 ms, 9,694 allocs 67.0 ms, 38,133 allocs 40.4 ms, 23,205 allocs
stub in_memory_application_settings per example, apply saved stubs on first call 10.87 ms, 9,782 allocs 65.0 ms, 38,441 allocs 46.1 ms, 32,648 allocs
master + reuse a pristine defaults object (dup per example) 12.60 ms, 12,074 allocs 63.8 ms, 37,521 allocs not measured
this MR + reuse a pristine defaults object (dup per example) 10.71 ms, 9,696 allocs 61.5 ms, 36,407 allocs not measured
  • Stubbing the fetcher method skips the build the same way, but every settings read then goes through an RSpec proxy: about 47 more allocations and about 28 µs per read, plus about 90 allocations per example to install the stub.
  • !270980 (application_settings_cache_frozen) changes only ApplicationSetting.cached. Specs set IN_MEMORY_APPLICATION_SETTINGS=true, so they never take that path. Reusing one pristine object per process would need a copy per example: specs stub and write settings, and ApplicationSetting.defaults reads Settings.gitlab, which specs change with stub_config. A dup costs 1.2 ms against 3.3 ms for build_from_defaults. It complements this MR and could be a follow-up, but it would serve stale defaults to specs that change Settings first.

Testing

  • spec/lib/gitlab/application_setting_fetcher_spec.rb, spec/lib/gitlab/current_settings_spec.rb, spec/models/application_setting_spec.rb, ee/spec/policies/global_policy_spec.rb, spec/requests/api/settings_spec.rb, spec/lib/gitlab/tracking_spec.rb, spec/lib/gitlab/auth/current_user_mode_spec.rb, a migration spec, and a :delete spec pass locally.
  • Other specs that use stub_application_setting heavily (spec/models/user_spec.rb, the Rack Attack specs, ee/spec/policies/project_policy_spec.rb) could only be partly checked locally because Gitaly was not available. Their local failures were Gitaly, Elasticsearch, or asset-compilation errors. The one other failure, spec/requests/api/users_spec.rb:3385, fails the same way on master. CI covers the rest.
  • What could break: a spec that stubs Gitlab config values that the defaults read (for example, stub_config_setting) before it first reads a setting now sees those values in the built object. Before this change, the object was built earlier, from the unstubbed config.

Production monitoring

Not applicable: this changes spec support only and does not run in production.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist.


Generated by GitLab Performance Optimisation

Edited by Kamil Trzciński (Claude)

Merge request reports

Loading
Loading