Skip to content

Commit

Permalink
Merge pull request #8011 from kkmuffme/performance-only-load-files-once
Browse files Browse the repository at this point in the history
Performance only load files once
  • Loading branch information
orklah authored May 26, 2022
2 parents d94f56a + 278e877 commit 06d8e3e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
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

0 comments on commit 06d8e3e

Please sign in to comment.