8000 `SlevomatCodingStandard\Helpers\TokenHelper#findPrevious()` is very expensive, and chokes on files with many `use` statements · Issue #1657 · slevomat/coding-standard · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
SlevomatCodingStandard\Helpers\TokenHelper#findPrevious() is very expensive, and chokes on files with many use statements #1657
Open
@Ocramius

Description

@Ocramius

I just analyzed a project where a file with tons of use statements easily slows down PHPCS in a quadratic way.

Specifically, the SlevomatCodingStandard\Helpers\TokenHelper#findPrevious() (relatively lightweight) is used in:

  • UseFromSameNamespaceSniff
  • MultipleUsesPerLineSniff
  • UseDoesNotStartWithBackspaceSniff

Along other code locations, like the SniffLocalCache and UseStatementHelper.

IT seems like PHP_CodeSniffer\Files\File#findPrevious() is extremely slow and complex, and iterates over the entire tokens of a file starting from the end: this obviously is problematic when the source code increases in size, and we're analyzing use statements.

I'm wondering whether we should try and optimize the TokenHelper in this project, by minimuizing #findPrevious() calls, or in PHP_CodeSniffer itself, by optimizing the main loop there 🤔

/cc @MatteoBiagini

Here's a rough profile screenshot, to make this a bit more visible:

phpcs-xdebug-profile-sorted-by-self-execution-time

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0