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
WIP: Try to parallelize linting with amphp #4838
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2afbcd1
added a amphp based pooled linter
staabm 8c73bbb
removed debug out
staabm 1a3a437
activate pooled linter
staabm 0e15e7a
try to properly wait for pool-shutdown
staabm 7603b4b
further progress
staabm 5de0f5e
extracted PooledLintFileTask/PooledLintSourceTask which need to be au…
staabm c27932a
added phpdoc
staabm 791b05d
update to latest amphp/parallel 1.3
staabm f21e1c1
added phpdoc
staabm 6b42442
implemented PR feedback
staabm b02a0f1
dont throw Exceptions accross process boundaries
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace PhpCsFixer\Linter; | ||
|
||
use Amp\Parallel\Worker\Environment; | ||
use Amp\Parallel\Worker\Task; | ||
use PhpCsFixer\Linter\TokenizerLinter; | ||
|
||
class PooledLintFileTask implements Task { | ||
private $path; | ||
|
||
/** | ||
* @param string $path | ||
*/ | ||
public function __construct($path) | ||
{ | ||
$this->path = $path; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function run(Environment $environment) { | ||
$linter = new TokenizerLinter(); | ||
|
||
try { | ||
$linter->lintFile($this->path)->check(); | ||
return true; | ||
} catch( LintingException $e) { | ||
// its not easy to handle a exception thrown within a task on the caller site. | ||
// therefore we transform it to a string a re-create the exception later on. | ||
return $e->getMessage(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace PhpCsFixer\Linter; | ||
|
||
use Amp\Parallel\Worker\Environment; | ||
use Amp\Parallel\Worker\Task; | ||
use PhpCsFixer\Linter\TokenizerLinter; | ||
|
||
class PooledLintSourceTask implements Task { | ||
private $source; | ||
|
||
/** | ||
* @param string $source | ||
*/ | ||
public function __construct($source) | ||
{ | ||
$this->source = $source; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function run(Environment $environment) { | ||
$linter = new TokenizerLinter(); | ||
|
||
try { | ||
$linter->lintSource($this->source)->check(); | ||
return true; | ||
} catch( LintingException $e) { | ||
// its not easy to handle a exception thrown within a task on the caller site. | ||
// therefore we transform it to a string a re-create the exception later on. | ||
return $e->getMessage(); | ||
} | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of PHP CS Fixer. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* Dariusz Rumiński <dariusz.ruminski@gmail.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace PhpCsFixer\Linter; | ||
|
||
use Amp\Parallel\Worker\DefaultPool; | ||
use Amp\Parallel\Worker\Environment; | ||
use Amp\Parallel\Worker\Task; | ||
use Amp\Parallel\Worker\Pool; | ||
|
||
class PooledLinter implements LinterInterface | ||
{ | ||
/** | ||
* @var Pool | ||
*/ | ||
private $processPool; | ||
|
||
public function __construct(Pool $pool = null) | ||
{ | ||
$this->processPool = $pool ?? \Amp\Parallel\Worker\pool(); | ||
} | ||
|
||
public function __destruct() | ||
{ | ||
\Amp\Promise\wait($this->processPool->shutdown()); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isAsync() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function lintFile($path) | ||
{ | ||
return new PooledLintingResult($this->processPool->enqueue($this->createTaskForFile($path))); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function lintSource($source) | ||
{ | ||
return new PooledLintingResult($this->processPool->enqueue($this->createTaskForSource($source))); | ||
} | ||
|
||
private function createTaskForFile($path) { | ||
return new PooledLintFileTask($path); | ||
} | ||
|
||
private function createTaskForSource($source) { | ||
return new PooledLintSourceTask($source); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,56 @@ | ||||||
<?php | ||||||
|
||||||
/* | ||||||
* This file is part of PHP CS Fixer. | ||||||
* | ||||||
* (c) Fabien Potencier <fabien@symfony.com> | ||||||
* Dariusz Rumiński <dariusz.ruminski@gmail.com> | ||||||
* | ||||||
* This source file is subject to the MIT license that is bundled | ||||||
* with this source code in the file LICENSE. | ||||||
*/ | ||||||
|
||||||
namespace PhpCsFixer\Linter; | ||||||
|
||||||
use Amp\Parallel\Worker\Task; | ||||||
use Amp\Parallel\Worker\TaskFailureException; | ||||||
use Amp\Promise; | ||||||
use Symfony\Component\Process\Process; | ||||||
|
||||||
/** | ||||||
* @internal | ||||||
*/ | ||||||
final class PooledLintingResult implements LintingResultInterface | ||||||
{ | ||||||
/** | ||||||
* @var string|bool|null | ||||||
*/ | ||||||
private $result; | ||||||
|
||||||
/** | ||||||
* @var Promise | ||||||
*/ | ||||||
private $promise; | ||||||
|
||||||
/** | ||||||
* @param Promise<TokenizerLintingResult> $promise | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
*/ | ||||||
public function __construct(Promise $promise) | ||||||
{ | ||||||
$this->promise = $promise; | ||||||
} | ||||||
|
||||||
/** | ||||||
* {@inheritdoc} | ||||||
*/ | ||||||
public function check() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should return :void |
||||||
{ | ||||||
if (null === $this->result) { | ||||||
$this->result = \Amp\Promise\wait($this->promise); | ||||||
} | ||||||
|
||||||
if (is_string($this->result)) { | ||||||
throw new LintingException($this->result); | ||||||
} | ||||||
} | ||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will work on how the compat with old php can/should look like, after I get it working on current php versions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might want to see #4810.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't want PHP 5, then why do we even need a parallel linter? We can use the tokenising linter on PHP 7?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had the theory that tokenized linting in parallel process is faster then doing it in a single process