[ https://issues.apache.org/jira/browse/AUTOTAG-24?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Hubert Law updated AUTOTAG-24: ------------------------------- Description: Despite AUTOTAG-21 the regex still eats up CPU time due to the inefficiency of the pattern {{s*$}} which, in case of long consecutive white-space in the middle of the body, consumes O(n^2^) time while failing to match (since it will attempt to start matching at each white space position). Matching the actual non-whitespace content directly may be more efficient in this case, e.g {code:java} private static final Pattern PATTERN = Pattern.compile("^\\s*(\\S(?:.*\\S)?)\\s*$"); if (body != null) { final Matcher matcher = PATTERN.matcher(body); if (matcher.matches()) { body = matcher.group(1); } else { body = null; } }{code} Backtracking is limited to the final white spaces, in linear time. was: Despite AUTOTAG-21 the regex still eats up CPU time due to the inefficiency of the pattern {{s*$}} which, in case of long consecutive white-space in the middle of the body, consumes O(n^2^) time while failing to match (since it will attempt to start matching at each white space position). Matching the actual non-whitespace content directly may be more efficient in this case, e.g {{private static final Pattern PATTERN = Pattern.compile("}}{{^\\s*(\\S(?:.*\\S)?)\\s*$");}} {{if (body != null) {}} {{ final Matcher matcher = PATTERN.matcher(body);}} {{ if (matcher.matches()) {}} {{ body = matcher.group(1);}} {{ } else {}} {{ body = null;}} {{ } }} {{}}} Backtracking is limited to the final white spaces, in linear time. > Efficiency of regular expression under AbstractModelBody.evaluateAsString > -------------------------------------------------------------------------- > > Key: AUTOTAG-24 > URL: https://issues.apache.org/jira/browse/AUTOTAG-24 > Project: Tiles Autotag > Issue Type: Improvement > Affects Versions: 1.2.0 > Reporter: Hubert Law > Priority: Minor > Labels: regex > > Despite AUTOTAG-21 the regex still eats up CPU time due to the inefficiency > of the pattern {{s*$}} which, in case of long consecutive white-space in the > middle of the body, consumes O(n^2^) time while failing to match (since it > will attempt to start matching at each white space position). > Matching the actual non-whitespace content directly may be more efficient in > this case, e.g > {code:java} > private static final Pattern PATTERN = > Pattern.compile("^\\s*(\\S(?:.*\\S)?)\\s*$"); > if (body != null) { > final Matcher matcher = PATTERN.matcher(body); > if (matcher.matches()) { > body = matcher.group(1); > } else { > body = null; > } > }{code} > Backtracking is limited to the final white spaces, in linear time. -- This message was sent by Atlassian JIRA (v7.6.3#76005)