-
Notifications
You must be signed in to change notification settings - Fork 275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Performance issue with regex in HtmlConverterCoreNodeRenderer #633
Comments
I replaced the regex call with an approximation which is probably ok for html
The same file takes about 150ms to process |
Here's a micro test showing the behaviour of the regular expression versus the non-regex solution: public class T {
private static final String[] INPUTS = {
"Line one\nLine two",
" Leading spaces\nTrailing spaces ",
"Multiple spaces between words",
"\n\nBlank lines\n\n",
"NoSpaces"
};
private static final String[] OUTPUTS = {
"Line one Line two",
"Leading spaces Trailing spaces",
"Multiple spaces between words",
"Blank lines",
"NoSpaces"
};
public static void main(final String[] args) {
for (var i = 0; i < INPUTS.length; i++) {
final var result = regex(INPUTS[i]);
if (!result.equals(OUTPUTS[i])) {
System.out.println("Test failed:");
System.out.println("Input: " + INPUTS[i]);
System.out.println("Expected: " + OUTPUTS[i]);
System.out.println("Got: " + result);
return;
}
}
System.out.println("ORIGINAL tests passed.");
for (var i = 0; i < INPUTS.length; i++) {
final var result = revision(INPUTS[i]);
if (!result.equals(OUTPUTS[i])) {
System.out.println("REVISION test failed!");
System.out.println("Input: " + INPUTS[i]);
System.out.println("Expected: " + OUTPUTS[i]);
System.out.println("Got: " + result);
return;
}
}
System.out.println("ALL tests passed.");
}
private static String regex(final String text) {
return text.trim().replaceAll("\\s*\n\\s*", " ");
}
private static String revision(final String text) {
final var result = new StringBuilder(text.length());
boolean wasSpace = false;
for (final var c : text.toCharArray()) {
final var isSpace = Character.isWhitespace(c);
final var toAppend = isSpace ? ' ' : c;
if (!wasSpace || !isSpace) {
result.append(toAppend);
}
wasSpace = isSpace;
}
return result.toString().trim();
}
} I've simplified the algorithm and showed the edge case that fails. The procedural implementation is not the same as the regular expression. Notice that instantiating the Have you tried pre-compiling the regex as a |
HtmlConverterCoreNodeRenderer.handleTableCell has a call to
String.replaceAll("\\s*\n\\s*", " ")
which can be quite slow. The regex is quite simple and can be sped up by removing the regex.To Reproduce
See attached file
test.html.txt
Expected behavior
The code takes >4000 ms to run on my laptop.
It should take much lesser time.
The text was updated successfully, but these errors were encountered: