Optimize Styles.isStylePattern() to avoid StackOverflowError #817
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.
Changes for JLine 3.19.0 in commit a27bcd1 adjusted pattern matching to support named colors in style declarations. These changes expanded the scope of valid style declarations, but also increased the possibility of performance problems when parsing long or malformed styles.
Fedora 37 has a default setting for the
LS_COLORS
environment variable that includes style elements which cause performance problems usingStyles.STYLE_PATTERN.matches()
. CallingStyles.lsStyle()
with Azul Zulu Java 8 Update 352 on Fedora 37 causes the JVM to hang due to excessive iteration in regular expression matching. This issue does not occur on Java 11 or 17.Changes in this pull request optimize style evaluation using a compiled
Pattern
for one key-value style element.Styles.isStylePattern()
splits the input style string using the standard separator character, and then evaluates all elements against the compiledSTYLE_ELEMENT_PATTERN
. This approach avoids compiling the style pattern for every call toStyles.isStylePattern()
, and also avoids aStackOverflowError
that occurs with the current implementation when evaluating a test style constructed of 1000 style elements.For reference, the default value of
LS_COLORS
on Fedora 37 is as follows: