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

Avoid is_readable check when possible #7095

Open
bnomei opened this issue Mar 27, 2025 · 3 comments · May be fixed by #7099
Open

Avoid is_readable check when possible #7095

bnomei opened this issue Mar 27, 2025 · 3 comments · May be fixed by #7099

Comments

@bnomei
Copy link

bnomei commented Mar 27, 2025

Description

The is_readable function seems innocent but since it checks the file on the disk it has a noticable impact on performance when execute thousands of times. My suggestion would be to delay it and just try and fail. This will improve the read function speed about 30-40% based on my tests – which is huge imho.

current

public static function read(string $file): string|false
	{
		if (
			is_readable($file) !== true &&
			Str::startsWith($file, 'https://') !== true &&
			Str::startsWith($file, 'http://') !== true
		) {
			return false;
		}

		return file_get_contents($file);
	}

proposed

public static function read(string $file): string|false
	{
		if (
			Str::startsWith($file, 'https://') !== true &&
			Str::startsWith($file, 'http://') !== true
		) {
			return false;
		}

		try { 
                    return file_get_contents($file);
                } catch (Exception $ex) {
                    return false;
                }
	}

public static function read(string $file): string|false

@bastianallgeier
Copy link
Member

I've added a PR for this. We need to add @ to surpress warnings. #7099 @bnomei could you run another performance test on this. I don't think the @ operator should have any negative effect, but who knows :)

@bnomei
Copy link
Author

bnomei commented Mar 28, 2025

@bastianallgeier used the code from your PR in my 20k pages test setup and got 3-4% total performance boost and like i measured before 30-40% inside the F::read itself.

@bnomei
Copy link
Author

bnomei commented Mar 28, 2025

btw the file_exists in PlainTextStorage::read/exists are similar calls, (most likely) avoidable assuming they do exist in most cases and fail when the read fails... they amount to 2-4% total wall time in my test suite.

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

Successfully merging a pull request may close this issue.

2 participants