Skip to content

Commit

Permalink
Fix performance issue for regexp in Reporting.kt (#4550)
Browse files Browse the repository at this point in the history
A regexp object is compiled into an internal data structure during initialization.
This could to a potential performance impact, if the `Regex()` constructor is called multiple times for the same pattern.
Since the private function `truncatedMessage()` could be called a lot, this PR fixes this potential performance issue.

Because of the overhead of object instantiation and regular expression compilation, reusing the initialized object is the recommended way.
  • Loading branch information
schalkms committed Jan 31, 2022
1 parent 1fbe3b8 commit fb1b604
Showing 1 changed file with 2 additions and 1 deletion.
Expand Up @@ -49,6 +49,7 @@ const val DETEKT_OUTPUT_REPORT_PATHS_KEY = "detekt.output.report.paths.key"
const val DETEKT_OUTPUT_REPORT_BASE_PATH_KEY = "detekt.output.report.base.path"

private const val REPORT_MESSAGE_SIZE_LIMIT = 80
private val messageReplacementRegex = Regex("\\s+")

fun Config.excludeCorrectable(): Boolean = subConfig(BUILD).valueOrDefault(EXCLUDE_CORRECTABLE, false)

Expand All @@ -75,7 +76,7 @@ fun Detektion.filterAutoCorrectedIssues(config: Config): Map<RuleSetId, List<Fin

private fun Finding.truncatedMessage(): String {
val message = messageOrDescription()
.replace(Regex("\\s+"), " ")
.replace(messageReplacementRegex, " ")
.trim()
return when {
message.length > REPORT_MESSAGE_SIZE_LIMIT -> "${message.take(REPORT_MESSAGE_SIZE_LIMIT)}($ellipsis)"
Expand Down

0 comments on commit fb1b604

Please sign in to comment.