Custom rule is slow
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 19.7k
- Forks
- 2.3k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 11
Description
Describe the bug
When using custom rule in swiftlint it takes 34s to analyse the file (custom rule and content are below).
Using swiftlint lint --benchmark: 34,672: custom_rules
When the same regex is used online on the the same content https://regex101.com/ it took only 0.4ms to find the match.
Seems that comment section in file (any text in that part of code) causing the issue. Longer text is causing much longer processing time.
Am I doing something wrong with regex? Or is it caused by swiftlint custom rule behaviour? Since regex as such seems to be working fine and fast I am not sure where should I fix my issue.
Complete output when running SwiftLint, including the stack trace and command used
$ swiftlint lint
Environment
- SwiftLint version 0.47.0
- Installation method used - Homebrew
- Paste your configuration file:
custom_rules:
# Wrong:
# MyClassTest {
# //missing [My-Class] usage
# }
# Correct:
# MyClassTest {
# let myClass = MyClass()
# }
test_class_name_match_class_under_test:
message: "TestClass should use the name of class under test"
included: ".*Test.\\.swift"
regex: 'class ([a-zA-Z0-9]+)Test.{0,1}: XCTestCase(?:(?!\1)(:?\n|.))*func'
- no nested configurations
- Xcode Version 13.3 (13E113)
ExpenseMetadataManagerTests.swift:
class ExpenseMetadataManagerTests: XCTestCase {
//anything to analyze by regex - this will make it unusable slow
//anything to analyze by regex
//anything to analyze by regex
//anything to analyze by regex
//anything to analyze by regex
//anything to analyze by regex
//anything to analyze by regex
//anything to analyze by regex
//anything to analyze by regex
//anything to analyze by regex
//anything to analyze by regex
//anything to analyze by regex
//anything to analyze by regex
//anything to analyze by regex
//anything to analyze by regex
//anything to analyze by regex
//anything to analyze by regex
//anything to analyze by regex
//anything to analyze by regex
//anything to analyze by regex
//anything to analyze by regex
//anything to analyze by regex
//anything to analyze by regex
//anything to analyze by regex
//anything to analyze by regex
//anything to analyze by regex
private lazy var manager = ExpenseMetadataManager()
func test() {}
}```
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the slowdown with swiftlint lint --benchmark using the reported custom_rules configuration and ExpenseMetadataManagerTests.swift content. Start by tracing custom-rule processing from the lint command, then establish whether the reported regex and comment text trigger the delay; done means the cause and expected behavior are confirmed and covered by an appropriate regression check.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 32/100