Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Put Vite check before Mix #650

Open
wants to merge 1 commit into
base: 1.x
Choose a base branch
from

Conversation

nshiro
Copy link

@nshiro nshiro commented Aug 2, 2024

Now that most people use Vite, why don't we check Vite manifest file before Mix's for performance?

@RobertBoes
Copy link
Contributor

Don't think this would have any sort of significant impact, not to mention PHP caches a file_exists call, so for repeat calls this would even have less of an impact

@nshiro
Copy link
Author

nshiro commented Aug 3, 2024

I should have checked the performance first.
On my computer (Ryzen 7 3700X, PHP 8.3) when I ran the code below, the average of ten runs was 1.021 seconds.
But when I put a mix file check first (which doesn't exist), I got an average time of 0.892 seconds.
The difference is 0.129 seconds for 100,000 hits.
This may not be an insignificant impact. But it could be worth considering in certain contexts.

<?php

$start = microtime(true);

for ($i=1; $i < 100_000; $i++) {
    if (file_exists($manifest = 'public/build/manifest.json')) {
        md5_file($manifest);
        continue;
    }

    if (file_exists($manifest = 'public/build/mix-manifest.json')) {
        md5_file($manifest);
        continue;
    }
}

$end = microtime(true);
$executionTime = $end - $start;
echo "Execution time: " . $executionTime . " seconds";

@RobertBoes
Copy link
Contributor

Is that with or without opcache? I'd imagine that call would benefit greatly from opcache.

@nshiro
Copy link
Author

nshiro commented Aug 3, 2024

Opcache is on.

Interestingly, the time didn't change when I checked only Vite and when I checked only Mix.
I cut out md5_file($manifest) and confirmed.
According to this page,
https://www.php.net/manual/en/function.clearstatcache.php

This function caches information about specific filenames, so you only need to call clearstatcache() if you are performing multiple operations on the same filename and require the information about that particular file to not be cached.

Because there was information that specific filenames referred to the last accessed file, I asked the AI (Claude) the following:
Does PHP's file_exists function only cache the most recent call?

The AI then answered as follows:

The file_exists() function in PHP does not cache results itself. However, PHP's internal file status cache, which is used by several filesystem functions including file_exists(), does cache information about files.
By default, this cache only stores information about the most recently accessed file. This means that repeated calls to file_exists() for the same file within a short time frame might use the cached result, but calls for different files will typically cause a new filesystem check.

If this answer is correct, it seems that the cache is not being used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants