Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimise performance of getAttribute() calls by caching status of sandbox for the current environment #2359

Closed
wants to merge 1 commit into from

Conversation

mantis
Copy link

@mantis mantis commented Jan 11, 2017

Fixes #2326

@stof
Copy link
Member

stof commented Jan 12, 2017

The issue with tests is probably that the same object hash is reused later by a different Environment object

@mantis
Copy link
Author

mantis commented Jan 12, 2017

I'll need to check what I've done later - the object hash was fabpot's suggestion and I ran phpunit before generating PR and had it passing... So I wonder at what point I've got confused!

@@ -1463,15 +1463,25 @@ function twig_get_attribute(Twig_Environment $env, Twig_Source $source, $object,
throw new Twig_Error_Runtime('Accessing Twig_Template attributes is forbidden.');
}

static $extensionSandbox = array();
Copy link
Contributor

@Koc Koc Jan 12, 2017

Choose a reason for hiding this comment

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

would it better introduce private non-static class property hasSandboxExtension or sandboxExtension?

Copy link
Author

Choose a reason for hiding this comment

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

I've put up a different implementation in #2361 . Have also added what would appear to be a missing call to update the options hash. In terms of the non-static class property - the code I was originally modifying wasn't within a class..

@mantis
Copy link
Author

mantis commented Jan 12, 2017

Regarding the phpunit test failures.... I'm not sure I understand:

PHPUnit 4.2.6 by Sebastian Bergmann.

Configuration read from twig\Twig\phpunit.xml.dist

Testing Twig Test Suite
...SS........................................................ 61 / 1405 ( 4%)
............................................................. 122 / 1405 ( 8%)
............................................................. 183 / 1405 ( 13%)
............................................................. 244 / 1405 ( 17%)
.........................SS.................................. 305 / 1405 ( 21%)
............................................................. 366 / 1405 ( 26%)
............................................................. 427 / 1405 ( 30%)
............................................................. 488 / 1405 ( 34%)
............................................................. 549 / 1405 ( 39%)
............................................................. 610 / 1405 ( 43%)
............................................................. 671 / 1405 ( 47%)
............................................................. 732 / 1405 ( 52%)
............................................................. 793 / 1405 ( 56%)
............................................................. 854 / 1405 ( 60%)
............................................................. 915 / 1405 ( 65%)
............................................................. 976 / 1405 ( 69%)
............................................................. 1037 / 1405 ( 73%)
............................................................. 1098 / 1405 ( 78%)
............................................................. 1159 / 1405 ( 82%)
............................................................. 1220 / 1405 ( 86%)
............................................................. 1281 / 1405 ( 91%)
............................................................. 1342 / 1405 ( 95%)
............................................................. 1403 / 1405 ( 99%)
..

Time: 46.29 seconds, Memory: 24.00MB

OK, but incomplete, skipped, or risky tests!
Tests: 1405, Assertions: 3308, Skipped: 4.

@@ -1463,15 +1463,25 @@ function twig_get_attribute(Twig_Environment $env, Twig_Source $source, $object,
throw new Twig_Error_Runtime('Accessing Twig_Template attributes is forbidden.');
}

static $extensionSandbox = array();
$envHash = spl_object_hash($env);
Copy link
Author

Choose a reason for hiding this comment

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

in terms of the spl_object_hash - does it actually makes more sense to look at the value of $this->optionsHash on the environment? [that would appear to get updated whenever a new extension is added]

@fabpot
Copy link
Contributor

fabpot commented Mar 2, 2018

@mantis Thanks for working on this. I've spent some time today to see how we could optimize it even more, and I've come up with something that is faster and cleaner IMHO. Closing in favor of #2635.

@fabpot fabpot closed this Mar 2, 2018
fabpot added a commit that referenced this pull request Mar 2, 2018
This PR was squashed before being merged into the 2.x branch (closes #2635).

Discussion
----------

Optimize the performance of the dot operator

This is an alternative to #2359 and #2361, and closes #2326.

Basically, we don't need to call `hasExtension()` at runtime for the sandbox check when calling the dot operator as we know if it's enabled at compile time (the cache hash depends on enables extensions).

My benchmarks shows a *huge* performance improvement (**around 20%**) by doing so (especially when not using the sandbox, which is almost always the case).

Commits
-------

c917d35 fixed resetting options hash when overriding all extensions
89876d2 optimized the . operator performance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants