Java JIT - what kind of optimizations are possible?

Academically curious. Can JIT take such code, admit that the format string is static final and thus precommute the cut-off format string, thus optimizing it only to StringBuilder with minimal additions?

public static String buildDeleteSql(BaseObject object)
{
    String table;
    String schema;

    String deleteSql = String.format(
            "DELETE FROM %s.%s WHERE %s = '%s' AND %s = '%s'",
            schema,
            table,
            BaseObject.ATTR_ID,
            StringUtils.escapeForSQLString(object.getId()),
            BaseObject.ATTR_REVISION,
            StringUtils.escapeForSQLString(object.getRevision())
        );

    return deleteSql;
}
+3
source share
1 answer

Theoretically, the JVM could probably look into your example. Meanwhile, in fact, existing JVMs will not; this is probably not a very profitable place to spend the budget on optimization. Moreover, formatting strings is usually done to serialize the data, in which case you will probably end up spending most of the time waiting for the I / O to complete.

+5
source

Source: https://habr.com/ru/post/1790467/


All Articles