# Eliminating stat() calls in opcode caches for include_once

- Thread: https://externals.io/message/64026
- Original archive: https://news-web.php.net/php.internals/64026
- Started by Dmitry Stogov on 2012-11-21T14:15:54+00:00
- Messages: 1
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #64026 — Dmitry Stogov — 2012-11-21T14:15:54+00:00

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

Hi Pierre,

To avoid `stat()` syscalls the opcode cache has to override
zend_resolve_path() and zend_open_stream() callbacks with something like
the following functions.

static char* my_resolve_path(const char *filename, int filename_len
TSRMLS_DC)
{
        if ((EG(opline_ptr) &&
             *EG(opline_ptr) &&
             (*EG(opline_ptr))->opcode == ZEND_INCLUDE_OR_EVAL &&
             ((*EG(opline_ptr))->extended_value == ZEND_INCLUDE_ONCE ||
              (*EG(opline_ptr))->extended_value == ZEND_REQUIRE_ONCE))) {

                CACHE_BUCKET *b = OPCODE_CACHE_FIND(filename TSRMLS_CC);
                if (b) {
                        return b->real_path;
                }
        }
        return orig_resolve_path(filename, filename_len TSRMLS_CC);
}

static int my_stream_open(const char *filename, zend_file_handle *handle
TSRMLS_DC)
{
        if ((EG(opline_ptr) &&
             *EG(opline_ptr) &&
             (*EG(opline_ptr))->opcode == ZEND_INCLUDE_OR_EVAL &&
             ((*EG(opline_ptr))->extended_value == ZEND_INCLUDE_ONCE ||
              (*EG(opline_ptr))->extended_value == ZEND_REQUIRE_ONCE))) {

                CACHE_BUCKET *b = OPCODE_CACHE_FIND(filename TSRMLS_CC);
                if (b) {
                        handle->type = ZEND_HANDLE_FILENAME;
                        handle->opened_path = estrdup(b->real_path);
                        return SUCCESS;
                }
        }
        return orig_steam_open(filename, handle TSRMLS_CC);
}

Thanks. Dmitry.
