Can we get compile time routines?

php.internals

Hans Henrik Bergan

3 years ago
Something like ``` $data = compile_time_routine(function() { return json_decode(file_get_contents("large.json"), true); }); ``` being equivalent at runtime to ``` $data = array(...whatever json_decode returned...); ``` and ``` echo "opcache compiled at " . compile_time_routine(function(){return date(DateTime::RFC3339);}); ``` being equivalent at runtime to ``` echo "opcache compiled at " . "2023-03-16T09:43:00+01:00"; ``` - at my work there are jsons which is being fetched/parsed like ``` $data=json_decode(file_get_contents("large.json"), true); ``` several times per second, while it would be fine for them to only be fetched/parsed every time opcache is reset.

Larry Garfield

3 years ago
On Thu, Mar 16, 2023, at 9:40 AM, Hans Henrik Bergan wrote:
> Something like > ``` > $data = compile_time_routine(function() { > return json_decode(file_get_contents("large.json"), true); > }); > ``` > being equivalent at runtime to > ``` > $data = array(...whatever json_decode returned...); > ``` > and > ``` > echo "opcache compiled at " . compile_time_routine(function(){return > date(DateTime::RFC3339);}); > ``` > being equivalent at runtime to > ``` > echo "opcache compiled at " . "2023-03-16T09:43:00+01:00"; > ``` > > - at my work there are jsons which is being fetched/parsed like > ``` > $data=json_decode(file_get_contents("large.json"), true); > ``` > several times per second, while it would be fine for them to only be > fetched/parsed every time opcache is reset.
You can already sort of do this by having a build script that reads the JSON file and then uses var_export() to dump it to a file. You can then just require the file to get the resulting array. Not precisely the same, but gets the same benefit in this case. --Larry Garfield