Assertions have less overhead and can be disabled completely, see http://php.net/manual/en/function.assert.php.

Since these methods can be called thousands of times per page load, that's worth it.

CommentFileSizeAuthor
#106 interdiff.txt3.48 KBdawehner
#106 2454649-106.patch34.23 KBdawehner
#104 xhprof runs.zip488.47 KBWim Leers
#102 interdiff.txt4.77 KBAki Tendo
#102 2454659-102.diff33.33 KBAki Tendo
#100 interdiff-cache-context-manager.txt23.93 KBAki Tendo
#100 2454659-100.diff33.3 KBAki Tendo
#97 2454659-97.diff15.17 KBAki Tendo
#93 2454659-93.diff11.34 KBAki Tendo
#89 remove-cache-validatetags--2454649-89.diff11.34 KBAki Tendo
#83 register_assert_handle.txt1.04 KBAki Tendo
#83 remove-cache-validatetags--2454649-82.diff11.85 KBAki Tendo
#79 remove-cache-validatetags--2454649-78.diff11.13 KBAki Tendo
#74 patch-73-artifacts.tar_.gz52.65 KBmradcliffe
#73 remove-cache-validatetags--2454649-73.diff10.94 KBAki Tendo
#71 2454649-phpinfo.txt29.19 KBmradcliffe
#70 2454649.sqlite.gz36.19 KBmradcliffe
#66 remove-cache-validatetags--2454649-66.diff10.37 KBAki Tendo
#63 2454649-63.diff10.23 KBAki Tendo
#59 2454649-59.diff11.12 KBAki Tendo
#56 interdiff--2454649-55.txt2.17 KBAki Tendo
#56 patch-only--2454649-55--do-not-test.diff254.8 KBAki Tendo
#56 testable--2454649-55.diff260.94 KBAki Tendo
#54 interdiff.txt13.73 KBdawehner
#54 2454649-54.patch31.87 KBdawehner
#50 interdiff.txt1.02 KBdawehner
#50 2454649-50.patch31.87 KBdawehner
#45 2454649-45.diff63.92 KBAki Tendo
#43 interdiff-43.txt14.82 KBAki Tendo
#43 Cache-only--2454649-43--do-not-test.diff31.83 KBAki Tendo
#43 2454649-43.diff63.21 KBAki Tendo
#41 interdiff-41.txt814 bytesAki Tendo
#41 Cache-patch-only--2454649-41--do-not-test.diff32.07 KBAki Tendo
#41 2454649-41.diff55.7 KBAki Tendo
#38 interdiff-38.txt1.03 KBAki Tendo
#38 2454649-38.diff55.97 KBAki Tendo
#36 interdiff-36.txt1.49 KBAki Tendo
#36 2454649-36.diff56.01 KBAki Tendo
#34 2454649-34.diff53.31 KBAki Tendo
#34 Cache-changes-only--2454649-34--do-not-test.diff33.04 KBAki Tendo
#33 interdiff-31.txt1.09 KBAki Tendo
#33 Cache-Op-31.diff56.22 KBAki Tendo
#29 interdiff-29.txt2.06 KBAki Tendo
#29 Cache-Optimize--2454649-29.diff56.12 KBAki Tendo
#25 Cache-optimizations-with-assert-tools--2454649-25.diff54.73 KBAki Tendo
#23 interdiff.txt1.03 KBAki Tendo
#21 Cache-optimizations-with-assert-tools--2454649-21.diff54.05 KBAki Tendo
#19 Cache-optimizations-with-assert-tools--2454649-19.diff53.33 KBAki Tendo
#19 Cache-optimizations-no-assert-tools--2454649-19--do-not-test.diff33.73 KBAki Tendo
#17 patch-2454649-6.diff55.54 KBAki Tendo
#15 patch-2454649-5.diff54 KBAki Tendo
#13 patch-2454649-4.diff48.08 KBAki Tendo
#11 patch_DB_backend_ValidTagCall.diff45.18 KBAki Tendo
#9 patch_CacheCollectorValidTagCall.diff44.4 KBAki Tendo
#6 cache-opt-1.diff42.42 KBAki Tendo
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Aki Tendo’s picture

I would suggest getting the patch of the referenced ticket in place before starting work with this since it contains code that aids in writing unit tests for classes which can throw assertions, and allows those assertions to be tested cleanly.

Wim Leers’s picture

Title: Use assert() instead of exceptions in Cache::merge(Tags|Contexts) » [PP-1] Use assert() instead of exceptions in Cache::merge(Tags|Contexts)
Status: Active » Postponed

Sounds reasonable.

Wim Leers’s picture

catch’s picture

It'd be useful to have a proof of concept patch here to validate the performance improvement though - and that in turn would help tip the balance towards getting assertion support into 8.0.0 as opposed to a minor release.

Aki Tendo’s picture

Status: Postponed » Active

These patches

https://www.drupal.org/node/2408013#comment-9790657

Implement the goals of this ticket.

Aki Tendo’s picture

FileSize
42.42 KB

At Wim's request, working towards removing validateTags(). I'm going to remove the calls to it one patch at a time since the automated testers online run much faster than my local ones (10 minutes vs. nearly an hour and a half).

First test - the call in /Drupal/Core/Cache/ApcuBackend.php at line 169. Also, Changed Cache:: to static:: in the Cache class itself.

Aki Tendo’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 6: cache-opt-1.diff, failed testing.

Aki Tendo’s picture

Still can't get local simpletests to work on these patches, even under PHP 5.5, due to previously discovered APCu "feature" (a bug they won't fix). Trying this change, passes PHPUnit.

Drupal\Core\Cache\CacheCollector

Aki Tendo’s picture

Status: Needs work » Needs review
Aki Tendo’s picture

Adding the db backend's call in now - passes PHP Unit without adjustement.

Status: Needs review » Needs work

The last submitted patch, 11: patch_DB_backend_ValidTagCall.diff, failed testing.

Aki Tendo’s picture

Status: Needs work » Needs review
FileSize
48.08 KB

Status: Needs review » Needs work

The last submitted patch, 13: patch-2454649-4.diff, failed testing.

Aki Tendo’s picture

Status: Needs work » Needs review
FileSize
54 KB

Status: Needs review » Needs work

The last submitted patch, 15: patch-2454649-5.diff, failed testing.

Aki Tendo’s picture

Status: Needs work » Needs review
FileSize
55.54 KB
Aki Tendo’s picture

Issue tags: +needs profiling

Ready to profile. One profile should be the patch as is, the other with assertions turned off (modify the .htaccess file)

Aki Tendo’s picture

Title: [PP-1] Use assert() instead of exceptions in Cache::merge(Tags|Contexts) » Cache Optimization and hardening -- [PP-1] Use assert() instead of exceptions in Cache::merge(Tags|Contexts)
Parent issue: #2451793: [META] Assert Statement Use in Drupal » #2408013: Adding Assertions to Drupal - Test Tools.
FileSize
33.73 KB
53.33 KB

Attached is the re-roll of the cache optimizations with the finalized assert tools. I've also hardened the cache library forcing calling classes to obey the interface. (a lot of assert('is_string($cid)') calls). Reassigning this ticket's parent to the assert tools.

Status: Needs review » Needs work

The last submitted patch, 19: Cache-optimizations-with-assert-tools--2454649-19.diff, failed testing.

Aki Tendo’s picture

One outlying test needed it's context_tags_manager mock updated. There are a couple dozen assert fails from the stronger demands of the new assertions that I need to sort through.

Aki Tendo’s picture

Status: Needs work » Needs review
Aki Tendo’s picture

FileSize
1.03 KB

For the above.

Status: Needs review » Needs work

The last submitted patch, 21: Cache-optimizations-with-assert-tools--2454649-21.diff, failed testing.

Aki Tendo’s picture

Status: Needs work » Needs review
FileSize
54.73 KB

Let's see how many assert raises this silences.

Status: Needs review » Needs work

The last submitted patch, 25: Cache-optimizations-with-assert-tools--2454649-25.diff, failed testing.

Aki Tendo’s picture

The two remaining issues are out of scope for this ticket -- A test that has not been working, and the views module passing bad data to the cache system.

The last submitted patch, 25: Cache-optimizations-with-assert-tools--2454649-25.diff, failed testing.

Aki Tendo’s picture

In this patch I've corrected the Alias path test to match current behavior, and then silenced the assertion the views module isn't passing.

Aki Tendo’s picture

Status: Needs work » Needs review

Test bot wakeup call.

Status: Needs review » Needs work

The last submitted patch, 29: Cache-Optimize--2454649-29.diff, failed testing.

Wim Leers’s picture

So close! :)

Aki Tendo’s picture

Issue summary: View changes
Status: Needs work » Needs review
FileSize
56.22 KB
1.09 KB

Forgot the return validity assertion must also be muted.

Aki Tendo’s picture

Moving the failing assertions to the previously created child tickets.

Status: Needs review » Needs work

The last submitted patch, 34: 2454649-34.diff, failed testing.

Aki Tendo’s picture

Status: Needs work » Needs review
FileSize
56.01 KB
1.49 KB

Removing two more assertions that aren't safe until the AliasManager is fixed. They will be placed in that child ticket.

Status: Needs review » Needs work

The last submitted patch, 36: 2454649-36.diff, failed testing.

Aki Tendo’s picture

FileSize
55.97 KB
1.03 KB

Change in assert patch broke the tear down. All should work now.

Aki Tendo’s picture

Status: Needs work » Needs review

::kicks the test bot::

Status: Needs review » Needs work

The last submitted patch, 38: 2454649-38.diff, failed testing.

Aki Tendo’s picture

Status: Needs work » Needs review
FileSize
55.7 KB
32.07 KB
814 bytes

Failing is_object assertion moved to it's own ticket. This one should now be clear.

Aki Tendo’s picture

Yay!! Green at last.

Aki Tendo’s picture

Function name change in parent forcing a re-roll.

Status: Needs review » Needs work

The last submitted patch, 43: 2454649-43.diff, failed testing.

Aki Tendo’s picture

Status: Needs work » Needs review
FileSize
63.92 KB

Recently added test needs the cacheContextsManager method defined for the assertion.

Wim Leers queued 45: 2454649-45.diff for re-testing.

Wim Leers’s picture

#2408013: Adding Assertions to Drupal - Test Tools. just landed, so now we can actually do this! Re-tested #45.

Status: Needs review » Needs work

The last submitted patch, 45: 2454649-45.diff, failed testing.

Aki Tendo’s picture

Almost - The tests of this particular group of assertions include checks to see if the assertions are actually firing, and that requires the PHP 7 shim to be finalized.

https://www.drupal.org/node/2536560

That is a much, much smaller issue though.

dawehner’s picture

Status: Needs work » Needs review
FileSize
31.87 KB
1.02 KB

It is certainly still worth to proceed here ... I wanted to test how much we gain with that. This is just the review only patch rebased against HEAD. Yeah assertions!

Function Name Calls Diff Calls
Diff%
Incl. Wall
Diff
(microsec)
IWall
Diff%
Incl.
MemUse
Diff
(bytes)
IMemUse
Diff%
Incl.
PeakMemUse
Diff
(bytes)
IPeakMemUse
Diff%
Current Function
Drupal\Core\Cache\CacheableMetadata::merge 0 N/A% -226 -0.5% 0 0.0% 0 0.0%
Exclusive Metrics Diff for Current Function 5 2.2% 0 N/A% 0 N/A%
Parent functions
Drupal\Core\Render\BubbleableMetadata::merge 0 N/A% -194 -85.8% 0 N/A% 0 N/A%
Drupal\Core\Render\HtmlResponse::addCacheableDependency 0 N/A% -12 -5.3% 0 N/A% 0 N/A%
Drupal\block\Plugin\DisplayVariant\BlockPageVariant::build 0 N/A% -10 -4.4% 0 N/A% 0 N/A%
Drupal\Core\Render\RenderCache::createCacheID 0 N/A% -4 -1.8% 0 N/A% 0 N/A%
Drupal\block\BlockRepository::getVisibleBlocksPerRegion 0 N/A% -3 -1.3% 0 N/A% 0 N/A%
Drupal\Core\Cache\Context\CacheContextsManager::convertTokensToKeys 0 N/A% -3 -1.3% 0 N/A% 0 N/A%
Drupal\Core\Render\Renderer::doRender@1 0 N/A% 0 0.0% 0 N/A% 0 N/A%
Child functions
Drupal\Core\Cache\Cache::mergeContexts 0 N/A% -143 -63.3% 0 N/A% 0 N/A%
Drupal\Core\Cache\Cache::mergeTags 0 N/A% -88 -38.9% 0 N/A% 0 N/A%
Wim Leers’s picture

Yeah, now that #2483433: Optimize CacheableMetadata::merge() + BubbleableMetadata::merge() and #2471232: Optimize Cache::merge*(), by only accepting 2 instead of N arguments have gone in, the gain is bound to be smaller.

Can you describe the test scenario? I'd expected the difference to be bigger.

dawehner’s picture

Can you describe the test scenario? I'd expected the difference to be bigger.

This was frontpage anonymous without page cache but I would suggest to have some actual stats instead.

Status: Needs review » Needs work

The last submitted patch, 50: 2454649-50.patch, failed testing.

dawehner’s picture

Status: Needs work » Needs review
FileSize
31.87 KB
13.73 KB

I hope this fixes some of the problems.

Status: Needs review » Needs work

The last submitted patch, 54: 2454649-54.patch, failed testing.

Aki Tendo’s picture

Pieces of the old assertion patch's traits where being referenced from one of the tests. I think this will run now. Three patches - one is the actual patch, then an interdiff, finally a binding of this patch with #2536560: Runtime Assertion unit and functional testing so that the unit tests can run correctly (some of the tests need to catch \AssertionError )

Wim Leers’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 56: testable--2454649-55.diff, failed testing.

Aki Tendo’s picture

Status: Needs work » Needs review
FileSize
11.12 KB

Rebuild of this patch since so much of the code around it changed.

Wim Leers’s picture

  1. +++ b/core/lib/Drupal/Core/Cache/ApcuBackend.php
    @@ -166,7 +168,7 @@ protected function prepareItem($cache, $allow_invalid) {
    -    Cache::validateTags($tags);
    +    assert(Inspector::assertAllStrings($tags));
    

    <3

  2. +++ b/core/lib/Drupal/Core/Cache/Cache.php
    @@ -99,6 +100,9 @@ public static function mergeMaxAges($a = Cache::PERMANENT, $b = Cache::PERMANENT
    +   *   Use \Drupal\Component\Inspector::assertAllStrings($tags);
    

    This should be wrapped in assert().

  3. +++ b/core/lib/Drupal/Core/Cache/Context/CacheContextsManager.php
    @@ -299,4 +302,20 @@ public function validateTokens(array $context_tokens = []) {
    +  /**
    +   * Wraps ::validateTokens for use with the assert() statement.
    +   *
    +   * The only difference between this function and the above is this returns
    +   * TRUE when runtime assertions are turned off instead of performing any
    +   * check, and it returns TRUE when the tokens are valid instead of NULL.
    +   * The method we are wrapping is retained only for backwards compatibilty.
    +   */
    +  public function assertValidTokens(array $context_tokens = []) {
    

    Incomplete docblock.

  4. +++ b/core/modules/system/src/Tests/Cache/GenericCacheBackendUnitTestBase.php
    @@ -223,7 +223,7 @@ public function testSetGet() {
           $this->fail('::set() was called with invalid cache tags, no exception was thrown.');
    ...
           $this->pass('::set() was called with invalid cache tags, an exception was thrown.');
    
    @@ -422,7 +422,7 @@ public function testSetMultiple() {
           $this->fail('::setMultiple() was called with invalid cache tags, no exception was thrown.');
    ...
           $this->pass('::setMultiple() was called with invalid cache tags, an exception was thrown.');
    

    These messages no longer make sense.

Status: Needs review » Needs work

The last submitted patch, 59: 2454649-59.diff, failed testing.

The last submitted patch, 59: 2454649-59.diff, failed testing.

Aki Tendo’s picture

I'm beginning to think this new test environment has assert disabled. Rerolling against current and trying again as I cannot duplicate errors locally.

Aki Tendo’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 63: 2454649-63.diff, failed testing.

Aki Tendo’s picture

Status: Needs work » Needs review
FileSize
10.37 KB

K, I'm going to do these one at a time - starting with just Cache::validateTags(). Wim's code review taken into account on this patch.

Status: Needs review » Needs work

The last submitted patch, 66: remove-cache-validatetags--2454649-66.diff, failed testing.

Aki Tendo’s picture

Test bots are misconfigured.

http://cgit.drupalcode.org/drupalci_testbot/tree/containers/web/web-5.5/...

Current settings:



[Assertion]
; Assert(expr); active by default.
; http://php.net/assert.active
;assert.active = On

; Issue a PHP warning for each failed assertion.
; http://php.net/assert.warning
;assert.warning = On

; Don't bail out by default.
; http://php.net/assert.bail
;assert.bail = Off

; User-function to be called if an assertion fails.
; http://php.net/assert.callback
;assert.callback = 0

; Eval the expression with current error_reporting().  Set to true if you want
; error_reporting(0) around the eval().
; http://php.net/assert.quiet-eval
;assert.quiet_eval = 0

The semicolon before assert.active needs to be dropped. This explains why the tests are running locally but not online.

mradcliffe’s picture

I looked on my local drupalci testbot after running the Cache test group.

I then exec'd into the web container and looked at the output of phpinfo. It looks like assert is active in the configuration by default. I'll ping someone in #drupal-testing.

php -i -c /etc/php5/cli/php.ini:

assert.active => 1 => 1
assert.bail => 0 => 0
assert.callback => no value => no value
assert.quiet_eval => 0 => 0
assert.warning => 1 => 1
mradcliffe’s picture

FileSize
36.19 KB

Here's my sqlite file from the Cache testgroup run, which corresponds to the production test bots.

mradcliffe’s picture

FileSize
29.19 KB

Uploaded phpinfo.txt from the container as well.

Edit: that php.ini file i posted in irc was from /etc/php5/cli/php.ini which was wrong according to this phpinfo output. Sorry.

The last submitted patch, 63: 2454649-63.diff, failed testing.

Aki Tendo’s picture

Status: Needs work » Needs review
FileSize
10.94 KB

Forcing assertions to be on (thankfully it's a runtime setting). If this works then the container setting is known to be the culprit.

mradcliffe’s picture

FileSize
52.65 KB

I tested your patch locally on the testbot, and I still see fails in the Cache group. I think that assertions are active as proven by the phpinfo output I provided, or there is some other hidden setting that needs to be enabled that we are not thinking of.

Attached tarball of my build artifacts from that test run as well.

Test summary
------------

Drupal\system\Tests\Cache\BackendChainUnitTest               147 passes   2 fails   6 exceptions             
Drupal\system\Tests\Cache\ApcuBackendUnitTest                147 passes   2 fails  13 exceptions             
Drupal\system\Tests\Cache\DatabaseBackendTagTest               8 passes                                      
Drupal\page_cache\Tests\PageCacheTagsIntegrationTest          22 passes                                      
Drupal\system\Tests\Cache\ClearTest                           11 passes                                      
Drupal\system\Tests\Cache\ChainedFastBackendUnitTest         147 passes   2 fails  13 exceptions             
Drupal\system\Tests\Cache\MemoryBackendUnitTest              147 passes   2 fails   2 exceptions             
Drupal\system\Tests\Cache\PhpBackendUnitTest                 147 passes             2 exceptions             
Drupal\system\Tests\Cache\DatabaseBackendUnitTest            160 passes   2 fails  13 exceptions             

Test run duration: 2 min 1 sec

Edit: Yep, just checked the test run and it's still failing.

The last submitted patch, 66: remove-cache-validatetags--2454649-66.diff, failed testing.

Status: Needs review » Needs work

The last submitted patch, 73: remove-cache-validatetags--2454649-73.diff, failed testing.

mradcliffe’s picture

+++ b/core/lib/Drupal/Core/Cache/ApcuBackend.php
@@ -166,7 +168,7 @@ protected function prepareItem($cache, $allow_invalid) {
+    assert(Inspector::assertAllStrings($tags), 'Cache Tags must be strings.');

The set function cannot continue if tags has strings. The code is continuing on

PHP on the test runner is configured to **not** bail when assert fails, and that is the default behavior for PHP.

+++ b/core/modules/system/src/Tests/Cache/GenericCacheBackendUnitTestBase.php
@@ -218,13 +218,20 @@ public function testSetGet() {
+    $assertion_mode = assert_options(ASSERT_ACTIVE);

So instead, this is not necessary, but the tests should bail if assert is FALSE?

The last submitted patch, 73: remove-cache-validatetags--2454649-73.diff, failed testing.

Aki Tendo’s picture

No. We register an assert callback to throw an AssertionError and that should cause a halt. Is the test running catching the exception, logging it and allowing code flow to continue?

I did find one failure to convert validateTags to an assertion - posting that patch. That *might* be the problem. At this point though I'm looking at my own code even harder trying to find the problem.

Status: Needs review » Needs work

The last submitted patch, 79: remove-cache-validatetags--2454649-78.diff, failed testing.

mradcliffe’s picture

Still failing... Do you know where in the chain AssertionError is setup? Maybe KernelTestBase doesn't use Drupal\Component\Assertion\Handle and the exception isn't being handled for PHP5.

I added the PHP7 test. Maybe it will pass under there and that could narrow it down.

The last submitted patch, 79: remove-cache-validatetags--2454649-78.diff, failed testing.

Aki Tendo’s picture

Yeah, registering the handler would help a lot.

Aki Tendo’s picture

Status: Needs work » Needs review
mradcliffe’s picture

+++ b/core/tests/Drupal/KernelTests/KernelTestBase.php
@@ -232,6 +233,7 @@ protected function setUp() {
+    AssertionHandler::register();

Would it make sense in the grand scheme of using assert() to register the Handler in UnitTestCase? KernelTestBase extends UnitTestCase which extends the generic PHPUnit test class.

Aki Tendo’s picture

Possibly - let's see if this is the problem first then deal with that issue over in it's own ticket. The drupal PHPUnit bootstrap calls to register the assert handler at present and I presumed that file would be called for any PHPUnit test. Is that not so?

Note - double calling the handler register function is harmless, but sloppy.

Status: Needs review » Needs work

The last submitted patch, 83: remove-cache-validatetags--2454649-82.diff, failed testing.

The last submitted patch, 83: remove-cache-validatetags--2454649-82.diff, failed testing.

Aki Tendo’s picture

Now with monkey patching! (aka a "fix" that cannot be the permanent one, but it will shed light on the nature of the problem and might fix it).

Aki Tendo’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 89: remove-cache-validatetags--2454649-89.diff, failed testing.

The last submitted patch, 89: remove-cache-validatetags--2454649-89.diff, failed testing.

Aki Tendo’s picture

Status: Needs work » Needs review
FileSize
11.34 KB

Trying with splicing the registry of assertHandle into TestBase.

Aki Tendo’s picture

Ok, pass. Should the code adding the assert handle registry to TestBase be spun out to it's own ticket, or is it fine here?

Wim Leers’s picture

Status: Needs review » Needs work

Just one nitpick:

+++ b/core/modules/system/src/Tests/Cache/GenericCacheBackendUnitTestBase.php
@@ -412,7 +412,7 @@ public function testSetMultiple() {
+    // Calling ::setMultiple() with invalid cache tags. This should fail an assertion.

80 cols violation.


This still needs profiling.

But first, I think this should also handle cache context validation: that should be done in an assertion too, there are much bigger gains to be had there. NW for that.

Aki Tendo’s picture

Ok, fixed the issue above, will appear next patch. Now for cacheContextTag validation.

Aki Tendo’s picture

Ok, now the CacheContextManager::validateTokens

Aki Tendo’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 97: 2454659-97.diff, failed testing.

Aki Tendo’s picture

Status: Needs work » Needs review
FileSize
33.3 KB
23.93 KB

Here we go....

As huge as this looks, it's just adding a mock method with a return of true to cache context mocks wherever they appear in the system.

Wim Leers’s picture

Status: Needs review » Needs work

This looks great! :)

Pretty much only small stuff below.

  1. +++ b/core/lib/Drupal/Core/Cache/ApcuBackend.php
    @@ -166,7 +166,7 @@ protected function prepareItem($cache, $allow_invalid) {
    +    assert('\Drupal\Component\Assertion\Inspector::assertAllStrings($tags)', 'Cache Tags must be strings.');
    
    +++ b/core/lib/Drupal/Core/Cache/CacheCollector.php
    @@ -115,7 +115,7 @@
    +    assert('\Drupal\Component\Assertion\Inspector::assertAllStrings($tags)', 'Cache Tags must be strings.');
    
    +++ b/core/lib/Drupal/Core/Cache/CacheTagsInvalidator.php
    @@ -27,8 +27,7 @@ class CacheTagsInvalidator implements CacheTagsInvalidatorInterface {
    +    assert('Drupal\Component\Assertion\Inspector::assertAllStrings($tags)', 'Cache Tags must be strings.');
    

    s/Tags/tags/

  2. +++ b/core/lib/Drupal/Core/Cache/Context/CacheContextsManager.php
    @@ -299,4 +297,33 @@ public function validateTokens(array $context_tokens = []) {
    +   * @param $context_tokens
    

    Missing typehint: string[].

  3. +++ b/core/lib/Drupal/Core/Cache/Context/CacheContextsManager.php
    @@ -299,4 +297,33 @@ public function validateTokens(array $context_tokens = []) {
    +  public function assertValidTokens($context_tokens) {
    

    Missing typehint: array.

  4. +++ b/core/tests/Drupal/Tests/Core/Cache/CacheTest.php
    @@ -60,6 +61,32 @@ public function testValidateTags(array $tags, $expected_exception_message) {
    +   * Tests Drupal\Component\Assertion\Inspector::assertAllStrings
    

    Missing trailing period.

    We also generally write Class::methodName(), i.e. with parentheses.

    Also missing leading backslash.

  5. +++ b/core/tests/Drupal/Tests/Core/Cache/CacheTest.php
    @@ -60,6 +61,32 @@ public function testValidateTags(array $tags, $expected_exception_message) {
    +   * the test below.  In practice the Inspector is called from the assert()
    

    Two spaces, should be one.

  6. +++ b/core/tests/Drupal/Tests/Core/Cache/CacheTest.php
    @@ -60,6 +61,32 @@ public function testValidateTags(array $tags, $expected_exception_message) {
    +  public function testInspectorAssertAllStrings(array $tags, $expected_exception_message) {
    

    Why are we testing this in CacheTest, despite the docblock indicating this is testing generic assertion functionality?

  7. +++ b/core/tests/Drupal/Tests/Core/Menu/LocalTaskManagerTest.php
    @@ -488,6 +488,10 @@ protected function setupNullCacheabilityMetadataValidation() {
    +    foreach ([NULL, ["user.permissions"], ["route"], ["route", "context.example1"], ["context.example1", "route"], ["context.example1", "route", "context.example2"], ["context.example1", "context.example2", "route"], ["context.example1", "context.example2", "route", "user.permissions"]] as $argument) {
    +      $cache_context_manager->assertValidTokens($argument)->willReturn(TRUE);
    +    }
    

    I don't understand what this is doing.

    If we actually need it: please use single quotes instead of double quotes, just for consistency sake.

Aki Tendo’s picture

Status: Needs work » Needs review
FileSize
33.33 KB
4.77 KB

1. Fixed.
2, 3. The function will accept mixed, but returns FALSE if it isn't an array (first check). Mixed is noted in the @param.
4, 5. Fixed. (Note I forget to hit save and didn't notice until after making the interdiff, so it's missing there. It is in the main patch).
6. This test uses the same data as ::testValidateTags(), which is the previous test. As the docblock says, it's a regression check to insure that the Inspector's assertAllStrings() method will have the same returns as Cache::validateTags() against that data set. I'll admit that it is unusual for the test to be here, but if I moved the test over to the rest of the Inspector tests I'd have to copy over the data provider function as well.
7. I barely understand it. Apparently when writing prophecies you have to state what arguments you should receive in order to pass the test. Each sub array is an argument set that PHPUnit told me it expected but didn't find. It would be cleaner to write it so that any argument would be accepted, but the test is stronger in its current form.

The last submitted patch, 97: 2454659-97.diff, failed testing.

Wim Leers’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -needs profiling
FileSize
488.47 KB

Thanks!

I think a committer will find #102.6 insufficiently convincing, but let's see :)


I did some profiling. I created an Article node, with the body field, image field and tags field (3 tags) filled out. I then posted 6 comments. Then I logged in as user 2.

Page Cache hits
No difference — exact same number of function calls.
Dynamic Page Cache
Run #node-dynamic_page_cache-HEAD Run #node-dynamic_page_cache-PATCH Diff Diff%
Number of Function Calls 47,429 46,891 -538 -1.1%
Incl. Wall Time (microsec) 167,535 164,973 -2,562 -1.5%
Incl. MemUse (bytes) 18,441,016 18,417,360 -23,656 -0.1%
Incl. PeakMemUse (bytes) 18,552,256 18,530,568 -21,688 -0.1%
No Dynamic Page Cache
Run #node-HEAD Run #node-patch Diff Diff%
Number of Function Calls 62,629 61,367 -1,262 -2.0%
Incl. Wall Time (microsec) 209,058 205,798 -3,260 -1.6%
Incl. MemUse (bytes) 20,809,336 20,798,384 -10,952 -0.1%
Incl. PeakMemUse (bytes) 20,939,024 20,928,152 -10,872 -0.1%
Conclusion
This makes a noticeable impact. Even on a simple page, with Dynamic Page Cache enabled, it still saves 1% of function calls (because it contains some placeholdered content: the comment form, comment links). On the same simple page without Dynamic Page Cache, it saves 2% of function calls.
catch’s picture

Status: Reviewed & tested by the community » Needs work

#102.6 - yes let's move things over. It's confusing as written, and just moving code to where it belongs should not be confusing once done, even if the patch size gets a bit bigger.

dawehner’s picture

Status: Needs work » Needs review
FileSize
34.23 KB
3.48 KB

Sure, let's do that.

Wim Leers’s picture

Status: Needs review » Reviewed & tested by the community

+1 for that change.

Fabianx’s picture

RTBC + 1

  • catch committed 72b0180 on 8.0.x
    Issue #2454649 by Aki Tendo, dawehner: Cache Optimization and hardening...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.0.x, thanks!

Aki Tendo’s picture

+1 dawehner. Thanks everyone.

Status: Fixed » Needs work

The last submitted patch, 106: 2454649-106.patch, failed testing.

Fabianx’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.