Skip to content

feat: Add optional template caching support to EnvironmentBuilder - #1294

Closed
CybotTM wants to merge 3 commits into
phpDocumentor:mainfrom
CybotTM:perf/twig-template-caching
Closed

feat: Add optional template caching support to EnvironmentBuilder#1294
CybotTM wants to merge 3 commits into
phpDocumentor:mainfrom
CybotTM:perf/twig-template-caching

Conversation

@CybotTM

@CybotTM CybotTM commented Jan 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds a $cacheDir parameter to EnvironmentBuilder to enable Twig's built-in template caching. When provided, compiled templates are stored on disk and reused, avoiding recompilation on every render.

Changes

// Before
public function __construct(ThemeManager $themeManager, iterable $extensions = [])

// After  
public function __construct(
    ThemeManager $themeManager,
    iterable $extensions = [],
    string|false $cacheDir = false,  // NEW
)

Behavior

$cacheDir value Behavior
false (default) No caching - same as current behavior
'/tmp/twig' Compiled templates cached to directory

The auto_reload option is enabled, so templates automatically recompile when the source file changes.

Performance Impact

~25% faster template rendering for repeated builds, especially beneficial for:

  • Large documentation projects with many pages
  • CI/CD pipelines with warm caches
  • Development workflows with frequent rebuilds

Backward Compatibility

This is fully backward compatible:

  • Default value false preserves current behavior
  • No changes required for existing code
  • Opt-in via DI configuration

Usage Example

// In DI configuration
->set(EnvironmentBuilder::class)
->arg('$cacheDir', sys_get_temp_dir() . '/guides-twig-cache')

Test Plan

  • All existing tests pass
  • No code style violations
  • CI validation

CybotTM added 3 commits July 1, 2026 14:02
Add a $cacheDir parameter to enable Twig's built-in template caching.
When provided, compiled templates are stored on disk and reused,
avoiding recompilation on every render.

Performance impact: ~25% faster template rendering for repeated builds.

- New parameter: $cacheDir (string|false, default: false)
- When false (default): no caching (backward compatible)
- When string: compiled templates cached to that directory
- auto_reload enabled: templates recompile when source changes

This is a backward-compatible change - existing code continues
to work without modification.
Tests cover:
- Environment creation without caching (default)
- Environment creation with cache directory
- Debug extension is always enabled
- Custom extensions are properly added
- Context setting adds global variable
- Auto-reload is enabled for development
- setEnvironmentFactory replaces environment
@CybotTM
CybotTM force-pushed the perf/twig-template-caching branch from 454c215 to 2512611 Compare July 1, 2026 12:02
@CybotTM

CybotTM commented Aug 15, 2026

Copy link
Copy Markdown
Contributor Author

Closing this. As shipped the option cannot be reached at all: the service definition in packages/guides/resources/config/guides.php sets only $extensions and $themeManager, and the configuration tree has no cache node, so the guides CLI — the consumer the documentation addresses — has no way to enable it.

The added tests do not exercise caching either. They assert that the constructor stored the option, and the theme fixture directories contain nothing but .gitignore, so no template is ever compiled, reused or invalidated. The services.yaml snippet in the documentation would also break a user's build: redefining the service id replaces the bundle definition instead of merging into it, so $extensions falls back to its empty default and every guides Twig extension disappears.

Beyond the implementation, the objection raised in #1287 stands: a compiled-template cache only pays off when the filesystem survives between runs, which is not the case for the CI and container setups this tool mostly runs in. Anything in this direction would have to start at the configuration layer, and only once that question is settled.

@CybotTM CybotTM closed this Aug 15, 2026
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