Steppschuh / Java-Markdown-Generator

Java library to generate markdown
MIT License
230 stars 47 forks source link

Performance issue in `StringUtil` #29

Open jkronegg opened 1 year ago

jkronegg commented 1 year ago

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).