Do static arrays get stored in opcache?

假如想象 提交于 2019-12-11 04:12:03

问题


Say I have a rather large associative array of 100k elements like so:

$resources = array(
'stone'=>'current_stone.gif',
'stick'=>'uglystick.jpg',
...
);

Stored in a file called resources.php and it will never change at runtime.

I would like to have this data in Zend opcache so as to share it across all processes (saving memory) and possibly speed up the lookup speed.

My current assumption is that, in this form, this array will not get stored in opcache as it's not defined as a static structure anywhere.

How would I go about making sure this data gets into opcache?


回答1:


No you can't store variables in OPcache, but statics in classes work:

class Resource {
    static $image = [
        'stone'=>'current_stone.gif',
        'stick'=>'uglystick.jpg',
        ...
    ];
}
...
echo Resource::$image['stone'], "\n";

This saves all of the opcodes initialising the arrays, but OPcache will still deep copy the version of Resource::$image in the compiled script in SMA into the corresponding class static property in the process space, so you will still have a copy of the HashTable in each of the active processes which are using Resource -- though the strings themselves will be interned and hence shared across all active php requests which are using this class.

If you are using a class autoloader, to load your classes, then you don't even need to do anything other than refer to Resoure::$image... and the autoloader will do the mapping for you.




回答2:


Answer above is incorrect. Variables are stored in opcode cache.

You can test it by creating large set of data, storing it in PHP file and checking cache statistics with opcache_get_status(). Result includes "scripts" array that lists all cached files and used memory.

I wrote several cache files that have nothing but one massive array. One of them is 24.1Mb

Here is dump result of print_r(opcache_get_status()):

Array
(
    [opcache_enabled] => 1
    ...
    [scripts] => Array
    (
        ...
        [/test/noto.php] => Array
            (
                [full_path] => /test/noto.php
                [hits] => 3
                [memory_consumption] => 24591120
                [last_used] => Sat Nov 24 21:09:58 2018
                [last_used_timestamp] => 1543086598
                [timestamp] => 1543086378
            )

        ...
    )
)

So it is definitely stored in memory with all data.

To make sure it is so, I made several files with combined size of about 300Mb of data. Without opcache it takes about 1.5 seconds to load them all. Then after initial load it gets cached and it takes 2 ms to load all data. Almost 1000 times difference.

So storing cache in PHP files that will be cached by Zend OPcache and using include to include it (with try..catch(\Throwable $e) to avoid errors) is by far the most efficient way to cache data.



来源:https://stackoverflow.com/questions/20831232/do-static-arrays-get-stored-in-opcache

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!