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

Performance only load files once #8011

Merged
merged 2 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/Psalm/Internal/Provider/FakeFileProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use function microtime;
use function strpos;
use function strtolower;

class FakeFileProvider extends FileProvider
{
Expand All @@ -25,8 +24,8 @@ public function fileExists(string $file_path): bool

public function getContents(string $file_path, bool $go_to_source = false): string
{
if (!$go_to_source && isset($this->temp_files[strtolower($file_path)])) {
return $this->temp_files[strtolower($file_path)];
if (!$go_to_source && isset($this->temp_files[$file_path])) {
return $this->temp_files[$file_path];
}

return $this->fake_files[$file_path] ?? parent::getContents($file_path);
Expand All @@ -39,8 +38,8 @@ public function setContents(string $file_path, string $file_contents): void

public function setOpenContents(string $file_path, string $file_contents): void
{
if (isset($this->fake_files[strtolower($file_path)])) {
$this->fake_files[strtolower($file_path)] = $file_contents;
if (isset($this->fake_files[$file_path])) {
$this->fake_files[$file_path] = $file_contents;
}
}

Expand All @@ -66,7 +65,7 @@ public function getFilesInDir(string $dir_path, array $file_extensions, callable
$file_paths = parent::getFilesInDir($dir_path, $file_extensions, $filter);

foreach ($this->fake_files as $file_path => $_) {
if (strpos(strtolower($file_path), strtolower($dir_path)) === 0) {
if (strpos($file_path, $dir_path) === 0) {
$file_paths[] = $file_path;
}
}
Expand Down
50 changes: 24 additions & 26 deletions src/Psalm/Internal/Provider/FileProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,29 @@
use function filemtime;
use function in_array;
use function is_dir;
use function strtolower;

use const DIRECTORY_SEPARATOR;

class FileProvider
{
/**
* @var array<lowercase-string, string>
* @var array<string, string>
*/
protected $temp_files = [];

/**
* @var array<lowercase-string, string>
* @var array<string, string>
*/
protected $open_files = [];
protected static $open_files = [];

public function getContents(string $file_path, bool $go_to_source = false): string
{
$file_path_lc = strtolower($file_path);
if (!$go_to_source && isset($this->temp_files[$file_path_lc])) {
return $this->temp_files[$file_path_lc];
if (!$go_to_source && isset($this->temp_files[$file_path])) {
return $this->temp_files[$file_path];
}

if (isset($this->open_files[$file_path_lc])) {
return $this->open_files[$file_path_lc];
if (isset(self::$open_files[$file_path])) {
return self::$open_files[$file_path];
}

if (!file_exists($file_path)) {
Expand All @@ -50,28 +48,30 @@ public function getContents(string $file_path, bool $go_to_source = false): stri
throw new UnexpectedValueException('File ' . $file_path . ' is a directory');
}

return (string)file_get_contents($file_path);
$file_contents = (string) file_get_contents($file_path);

self::$open_files[$file_path] = $file_contents;

return $file_contents;
}

public function setContents(string $file_path, string $file_contents): void
{
$file_path_lc = strtolower($file_path);
if (isset($this->open_files[$file_path_lc])) {
$this->open_files[$file_path_lc] = $file_contents;
if (isset(self::$open_files[$file_path])) {
self::$open_files[$file_path] = $file_contents;
}

if (isset($this->temp_files[$file_path_lc])) {
$this->temp_files[$file_path_lc] = $file_contents;
if (isset($this->temp_files[$file_path])) {
$this->temp_files[$file_path] = $file_contents;
}

file_put_contents($file_path, $file_contents);
}

public function setOpenContents(string $file_path, string $file_contents): void
{
$file_path_lc = strtolower($file_path);
if (isset($this->open_files[$file_path_lc])) {
$this->open_files[$file_path_lc] = $file_contents;
if (isset(self::$open_files[$file_path])) {
self::$open_files[$file_path] = $file_contents;
}
}

Expand All @@ -81,34 +81,32 @@ public function getModifiedTime(string $file_path): int
throw new UnexpectedValueException('File should exist to get modified time');
}

return (int)filemtime($file_path);
return (int) filemtime($file_path);
}

public function addTemporaryFileChanges(string $file_path, string $new_content): void
{
$this->temp_files[strtolower($file_path)] = $new_content;
$this->temp_files[$file_path] = $new_content;
}

public function removeTemporaryFileChanges(string $file_path): void
{
unset($this->temp_files[strtolower($file_path)]);
unset($this->temp_files[$file_path]);
}

public function openFile(string $file_path): void
{
$this->open_files[strtolower($file_path)] = $this->getContents($file_path, true);
self::$open_files[$file_path] = $this->getContents($file_path, true);
}

public function isOpen(string $file_path): bool
{
$file_path_lc = strtolower($file_path);
return isset($this->temp_files[$file_path_lc]) || isset($this->open_files[$file_path_lc]);
return isset($this->temp_files[$file_path]) || isset(self::$open_files[$file_path]);
}

public function closeFile(string $file_path): void
{
$file_path_lc = strtolower($file_path);
unset($this->temp_files[$file_path_lc], $this->open_files[$file_path_lc]);
unset($this->temp_files[$file_path], self::$open_files[$file_path]);
}

public function fileExists(string $file_path): bool
Expand Down