3

Academically curious. Can the JIT take code like this, recognize that the format string is static final and thus precompute the sliced format string, thus optimizing this down to only StringBuilder with minimal appends?

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;
}
2
  • The hypothetical JIT that I am thinking of can. Is there a particular JVM you are considering? Commented Feb 7, 2011 at 15:00
  • 2
    This code screams to be refactored with a PreparedStatement :) Commented Feb 8, 2011 at 9:59

1 Answer 1

5

Theoretically, a JVM could probably grok your example. Meantime, in reality, existing JVMs won't; it's probably not a very lucrative place to spend the budget for optimizations. Especially since string formatting is usually done to serialize data, in which case you'll probably end up spending most of the time waiting for I/O to complete.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.