Skip to content

[TwigComponent] Minor performance improvement by caching PropertyAccessor::isWritable() calls#3341

Merged
Kocal merged 1 commit intosymfony:2.xfrom
Kocal:twig-component-cache-methods-calls
Feb 12, 2026
Merged

[TwigComponent] Minor performance improvement by caching PropertyAccessor::isWritable() calls#3341
Kocal merged 1 commit intosymfony:2.xfrom
Kocal:twig-component-cache-methods-calls

Conversation

@Kocal
Copy link
Member

@Kocal Kocal commented Feb 10, 2026

Q A
Bug fix? no
New feature? no
Deprecations? no
Documentation? no
Issues Fix #...
License MIT

Still on Kocal/Gotta-Catch-Em-All#11, my Pokemon component has three properties and is rendered 386 times in the page.
We can remove 1155 calls to PropertyAccessor::isWritable() by caching the result of the first call for each property of the component instance. This number can increase of each attributes passed to the component.

AFAIK PropertyAccessor::isWritable($componentInstance, $propertyName) can not return two different values for the same $componentInstance (an object) and $propertyName (a property of the object, or an HTML attr key) arguments.

image

@Kocal Kocal self-assigned this Feb 10, 2026
@carsonbot carsonbot changed the title [TwigComponent] Minor performance improvement by caching PropertyAccessor::isWritable() calls Minor performance improvement by caching PropertyAccessor::isWritable() calls Feb 10, 2026
@Kocal Kocal requested a review from Copilot February 10, 2026 23:19
@carsonbot carsonbot added the Status: Needs Review Needs to be reviewed label Feb 10, 2026
@Kocal Kocal marked this pull request as ready for review February 10, 2026 23:19
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a small cache inside ComponentFactory to avoid repeated PropertyAccessor::isWritable() checks when mounting many instances of the same Twig component, aiming to reduce overhead during repeated renders.

Changes:

  • Add an in-memory cache for PropertyAccessorInterface::isWritable() results keyed by component name and property.
  • Clear the new cache in reset() alongside existing cached reflection data.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 101 to 103
foreach ($data as $property => $value) {
if ($this->propertyAccessor->isWritable($component, $property)) {
if ($this->writableProperties[$componentMetadata->getName()][$property] ??= $this->propertyAccessor->isWritable($component, $property)) {
$this->propertyAccessor->setValue($component, $property, $value);
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$data at this point can include arbitrary attribute keys (e.g. class, data-*, spread attributes), not just actual component properties. Caching writability for every encountered key can cause the cache to grow with the number of distinct attribute names used during the request/worker lifetime. Consider limiting what gets cached (e.g. only cache keys that are valid property names / property paths you explicitly support, or only cache positive isWritable() results), and/or key the cache by the component class (since writability is effectively a class+property concern).

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true, but I think it's fine

@carsonbot carsonbot changed the title Minor performance improvement by caching PropertyAccessor::isWritable() calls [TwigComponent] Minor performance improvement by caching PropertyAccessor::isWritable() calls Feb 11, 2026
@Kocal Kocal requested review from WebMamba, kbond and smnandre February 11, 2026 07:00
Copy link
Member

@kbond kbond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I would have thought the property accessor had its own cache.

@carsonbot carsonbot added Status: Reviewed Has been reviewed by a maintainer and removed Status: Needs Review Needs to be reviewed labels Feb 11, 2026
@Kocal
Copy link
Member Author

Kocal commented Feb 11, 2026

It does, but for computing the property access path IIRC.
But the PropertyAccessor also handles different data types other than object, that's why I think it couldn't cache these calls, when we could.

@Kocal Kocal merged commit eaf1e02 into symfony:2.x Feb 12, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Performance Status: Reviewed Has been reviewed by a maintainer TwigComponent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments