Hacker News new | ask | show | jobs
by alserio 1643 days ago
A typical logger.info("user {} did the thing", user) can skip the actual string interpolation and user value stringification, if the log level is not > info. However, logger.info("user " + user + " did the thing") cannot avoid at least the execution of user.toString(), even after jit optimizations, unless the jit could prove that toString does not have side effects. But I don't believe the jvm jit tries to do that. Am I wrong?
1 comments

If user is a string, then #toString() is a trivial method, will inline, and become a no-op.
However, if user happened be an user-defined User object it would be hard to guarantee that it could be inlined
Yes