Java-Markdown-Generator icon indicating copy to clipboard operation
Java-Markdown-Generator copied to clipboard

Performance issue in `StringUtil`

Open jkronegg opened this issue 1 year ago • 0 comments

Observed behavior

In my project, I generate Markdown tables with this library. Using the IntelliJ profiler, I observed that the library takes about 9% of the total time to generate the tables. About 30% of this 9% is consumed in StringUtils.fillUpAligned.

This is most probably because of multiple String concatenation:

public static String fillUpLeftAligned(String value, String fill, int length) {
    if (value.length() >= length) {
        return value;
    }
    while (value.length() < length) {
        value += fill;
    }
    return value;
}

public static String fillUpRightAligned(String value, String fill, int length) {
    if (value.length() >= length) {
        return value;
    }
    while (value.length() < length) {
        value = fill + value;
    }
    return value;
}

For example, using the debugger, I've seen calls with value="", fill="-", length=39 (that is: the String underlying character array is redefined 39 times during the alignment).

Desired behavior

The StringUtil alignment methods should be improved to use better performing String composition (e.g. StringBuilder).

jkronegg avatar Mar 22 '23 10:03 jkronegg