Skip to content

[6.x] Work out fieldtype config defaults once per request - #15805

Open
DanielDarrenJones wants to merge 1 commit into
statamic:6.xfrom
DanielDarrenJones:perf/6x-fieldtype-config-defaults
Open

DanielDarrenJones wants to merge 1 commit into
statamic:6.xfrom
DanielDarrenJones:perf/6x-fieldtype-config-defaults

Conversation

@DanielDarrenJones

Copy link
Copy Markdown
Contributor

Fieldtype::config($key) is called constantly while augmenting and rendering, and every call rebuilt the whole config as collections: each config field's default value, merged with the field's own config, even when asking for a single key.

This changes it so that:

  • Single-key reads return the field's own value, or that config field's default, directly.
  • Each default is worked out once per config fields collection (the one Blink already keeps per fieldtype for the request), and only when it's first needed, so defaults for keys the field sets itself are still never computed.
  • Computed (computed:) defaults are still resolved on every read.
  • Reading the whole config gives the same array, in the same order, as before.

HasSelectOptions::getLabel() also finds the option with a loop rather than a filtered collection.

Benchmark

A generated site on 6.40.0 (entries, structured collections, navs of three sizes), six warm scenarios: three sets of page renders and three nav renders. Each request runs in its own PHP process, as PHP-FPM serves it (boot not timed, opcache on), on an M1 Pro with PHP 8.5. The rendered HTML is byte-identical to stock for two different generated sites.

This change on its own is 11% faster (geometric mean over the six scenarios).

Tests

Two new tests in FieldtypeTest:

  • A config field's default is computed once across reads, and again after Blink is flushed. This fails without the change.
  • Computed defaults are resolved on every read.

The existing it_only_computes_default_values_for_config_fields_missing_from_the_raw_config still passes. The full suite passes.

Fieldtype::config() rebuilt the whole config (every config field's default,
merged with the field's own config) as collections on each call, even when
asking for a single key. Single-key reads now return the field's own value or
that config field's default directly, and each default is worked out once per
config fields collection, which Blink already keeps for the request. Computed
defaults are still resolved on every read.

HasSelectOptions looks up an option's label with a loop instead of building
a filtered collection.

This branch has not been deployed

No deployments
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