# Can we get compile time routines?

- Thread: https://externals.io/message/119726
- Original archive: https://news-web.php.net/php.internals/119726
- Started by Hans Henrik Bergan on 2023-03-16T14:40:46+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #119726 — Hans Henrik Bergan — 2023-03-16T14:40:46+00:00

URL: https://externals.io/message/119726#119726

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.

---

## #119727 — Larry Garfield — 2023-03-16T14:57:49+00:00

URL: https://externals.io/message/119726#119727 · In reply to: #119726

> 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
