Skip to content

perf: write runtime cache payloads as plain arrays when OPcache serves them - #174

Merged
dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:perf/cache-format
Oct 24, 2026
Merged

dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:perf/cache-format

Conversation

@Contributolo

Copy link
Copy Markdown
Collaborator

Since a1206b6 cache files store base64_encode(gzcompress(serialize())) so large payloads do not become array literals that are expensive to parse and can exhaust CLI memory. That holds without OPcache. With OPcache the parse happens once per file version, while inflating and unserializing the compressed form runs on every include. A plain array file is compiled once into shared memory, and each include returns the immutable array without parsing or copying it.

export_payload() now writes the plain array when the writing process serves includes from OPcache shared memory (opcache.enable, plus opcache.enable_cli on the CLI, and not opcache.file_cache_only), and the compressed form otherwise. The new blockstudio/cache/compress filter overrides the choice per scope, for example when WP-CLI without OPcache warms the cache for web workers that use it. The cache version moves to 7 so existing files are rewritten.

Per include of the runtime payload (PHP 8.4). Real site with 28 blocks (398 KB serialized), and the same payload scaled to 560 blocks (7.1 MB):

gz, 28 blocks array, 28 blocks gz, 560 blocks array, 560 blocks
Warm worker (OPcache) 0.45 ms, 1.7 MB held <0.001 ms, 0 MB 9.8 ms, 31.6 MB <0.001 ms, 0 MB
New process, shared memory warm 0.90 ms 0.02 ms 15.1 ms 0.03 ms
First compile (once per file version) 0.80 ms 2.8 ms 12.7 ms 53 ms
No OPcache (CLI) 0.76 ms, +2.2 MB peak 2.4 ms, +3.3 MB peak 12.4 ms, +41.6 MB peak 45 ms, +61.6 MB peak
OPcache shared memory ~0 0.6 MB ~0 11.5 MB

Full page views (WordPress 7.1.3, forked in-process requests, ABAB, n=30, 4 rounds): 0.6 to 0.75 ms less per request on all three pages measured, and peak memory 14 → 12 MB.

Trade-offs:

  • A process without OPcache that reads a file written by a web worker parses the array literal (see the last rows). Writers without OPcache keep the compressed form, so CLI-only and OPcache-less hosts are unchanged.
  • OPcache does not cache a file in the first opcache.file_update_protection seconds (2 by default) after it was written, so requests in that window compile it themselves.
  • Each new file version takes OPcache memory until the next OPcache restart.

Tests: the compact-format test forces compression through the filter. New tests cover the plain-array format (round trip including quotes, a backslash, a NUL byte, scalars and nested lists) and the default choice.

Independent of #173 (no conflicts when merged in either order).

Cache files stored base64(gzcompress(serialize())) so large payloads did
not become array literals that are expensive to parse. With OPcache that
parse happens once per file version, while inflating and unserializing
the compressed form runs on every include. A plain array file is compiled
once into shared memory and each include returns the immutable array
without parsing or copying it.

export_payload() now writes the plain array when the writing process
serves includes from OPcache shared memory (opcache.enable, plus
opcache.enable_cli on the CLI, and not opcache.file_cache_only), and keeps
the compressed form otherwise. The new blockstudio/cache/compress filter
overrides the choice per scope, for example when WP-CLI without OPcache
warms the cache for web workers that use it. The cache version moves to 7
so existing files are rewritten in the new format.
@dnnsjsk

dnnsjsk commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Merged and released in Blockstudio 7.7.8, with the reviewed implementation and regression coverage. The release commit passed all 20 CI and distribution checks before publication.

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