The DatabaseBackend class currently uses the static serialize()/unserialize() functions. For extensibility (such as a compression step), it would be better to use the serialization.phpserialize service that wraps these two functions.

For example, right now the easiest way for a contrib module to compress the cache data is to swap out the entire cache backend for a new service. With this patch, it would be enough to swap out the regular cache backend's serializer with one that transparently compresses.

Comments

cburschka created an issue. See original summary.

cburschka’s picture

Status: Active » Needs review
StatusFileSize
new5.15 KB

Status: Needs review » Needs work

The last submitted patch, 2: drupal-2886405-cache-serialization.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

cburschka’s picture

Status: Needs work » Needs review
StatusFileSize
new4.75 KB

Status: Needs review » Needs work

The last submitted patch, 4: drupal-2886405-cache-serialization-2.patch, failed testing. View results

cburschka’s picture

Status: Needs work » Needs review
StatusFileSize
new5.64 KB

The cache backend is also used directly by the bootstrap container definition, so we need to update that as well.

(This is not problematic in core, as the phpserializer wrapper class doesn't depend on anything. But it does mean that any contrib alteration cannot affect the container cache, which is accessed by the bootstrap container.)

Status: Needs review » Needs work

The last submitted patch, 6: drupal-2886405-cache-serialization-3.patch, failed testing. View results

cburschka’s picture

Status: Needs work » Needs review
StatusFileSize
new7.53 KB

Updated the unit tests.

Status: Needs review » Needs work

The last submitted patch, 8: drupal-2886405-cache-serialization-4.patch, failed testing. View results

cburschka’s picture

Status: Needs work » Needs review
StatusFileSize
new8.27 KB

And a final unit test.

znerol’s picture

it would be better to use the serialization.phpserialize service that wraps these two functions.

Thanks for the effort. Why do you think so?

dawehner’s picture

My assumption would be that you can swap out the serialization implementation with one of the faster alternatives?

cburschka’s picture

Pretty much, or add other functionality. For example, right now the easiest way for a contrib module to compress the cache data is to swap out the entire cache backend for a new service. With this patch, it would be enough to swap out the regular cache backend's serializer with one that transparently compresses.

I stumbled over this while comparing the storage services for key_value and cache_: The former uses the serialization service, while the latter uses the PHP functions directly, for which I can't see a clear reason.

dawehner’s picture

@cburschka
Please document this in the issue summary, so its easier for others to understand.

znerol’s picture

Issue tags: +needs profiling

Even though Drupal\Component\Serialization\PhpSerialize looks quite innocent, we still might want to check the performance impact of this modification.

Also: Why do we actually serialize here at all? It looks to me that the cache backend is responsible for serialization/deserialization in the first place.

Update: Please ignore that comment, I was under the impression, that we are discussing serialize/unserialize in page cache. Sorry for that.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

borisson_’s picture

Issue summary: View changes
Status: Needs review » Needs work
Issue tags: +Needs reroll

Made a small update to the IS, I wanted to try to the patch but it no longer applies, so added the needs reroll tag.

I don't think we need any explicit test coverage for this, because it's indirectly tested sufficiently. I agree with @znerol though, this needs profiling tests to see if this makes usage slower.

The patch does look quite simple, so that's great.

jofitz’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new8.78 KB

Re-rolled.

Status: Needs review » Needs work

The last submitted patch, 18: 2886405-18.patch, failed testing. View results

jofitz’s picture

Status: Needs work » Needs review
StatusFileSize
new969 bytes
new9.72 KB

Fixed test failure.

borisson_’s picture

I tested this by running ab -n 200 -c 1 to a local drupal install (home page, a view with 5 pages, only page cache)

              min  mean[+/-sd] median   max
Total:        263  343  48.2    335     627
Total:        263  329  13.1    332     383

The first line is without the patch, the second with the patch. It looks like this is a decent improvement. We should probably do a check with xhprof as well but it looks like this patch does improve the request time.

dawehner’s picture

It would be nice to see this on a real life website with a LOT of cache gets.
@borisson_ Page cache basically means you have just a few cache gets. It becomes more interesting if you would have way more cache gets on the page.

I tried to disable page cache and dynamic page cache and reproduced your test. Its hard to say but I think the result is still inside random fluctuation: https://blackfire.io/profiles/compare/a7abdb67-01b2-4f4e-8b9e-9a7f19ba30...
Maybe @berdir could make some profiling for their gigantic installations?

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

borisson_’s picture

@dawehner, yes - having real-world profiling would be a lot better. However, both your tests and mine did find that there isn't a big change that we should worry about.

fabianx’s picture

Issue tags: +Needs change record

I like the patch, but isn't that a pretty hard BC break for the Cache\DatabaseBackend Interface?

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

fabianx’s picture

Status: Needs review » Closed (duplicate)